Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear search term and reload all items of Select2 input after selecting any item

I have a select2 Input text field in my page in which I am loading items dynamically on focus event of Input field.

When I enter some search text let's say 'vai' then the result items are loaded as 'vaibhav1', 'vaibhav2', 'vaibhav3', 'vaibhav4' with focus on first item. If I press enter key it gets selected.

Now what I want is to clear the search term 'vai' and the items list should be refreshed displayed containing all remaining records including last searched items. Below is my javascript code under $(document).ready(function) for the select2 input field.

function format(item) { return item.name; };
// select2 multiple select feature for Impact Area initialization
var test = $('#impactArea'); 
$(test).select2({
    formatSelection: format,
    formatResult: format ,
    multiple: true,
    width: "612px",
    height: "50px",
    closeOnSelect : false,
    maximumSelectionSize : 20,
    ajax: { 
        type: "GET",
        url: "/progressManagement/testingIssue/impactAreaList",
        dataType: 'json',
        data: function (term) {
          return { q: term };
       },
        results: function (data) {
          return {results: data};
        }
       } ,
       initSelection: function(element, callback) { 
           var rangeList=$('#impactArea').val();
           var id=$(element).val();
            if (id!=="") {
            $.ajax("/progressManagement/testingIssue/selectedImpactArea", {
                data: { rangeList: rangeList },
                dataType: "json"
            }).done(function(data) { 
                callback(data); 
            });
            }
        } 
});

I tried searching in the select2js file to find a function or a flag which could be used for this, please help me if you guys have any ideas about this. Let me know if I am not clear in specifying my requirements or approach. Thanks in advance :)

like image 923
VPK Avatar asked Jan 15 '15 10:01

VPK


People also ask

How do I reset Select2 options?

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();

How do I update Select2 options?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data.text, data.id, false, false); $('#mySelect2').append(newOption).trigger('change');

How do you add select all in Select2?

Select2 also supports multiple selection option. All you have to do is to declare the multiple attribute to the input. This will add the selected options as pills to the text box at the top of the control.

How does Select2 search work?

When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the matcher configuration option.


2 Answers

Somehow I found a workaround, I have cleared the search string using the $(test).select2("search", ""); command whenever the Input field value changes.

$(test).change(function() {
    $(test).select2("search", "");
});

Here, search is the search term variable used in select2.js file which I set to blank string every time user selects or deselects the item in the list. It is working fine for my requirement.

Please let me know if anyone finds a better solution, thanks.

like image 112
VPK Avatar answered Nov 08 '22 22:11

VPK


In v4, I was able to clear the search field with the following code.

$(test).change(function() {
  var searchfield = $(test).parent().find('.select2-search__field');
  searchfield.val("");
})
like image 23
Enoch Avatar answered Nov 08 '22 21:11

Enoch