Populate select2 dynamically

315 views Asked by At

I have a select2 component that works well with ajax

<select id="select_lpr" style="width:100%" multiple="multiple" data-placeholder="Link a project"></select>

and here is how I pull data while searching

$("#select_lpr").select2({
    minimumInputLength: 2,
    maximumSelectionLength: 5,
    ajax: {
        type  : 'POST',
        url: '../../model/prj.php',
        dataType: 'json',
        delay: 250,
        data: function (term, page) {
            return {
                wildcardsearch: term, // search term
                data_limit: 10,
                data_offset: 0
            };
        },
        processResults: function (data, page) {
            return { results: data.dataset};
        },
        cache: true
    },
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
});

Now I need to ensure the select is populated on the page load. I am struggling to understand how this can be achieved. I use the latest version of select2 (Version 4).

0

There are 0 answers