4

I'm working on removing ajax calls from select2. I have a static list of objects populating the dropdown. The workflow I'm looking at is...

  1. User clicks the select2
  2. User types into the search box (and results are displayed)
  3. User makes a selection and a new webpage loads.

At this point, I want to prepopulate the search term with what the user initially searched for, so that the user can just click the box and have it prepopulated with the results list that they selected from to get to this page. However I do not want to tamper with the available options in the static list.

I.E.

  1. User types in "ABC", and they have a list of "ABC Company" "ABC Person" and "ABC Entity" show up to choose from.

  2. User chooses "ABC Person" so they're taken to the ABC Person page.

  3. User sees that this is not the page they were looking for.

  4. User clicks the search box again, and the select2 box has "ABC" in the search term area and a list of "ABC Company" "ABC Person" and "ABC Entity" automatically displayed (as if they just searched for "ABC").

User can backspace and type in "XYZ" and a new list of "XYZ Company" "XYZ Person" "XYZ Entity" shows up without making any ajax calls.

Currently I can programmatically set the search term value, but I cannot figure out how to trigger the change event on the .select2-input box to get the results to open up and load.

Bardicer
  • 1,405
  • 4
  • 26
  • 43

2 Answers2

10

Inorder to populate the select box and trigger we need this:

<select id="e1" style="width:300px;">
    <optgroup label="A1 comps">
       <option value="A1">AAA 1</option>
       <option value="A2">AAA 2</option>
       <option value="A#">AAA 3</option>
       <option value="A4">AAA 4</option>
   </optgroup>
   <optgroup label="B1 comps">
       <option value="B1">BBB 1</option>
       <option value="B2">BBB 2</option>
       <option value="B3">BBB 3</option>
       <option value="B4">BBB 4</option>
   </optgroup>
</select>

<script>
 $(document).ready(function() { 
    $("#e1").val("B1").select2();
    $("#e1").on("select2-opening", function() { 
       $("#e1").on("select2-open", function() { 
         $("#e1").select2("search","BBB");
       });
    });
 });
</script>

But in my code we need to set the value of option (in eg: "B1") and not the searched term (eg :"ABC")

http://jsfiddle.net/abhiklpm/98mhzv5b/

Got it: :) Updated fiddle: http://jsfiddle.net/abhiklpm/98mhzv5b/1/

abhiklpm
  • 1,603
  • 2
  • 16
  • 21
  • I can set the selected value, but I don't have a clear breakpoint for selections as you are doing in the fiddle. What I'm needing is to be able to trigger a 'search update' via javascript/jquery after I set the value. Setting the value programmatically doesn't fire the change event, and doing something like $(".select2-input").trigger("change"); doesn't work. If someone searches for "ABC" I need to be able to set "ABC" as the search term, open the dropdown, and show the appropriate subset from the select options, all programmatically. I have been able to do everything except the subset. – Bardicer Jan 12 '15 at 19:06
  • 1
    or this : $("#e1").select2().select2("search","BBB"); solution was simple but took some time to findout, was in documentation under "search" http://select2.github.io/select2/#documentation – abhiklpm Jan 12 '15 at 20:47
  • That is exactly what I was looking for. I guess I just didn't read far enough down to get that. Thanks! – Bardicer Jan 12 '15 at 21:57
  • 3
    For anyone using select2 version 4.0.0 the events are now `select2:opening` and `select2:open`. And the 'search' is no longer available. A solution in version 4 can be found at http://stackoverflow.com/questions/30517128/how-to-programmatically-inject-search-queries-into-select2-v4 – Sach Aug 21 '15 at 00:39
0

this work for me

<select id="my_select2" data-value="2612"></select>
$input = $('#my_select2')
$.ajax({
    type: 'GET',
    url: url,
    data: {
        term: $input.data('value')
    },
    dataType: 'json',
    cache: false,
    success: function (data) {
        Object.keys(data).forEach(function (index){
            $input.html(`<option value='${data[index].id}'>${data[index].name}</option>`)
        })


        $input.select2({
            width: '100%',
            dir: "rtl",
            ajax: {
                url: url,
                dataType: 'json',
                type: "GET",
                data: function (term) {
                    return term;
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.name,
                                id: item.id
                            }
                        })
                    };
                }
            },
            escapeMarkup: function (markup) {
                return markup;
            },
            minimumInputLength: 1
        });
    }
});
Hossein Piri
  • 716
  • 1
  • 7
  • 15