Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

javascript - How to set selected values in ajax multi Select2

I have saved data in the database and want to load it as selected in the Select2 field on page load. What I`ve tried is this(whit example data):

productsSelect.select2('val', [{'id':1,'text':'lala'},{'id':2,'text':'blabla'}])

I simply can't find such option in the documentation or I can't understand it probably. My select2 looks like:

const productsSelect = $("#benefitProductsSelect")

        productsSelect.select2({
            minimumInputLength: 4,
            multiple: true,
            ajax: {
                url: "/admin/Products/getAllSelect2",
                dataType: "json",
                processResults: function (data) {
                    return { results: data.items }
                }
            }
        })

E.g. something liek selectedValues option. Is there such opportunity?

question from:https://stackoverflow.com/questions/65830324/how-to-set-selected-values-in-ajax-multi-select2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...