json - select2 使用查询词过滤本地 JSON 数据结果

标签 json filter local jquery-select2

我正在实现 select2 版本 3.5.0。我在我的文档就绪函数中使用了以下 jQuery:

jQuery.getJSON('/rest/call/to/retrieve/JSON').done(
  function( data ) {
     jQuery('#byProductName').select2({
         placeholder: 'Type any portion of a single product name...',
         allowClear: true,
         minimumInputLength: 0,
         multiple: true,
         id: function(data){ return data.product; },
         data: data,
         formatResult: function(data){ return data.product; },
         formatSelection: function(data){ return data.product; },
     });
  }
);

HTML 隐藏输入元素:
<div id='freeForm'>
    <input name='Search Products' type='hidden' id='byProductName'>
</div>

JSON 结果:
[{"product":""},{"product":" windows"},{"product":" mac"},
 {"product":" linux"},{"product":" RHEL"},{"product":"Test product list"}]

下拉列表正确填充了我的值,我可以选择多个项目并成功删除它们。但是,当我在输入字段中键入字符串以过滤结果集时,它不会进行过滤。

我尝试将数据更改为:
data: function (data, term){
    return { 
        results: data,
        query: term }
    },

但是一旦我点击下拉菜单就会出现这个错误:
Uncaught TypeError: Cannot read property 'length' of undefined - select2 line 1784

如何使用查询词成功过滤结果列表?

最佳答案

来自 Select2 documentationdata选项:

Options [sic] for the built in query function that works with arrays.

If this element contains an array, each element in the array must contain id and text keys.

Alternatively, this element can be specified as an object in which results key must contain the data as an array and a text key can either be the name of the key in data items that contains text or a function that retrieves the text given a data element from the array.



这意味着您有两个选择:

(1) 更改您的 data数组是具有 id 的对象数组和 text在将其提供给 .select2() 之前的属性.然后你可以摆脱 id , formatResultformatSelection选项。
jQuery.getJSON('/rest/call/to/retrieve/JSON').done(
    function( data ) {

        data = $.map(data, function(item) {
            return { id: item.product, text: item.product }; 
        });

        jQuery('#byProductName').select2({
            placeholder: 'Type any portion of a single product name...',
            allowClear: true,
            minimumInputLength: 0,
            multiple: true,
            data: data
        });
    }
);

jsfiddle

(2) 提供对象 resultstext data 的属性选项。在这种情况下,您仍然需要提供 id选项,但您可以去掉 formatResultformatSelection选项。
jQuery.getJSON('/rest/call/to/retrieve/JSON').done(
    function( data ) {
        jQuery('#byProductName').select2({
            placeholder: 'Type any portion of a single product name...',
            allowClear: true,
            minimumInputLength: 0,
            multiple: true,
            data: { results: data, text: 'product' },
            id: function(item) { return item.product; }
        });
    }
);

jsfiddle

关于json - select2 使用查询词过滤本地 JSON 数据结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28196932/

相关文章:

javascript - angularjs过滤器过滤器在 Controller /工厂中的顺序

json - jq:根据键是否以指定字符串结尾过滤输入

Android PhoneGap OCR 插件

json - 如何通过jq将json文件中的星号解析为字符串

javascript - 导入 json 文件以在 vis.js 中创建网络

php - 在 Yii2 中获取 JSON 格式的响应

javascript - 将字符串数组映射到 JavaScript 中的 json 对象

java - 使用FilenameFilter过滤java中文件夹及其子文件夹中的所有文件

iOS 本地推送通知覆盖静音/静默模式

python - GAE 开发服务器 memcached 永远不会返回任何内容