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: 12Questions: 5Answers: 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: 51,422Questions: 1Answers: 7,754 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: 12Questions: 5Answers: 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: 51,422Questions: 1Answers: 7,754 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

Sign In or Register to comment.