简体   繁体   English

Select2 以编程方式设置搜索词并打开带有相关结果的下拉菜单(无 ajax 调用)

[英]Select2 Programmatically set search terms and open dropdown with relevant results (no ajax calls)

I'm working on removing ajax calls from select2.我正在努力从 select2 中删除 ajax 调用。 I have a static list of objects populating the dropdown.我有一个填充下拉列表的对象的静态列表。 The workflow I'm looking at is...我正在查看的工作流程是...

  1. User clicks the select2用户单击 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.但是我不想篡改静态列表中的可用选项。

IE IE

  1. User types in "ABC", and they have a list of "ABC Company" "ABC Person" and "ABC Entity" show up to choose from.用户在“ABC”中输入,他们有一个“ABC公司”“ABC人员”和“ABC实体”列表可供选择。

  2. User chooses "ABC Person" so they're taken to the ABC Person page.用户选择“ABC Person”,因此他们被带到 ABC Person 页面。

  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").用户再次点击搜索框,select2框的搜索词区域出现“ABC”,并自动显示“ABC公司”“ABC人员”和“ABC实体”的列表(就像他们刚刚搜索了“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.用户可以退格并输入“XYZ”,然后显示“XYZ 公司”“XYZ 人”“XYZ 实体”的新列表,而无需进行任何 ajax 调用。

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.目前我可以以编程方式设置搜索词值,但我不知道如何触发 .select2-input 框上的更改事件以打开和加载结果。

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") 但是在我的代码中我们需要设置option的值(例如:“B1”)而不是搜索的术语(例如:“ABC”)

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

Got it: :) Updated fiddle: http://jsfiddle.net/abhiklpm/98mhzv5b/1/ 知道了::)更新了小提琴: http//jsfiddle.net/abhiklpm/98mhzv5b/1/

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
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM