Passing label & value to update method for a field

Passing label & value to update method for a field

paulhickmanpaulhickman Posts: 21Questions: 1Answers: 0
edited February 2013 in Editor
In the documentation for the update method you have the example:

[code]
editor.add( {
"type": "select",
"label": "Title:",
"name": "title",
"ipOpts": [
{ "label": "Mr", "value": "Mr" },
{ "label": "Ms", "value": "Ms" },
{ "label": "Mrs", "value": "Mrs" },
{ "label": "Miss", "value": "Miss" },
{ "label": "Dr", "value": "Dr" }
]
} );

editor.field('title').update( [
"Mr", "Ms", "Mrs", "Miss", "Dr"
] );
[/code]

What would you put in the call to update() if the label and values were different?

Replies

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    That example is a bit misleading actually (I'll correct that, thanks for flagging it up). It isn't incorrect, its just redundant as it is exactly the same thing...

    In the `select` field type, you can pass in:

    - An array of objects, where the objects have `label` and `value` properties
    - An array of values (strings, integers)

    If an array of values is passed in, the value is used for both the label and value, if an object is passed in, each is used as appropriate. You can actually mix the two types together if you wanted as well...

    In future I'll add the ability to pass in an array of functions or just a function that returns an array.

    So that example is misleading in that the update actually just sets exactly the same options as are already present! However, it should work correctly.

    > What would you put in the call to update() if the label and values were different?

    Just to clarify, the `update` call will completely replace the values that are already present. They have no bearing on the new values.

    Regards,
    Allan
  • paulhickmanpaulhickman Posts: 21Questions: 1Answers: 0
    So I could write the following to replace the items:

    [code]
    editor.field('title').update( [
    { label: "Mr", value: 0}, { label:"Ms", value: 1}
    ] );
    [/code]
  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    Absolutely. You could also use that array of objects in the field constructor's `ipOtps` .

    Allan
  • paulhickmanpaulhickman Posts: 21Questions: 1Answers: 0
    Thanks.

    I understood I could use it in ipOpts. What wasn't clear from the example in the documentation was that the update() method offered the same flexibility.
  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    I'll clarify that in the documentation.

    Thanks,
    Allan
This discussion has been closed.