how to filter a extjs store with an exact match

你说的曾经没有我的故事 提交于 2019-12-05 01:59:09

问题


I am using a filter for a store. The problem is that I want to return an exact match.

For example:

If I am filtering for aa-1 in a grid it will show aa-1 and aa-1*** but if I want only see everything with aa-1.

I use this to filter:

listeners: {
    itemclick: function() {
        var data = grid.getSelectionModel().selected.items[0].data;
        store.clearFilter();
        store.filter('productsCat', data.productsCat);
    }
}

What do I have to do to do an exact match?


回答1:


You could use a regular expression in the filter to perhaps ensure that the comparison value was the end of the phrase.

Either that, or use the filterBy() method to define a comparison function, e.g.:

store.filterBy(this, function(rec, id) {
    if(rec.get('thefieldtocompare') === "what you want to compare against") {
        return true;
    }
    else {
        return false;
    }
});



回答2:


For ExtJS 4, you can simply use exactMatch and caseSensitive config options:

store.filter({
  property: fieldName,
  value: fieldValue,
  exactMatch: true,
  caseSensitive: true
})



回答3:


You didn't mention what version of ExtJS you are using but here are two general approaches:

  1. Provide a regexp with exact mathing pattern:

    store.filter('productsCat', new RegExp('^' + Ext.escapeRe(data.productsCat) + '$'));
    

    or

  2. Provide your own matching function to filterBy

    store.filterBy(function(rec) {
        return rec.get('productsCat') === data.productsCat;
    });
    


来源:https://stackoverflow.com/questions/11984663/how-to-filter-a-extjs-store-with-an-exact-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!