在日常开发中,总是断断续续的开发各种各样的程序,前几天还在熬夜写着服务器端开发,今天又开始修改之前的管理系统界面,界面大部分均采用extjs构建。

在采用store的filter方法实现过程中,发现过滤之后的结果总是比预想的到多几个,最后发现如下bug。

Ext.data.store.filter() 在说明文档中有如下三种用法:


[filters], [value] )

Filters the loaded set of records by a given set of filters.

By default, the passed filter(s) are added to the collection of filters being used to filter this Store.

To remove existing filters before applying a new set of filters use

// Clear the filter collection without updating the UI
store.clearFilter(true);

see clearFilter.

Alternatively, if filters are configured with an id, then existing filters store may be replaced by new filters having the same id.

Filtering by single field:

store.filter("email", /\.com$/);

Using multiple filters:

store.filter([
    {property: "email", value: /\.com$/},
    {filterFn: function(item) { return item.get("age") > 10; }}
]);

Using Ext.util.Filter instances instead of config objects (note that we need to specify the root config option in this case):

store.filter([
    Ext.create('Ext.util.Filter', {property: "email", value: /\.com$/, root: 'data'}),
    Ext.create('Ext.util.Filter', {filterFn: function(item) { return item.get("age") > 10; }, root: 'data'})
]);

但是实际使用过程中发现第一种使用方法可能没有对field类型进行判断,默认全部按照字符进行比对,这样就导致我在比对 int值为3的两个obj时,

默认全部转换为字符串,所以结果中出现了  ('30' == 3) 为true的结果,而我真实的想法是找到与3值相当的record,但是实际把只要是3开头的结果都认定为true,导致和预想结果不同。

具体原因暂时未知,随后花时间仔细看下源码。

最后采用第二,第三种方法解决该问题。

最后感叹下,ExtJS中combo采用store的filter方法来实现多级联动的确要比store.reload效率高很多,代码也更简介一些。

发现疑似ExtJS4.2.1 bug – Ext.data.store.filter();
Tagged on:

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据