搜索多个项目sencha touch

时间:2013-03-07 09:52:15

标签: search sencha-touch-2 multiple-columns

我需要搜索多个项目。到目前为止,我只知道如何搜索单项

这是我的代码

Controller.js

onSearchKeyUp: function (field) {
  //get the store and the value of the field
  var value = field.getValue(),
      store = Ext.getCmp('transactionlist').getStore();    

  //first clear any current filters on thes tore
  store.clearFilter();

  //check if a value is set first, as if it isnt we dont have to do anything
  if (value) {
    //the user could have entered spaces, so we must split them so we can loop through them all
    var searches = value.split(' '),
    regexps = [],
    i;

    //loop them all
    for (i = 0; i < searches.length; i++) {
      //if it is nothing, continue
      if (!searches[i]) continue;

      //if found, create a new regular expression which is case insenstive
      regexps.push(new RegExp(searches[i], 'i'));
    }

    //now filter the store by passing a method
    //the passed method will be called for each record in the store
    store.filter(function (record) {
      var matched = [];

      //loop through each of the regular expressions
      for (i = 0; i < regexps.length; i++) {
        var search = regexps[i],
        didMatch = record.get('transactionId').match(search) ;

        //if it matched the first or last name, push it into the matches array
        matched.push(didMatch);
      }

      //if nothing was found, return false (dont so in the store)
      if (regexps.length > 1 && matched.indexOf(false) != -1) {
        return false;
      } else {
        //else true true (show in the store)
        return matched[0];
      }
    });
  }
},

请指导我如何搜索多个项目。感谢

2 个答案:

答案 0 :(得分:0)

当你的正则表达式的任何失败时,你似乎正在返回false。如果我正确理解了您的问题,您希望捕获任何输入值,因此只有在所有失败时才会返回false

尝试更改:

  if (regexps.length > 1 && matched.indexOf(false) != -1) {
    return false;

为:

  if (regexps.length > 1 && matched.indexOf(true) != -1) {
    return false;

看看是否有帮助。

答案 1 :(得分:0)

didMatch = record.get('transactionId').match(search) || record.get('transactionName').match(search);