简体   繁体   English

select2自动选择ajax调用的项目

[英]select2 automatically select item for ajax call

Is there a way to make select2 control auto select an item when ajax response contain extra data.当 ajax 响应包含额外数据时,有没有办法让 select2 控件自动选择一个项目。

I want to implement my controller to in JsonResult mark item as exact mach and then to select2 control automatic select that without opening drop n down.我想实现我的控制器以在 JsonResult 中将项目标记为精确马赫,然后选择 2 控制自动选择它而不打开下拉菜单。

From user prespecitve: If user type in select2 textbox string that exactly match item on controller.从用户 prespecitve:如果用户输入与控制器上的项目完全匹配的 select2 文本框字符串。 eg If user type in barcode and controller method find that bar-code.例如,如果用户输入条形码和控制器方法,则找到该条形码。 Select2 control will immediately select that item without opening dropdown. Select2 控件将立即选择该项目,而无需打开下拉菜单。

If user type in query that is not exactly match controller will return list of items without param exact and select2 will open dropdown to show user possible choice of items.如果用户输入不完全匹配的查询,控制器将返回不带参数的项目列表, select2 将打开下拉菜单以显示用户可能选择的项目。

"

To do this with AJAX, you need to add a selected option to the select DOM element, and then trigger a change on the select2 widget so it redraws. 要使用AJAX做到这一点,您需要向select DOM元素中添加一个选定的选项,然后在select2小部件上触发更改,以便其重新绘制。 Below may be what you are looking for. 以下可能是您想要的。 This example uses processResults to check to see if there is exactly one match and it matches what the user typed exactly. 本示例使用processResults来检查是否存在完全匹配的一项,并且该匹配项与用户键入的内容完全匹配。

$("#product_id").select2({
  ajax: {
    url: "/api/productLookup",
    dataType: 'json',
    data: function (params) {
      return {
        term: params.term,
        };
    },
    processResults: function (data) {
        var searchTerm = $("#product_id").data("select2").$dropdown.find("input").val();
        if (data.results.length == 1 && data.results[0].text == searchTerm) {
            $("#product_id").append($("<option />")
                .attr("value", data.results[0].id)
                .html(data.results[0].text)
            ).val(data.results[0].id).trigger("change").select2("close");
        }
        return data;
    },
    minimumInputLength: 8,
    cache: true
  }
});
#this worked for me... using select2 with barcode
var defaultInitialGroup = '';
    $("#idbarang").select2({
        placeholder: "Type/ Scan your barcode item",
        allowClear: true,
        minimumInputLength: 2,
        multiple: true,
        ajax: {
            url: HOST_URL + 'stock/balance/list_xxv',
            type: 'POST',
            dataType: 'json',
            delay: 250,
            data: function(params) {
            return {
            _search_: params.term, // search term
            _page_: params.page,
            _draw_: true,
            _start_: 1,
            _perpage_: 2,
            _paramglobal_: defaultInitialGroup,
            term: params.term,
            };
            },
            processResults: function (data, params) {
            var searchTerm = $("#idbarang").data("select2").$dropdown.find("input").val();
        if (data.items.length === 1 && data.items[0].text === searchTerm) {
        var option = new Option(data.items[0].nmbarang, data.items[0].idbarang, true, true);
        $('#idbarang').append(option).trigger('change').select2("close");
        // manually trigger the `select2:select` event
         $('#idbarang').trigger({
         type: 'select2:select',
         params: {
            data: data
        }
        });}
        params.page = params.page || 1;
        return {
        results: data.items,
            pagination: {
            more: (params.page * 30) < data.total_count
            }
            };
            },
            cache: false
        },
        escapeMarkup: function(markup) {
        return markup;
        }, // let our custom formatter work
        templateResult: formatItem, // omitted for brevity, see the source of this page
        templateSelection: formatItemSelection // omitted for brevity, see the source of this page
    })
function formatItem(repo) {
if (repo.loading) return repo.text;
var markup ="<div class='select2-result-repository__description'>" + repo.idbarang +"<i class='fa fa-circle-o'></i>"+ repo.nmbarang +"</div>";
return markup;
}
function formatItemSelection(repo){
return repo.nmbarang || repo.text;
}

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

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