Editor. Current cell value as default option for select2 field.
Editor. Current cell value as default option for select2 field.

Hello. Help me please, i don't know what to do next. Please.
I've got a simple table
ID NAME SURNAME
NAME and SURNAME are fields of select2 type
When I am initializing a select2 field on a field with the value "John", for example, the select2 appears with empty options list. How can I set current cell value "John" as a default option for select2?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
The
field().update()
is used to apply an options list to a field. See theselect
decos for more details.As Allan mentioned in your other thread if "John" matches one of the values in the options list it should be selected. Here is an example using the
select
field type. The same should work for select2.Kevin
I see, so there is no built in feature that can automatically make current cell value as a default option. That is what i asked about. I have several thousands potential options and according to your answers i need to pass all the options with the list of data to make editor see the current cell value. Or set the field value manually and programatically outside the editor settings. Than I'll better check null on server, but I thought there was some simple way of doing it... Thank you for your time, Kevin.
Not sure I understand what you are tying to do. Do you have an options list for the field? You can use the
initEdit
event to update the options list withfield().update()
and append the value for that row (found in thedata
parameter) to the list.EDIT: You can use this technique to create a one item options list with just the data for that field.
Kevin
I tried the append option with
field().update()
and found it doesn't work with Select2. The docs state this:Using the technique described in the Select2 Programmatic Control docs does work. The
defaultSelected
andselected
options need to be settrue
to set the default.Maybe something like this:
Not sure if this is what you are looking for. If not please provide more details of the options, how you are applying it. Maybe post some code and example data.
Kevin
"I see, so there is no built in feature that can automatically make current cell value as a default option."
Well, the current cell value IS the default option because it is actually the field content. And that is what the example shows that Kevin quotes.
Here is an example from my own coding with a multi-select field ajax sourced with potentially 100s of options: The options shown on top are the ones selected. They are the "default" if you will.
I think there is a different problem in your code that makes the selected values not show up in the select2 Editor field.