2017-09-04 80 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}; 
      } 
     }