2017-09-04 139 views
0

使用Select2的搜索默认值,我看到它运行与我键入的字符的查询条件的ajax调用。它不会过滤当前的项目列表。我没有看到文档中的任何内容来控制此行为。有没有可能改变它?Select2搜索项目的当前列表

回答

0

对于ajax'd数据,Select2没有选项来筛选当前缓存的结果集或新获取的集合。它总是得到一个新的数据集。

我的解决方案是在新数据上编写我自己的过滤器。它可以工作,但对于每个搜索词,它都会创建一个新的ajax调用,每次都返回相同的数据。

processResults: function (data, query) { 
      let normalizedData = hj.gic.swapFieldDataConditioner('name', 'text', data.results); 

      if(query.term === undefined) { 
       return {results: normalizedData}; 
      } else { 
       let term = new RegExp(query.term, 'gi'), matchedResults = []; 
       normalizedData.forEach(function (item) { 
        if(item.text.match(term)) { 
         matchedResults.push(item); 
        } 
       }); 
       return {results: matchedResults}; 
      } 
     }