【问题标题】:Results not displaying using ajax-select2 e使用 ajax-select2 e 不显示结果
【发布时间】:2015-12-16 05:19:52
【问题描述】:

我已经编写了一个代码来使用 select2 和 ajax 显示相关的 serch 结果。结果正确显示在控制台中,但未显示在 select2 的结果表中。它只是说明没有找到结果...下面是我的代码:

<script type="text/javascript">

   $(".showname").select2({        
    ajax: {
        url:"http://hub.w.net/datatables/brand_processing",
        //dataType: "json",
        //delay: 250,
        type:'POST',
        data: function (params) {
            return {
                search_name: params.term // search term

               //console.log(params:params);
            };
        },
        processResults: function (data) {
            // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to
            // alter the remote JSON data
            console.log(data.data);
            return {
                 data:{text:data.data,id:data.id}
            };
        },

        cache: true
    },
    minimumInputLength: 1

}); 
</script>

有什么建议吗?

【问题讨论】:

  • 你在控制台得到了什么?

标签: ajax jquery-select2


【解决方案1】:

试试这个 select2 最新版本

processResults: function (data, params) {

               var results = [];

                if (data != null && data.length > 0) {

                    $.each(data, function (index, item) {

                        results.push({
                            id: item.id,
                            text: item.text
                        });
                    });
                }
                return {
                    results: results
                };
};

对于 select2 3.5.2 版本:

results: function (data, page) { // parse the results into the format expected by Select2.
                var results = [];

                if (data != null && data.length > 0) {

                    $.each(data, function (index, item) {

                        results.push({
                            id: item.id,
                            text: item.text
                        });
                    });
                }
                return {
                    results: results
                };
            },
            cache: true
        }

【讨论】:

    【解决方案2】:

    processResults 函数上试试这个

    return {
        text:data.data,
        id:data.id
       };
    

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2013-06-12
      • 2016-09-11
      • 2017-08-16
      • 2015-10-15
      • 1970-01-01
      • 2014-07-26
      • 2018-11-22
      • 2016-04-12
      相关资源
      最近更新 更多