DataTables Editor - Plugins - Select2: Why not clear the dropdown when needAjax === true?

DataTables Editor - Plugins - Select2: Why not clear the dropdown when needAjax === true?

washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

I noticed that when editing rows back to back, my editor's select2 fields still have the old values. I know I can do update(null) but why is this not done as part of the plugin? Ideally id like a loading dialog.

This question has an accepted answers - jump to answer

Answers

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    I have sort of solved the first half of this. If you do _fieldTypes.select2.update(conf); right before the ajax call it will clear the dropdown.

    This is because _addOptions gets called and then conf._input[0].options.length is set to 0 clearing the options.

    I still dont have a good way to show a spinner or something.

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Final code, put this above the ajax call.

    var loadingText = 'Loading...';
    _fieldTypes.select2.update(conf, [loadingText]);
    _fieldTypes.select2.set(conf, loadingText);
    conf._input.prop("disabled", true);
    

    You get the following until the value arrives:

    Slightly more advanced, but you can set loadingText to HTML if you set your select2 options escapeMarkup to not escape markup (see docs). Check out the dots:

    This was very easy I just grabbed the code snippets from here: https://martinwolf.org/before-2018/blog/2015/01/pure-css-savingloading-dots-animation/

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Clearing the old values by default isn't done in the plug-in as it might be that you want to reuse the values. I guess that isn't always true if you are Ajax loading the options, but it is still a possibility.

    Great to hear that you've got it working the way you need now.

    Allan

This discussion has been closed.