Losing select2 field value when loading options via ajax

Losing select2 field value when loading options via ajax

kevan.watkinskevan.watkins Posts: 22Questions: 5Answers: 0

I have a select2 field (problem_field) whose options depend on the value of another field (dependant_field).
I update the options with:

editor.dependent("dependant_field", function (val) {
  $.ajax({
            type: "POST",
            ...,
            success: function (options) {
                editor.field("problem_field").update(options);
    });

If I remove the ajax call and load the options directly then it works.
If I leave the ajax call in place and change the field type to "select" then it works.

So it seems that select field allows you to set value not in options, but select2 does not.
Is there a work around to this?
Something I can do in initEdit to save the field values, and set them after field().update() ?

Kind regards,
Kevan

Answers

  • allanallan Posts: 50,047Questions: 1Answers: 7,403 Site admin

    Just to check my understanding, if problem_field is select then it works, but if it is a select2 it doesn't? That sounds like a bug in the select2 integration.

    Are you able to give me a link to the page showing the issue please? I'm not aware of a problem in the select2 integration here.

    Allan

  • kevan.watkinskevan.watkins Posts: 22Questions: 5Answers: 0

    Correct.

    I will set up a test on http://live.datatables.net and post tonight.

  • kevan.watkinskevan.watkins Posts: 22Questions: 5Answers: 0

    Hmm, is there a URL for editor.select2.js so I can include it in the live.datatables.net example?

  • kevan.watkinskevan.watkins Posts: 22Questions: 5Answers: 0
    edited June 2017

    Here is a simple demonstration:
    http://live.datatables.net/zubiqeke/5/edit?html,js,output

    If you comment out as follows:

    //editor.on('initEdit', function () { 
        editor.field("field1").update(options);
        editor.field("field2").update(options);
    //});
    

    Then it will work.
    But if the select2 field options are set after the field value assignment is done, e.g. in initEdit, it's values are lost if they do not exist in the options.

  • allanallan Posts: 50,047Questions: 1Answers: 7,403 Site admin
    edited June 2017

    Thanks for the example - much appreciated! I certainly do see the issue. Let me dig into it and get back to you. I think I know what needs to happen... (i.e. what is causing the issue)

    Allan

  • ManuLanManuLan Posts: 1Questions: 0Answers: 0

    Did you find a solution? I have the same problem.

  • allanallan Posts: 50,047Questions: 1Answers: 7,403 Site admin

    This should have been resolved in Editor already. Could you give me a link to your page please @ManuLan.

    Allan

Sign In or Register to comment.