Type select2 not showing saved value of dependent filed after ajax call unlike select type

Type select2 not showing saved value of dependent filed after ajax call unlike select type

karmendrakarmendra Posts: 21Questions: 7Answers: 0

I upgraded to latest Editor v 1.9.2, copied the latest editor.select2.js plugin, but having issue retaining the select field's saved option on edit.

...
{label: "Country", name: "country", type: 'select2', options: @json($countries)},
{label: "State", name: "state", type: 'select', className: 'required'},
...

with select in second field when I select a row with county India and state Karnataka and hit Edit. Country is automatically selected as India in the first dropdown and triggers the ajax to fetch list of states and then Karnataka is automatically selected in the second dropdown.

editor.dependent('country', function (val, data, callback) {
    $.ajax({
        url: '/getstates,
        data: {
            country: val
        },
        dataType: 'json',
        success: function (json) {
            editor.field('state').update(json);
            callback(json);
        }
    });
});

Now the moment I change the type for state field to select2

{label: "State", name: "state", type: 'select2', className: 'required'},

First filed is automatically selected to India like before but after the ajax call the state filed is not Karnataka, it is either empty or the first value in the newly loaded options.

Is there a fix for this, for now I am using select type for the second field.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I think what might be happening here is that the select2 input type is not remembering the previous value if there is no matching option in the list. The select has some special code for exactly that, so the value can be reselected if the next update has it.

    I don't have an immediate fix for this I'm afraid, but I have got an issue filed for it now that we will look at alongside the next Editor release.

    Allan

  • karmendrakarmendra Posts: 21Questions: 7Answers: 0

    Thanks for acknowledging this, but I can confirm that the selected value is present in the updated list. but it is not selected.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes, but the problem is that the value isn't in the list when the Editor is initialised. So at that point the value can't be selected. Then the list is updated with a value that does have the value - but Select2 has already lost the selected value...

    Allan

  • karmendrakarmendra Posts: 21Questions: 7Answers: 0

    Hi Allan,

    Can you point me to special code for The select to re-selected if the next update has value, I will try to have a workaround till we see an official fix for this in new release.

    Thanks,
    Karmendra

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Sure - this is the code in Editor for the field().update() method for the select field type:

        update: function ( conf, options, append ) {
            fieldTypes.select._addOptions( conf, options, append );
    
            // Attempt to set the last selected value (set by the API or the end
            // user, they get equal priority)
            var lastSet = conf._lastSet;
    
            if ( lastSet !== undefined ) {
                fieldTypes.select.set( conf, lastSet, true );
            }
    
            _triggerChange( conf._input );
        },
    

    So basically it uses the _lastSet property (which was set in the set method) to attempt to reselect the values.

    Allan

This discussion has been closed.