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

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

Ivse05Ivse05 Posts: 23Questions: 7Answers: 0

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

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    Answer ✓

    The field().update() is used to apply an options list to a field. See the select 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

  • Ivse05Ivse05 Posts: 23Questions: 7Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited March 2020

    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 with field().update() and append the value for that row (found in the data 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

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    I tried the append option with field().update() and found it doesn't work with Select2. The docs state this:

    Plug-ins such as that for Select2 may also provide this method, please check their documentation for full details and all methods available for each.

    Using the technique described in the Select2 Programmatic Control docs does work. The defaultSelected and selected options need to be set true to set the default.

    Maybe something like this:

      editor.on('initEdit', function(e, node, data, items, type) {
        var field = editor.field('name');  // Get the field
        var selectVal = data.name;      // Get the value of the `name` field
    
        var select = $(field.node()).find('select');  // Find the fields select
        var newOption = new Option(selectVal, selectVal, true, true);  // Append and set default the name
        $(select).append(newOption).trigger('change');  // Append to the select
    
      });
    

    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

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    "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.

This discussion has been closed.