How do I set value of a Select2 field?

How do I set value of a Select2 field?

dcdcdcdc Posts: 1Questions: 1Answers: 0

Hello -

LONG time user of DataTables, still learning about Editor.

I am attempting to set the value of a field in Editor which is marked as a "Select2" field type which uses as set of objects in the format {label: "xxxxxx", value: "xxxxx"}

I am able to set other Select2 fields if using an array of values (of which each index is used as both label and value), but not a field which is using a set of Objects.

I am cycling through the array of Options directly from the Editor's field instance, and setting the value if there is a match to another value.

I have tried setting the value to a matching object of the exact same structure, setting it to the value: of the object, setting it to the label: of the object. nothing is working.

Thanks very much.

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @dcdc ,

    You should just be able to say:

    editor.field(your_field).set([{ label: 'red', value: 11 }, { label: 'white', value: 22 }, { label: 'blue', value: 33 }]);
    

    When you say nothing is working, are you seeing console errors? Is the field appearing empty? If you could link to the page, that would be helpful.

    Cheers,

    Colin

This discussion has been closed.