今天遇到了一个勾选GridPanel里面某行总是不对的问题,研究了store的数据结构发现没什么问题,猜测是不是Ext的代码出bug了?看源代码分析了一番……
Ext.data.Store.find的匹配模式,使用的是test方法,源代码片段是
value.test(r.data[property]);
也就是说,store.find(‘find’, ‘a’);
如果store里面有两行record,一行的find列是’a’, 一行的find列是’ab’,那么会返回先匹配到的那一行,而不是你想要的’a’,如果你想用find方法获得你给定的值相等的行,那就错了
如果要用精确匹配,使用Ext.data.Store.findExact()方法
这个方法是这样实现的
findExact: function(property, value, start){
return this.data.findIndexBy(function(rec){
return rec.get(property)?===?value;
}, this, start);
},
注意这里面的匹配使用的是恒等===,?所以你要注意你store的fields配置了,如果你配置了int,那么用数字字符串去匹配的返回值永远是-1,这时先做个parseInt处理一下你的数字字符串
在Ext官网的手册里面,上面提到的两个函数的说明,都是下面这句,
Finds the index of the first matching Record in this store by a specific field value.
Finds the index of the first matching Record in this store by a specific field value.
这里Ext可能疏忽了