Select2 options disappear when selecting a

Asked

Viewed 268 times

0

I am using Select2 to load data remotely and it is bringing the results normally, but when selecting one of the returned options all existing ones disappear and I could not identify why, follow the code:

Javascript

$('.js-data-example-ajax').select2({
    ajax: {
        url: "/Home/PopularUsuarios",
        dataType: 'json',
        type: "post",
        delay: 250,
        theme: 'clasic',
        data: function (params) {
            return {
                objPesquisa: params.term, // search term                              
            };
        },

        processResults: function (data, params) {
            params.page = params.page || 1;
            data.forEach(function (d, i) { d.id = i; })

            return {
                results: data,
                pagination: {
                    more: (params.page * 30) < data.total_count
                }
            };
        },
        cache: true
    },
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
    minimumInputLength: 1,
    templateResult: function (data) {
        if (data.loading) return data.text;
        var markup = '<option value="' + data.Matricula + '">' + data.Nome + '</option>'
        return markup;
    },
    templateSelection: function (data) {
        return data.full_name || data.text;
    }
});
  • Code http://www.codepen.io or https://jsfiddle.net so that we can better understand what you are doing.

  • In one of the parts of the register of activities of the system I want to create a field in which will allow me to select the user associated with such activity, for this I used Select2 that allows me to type the name of the employee, calls the function that creates an employee list and returns a JSON and displays the employee name, the list is loading smoothly, it can return the object also correctly, but at the time I click one of the objects (Assuming there are 3 Milestones in the company) all the already listed ones disappear instead of the drop down "collect" and stay with the selected clicked.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.