如何在ExtJs中使用过滤器?
我使用ExtJ创建了一个商店,我想将商店的值加载到ComboBox。但是在加载值之前,我需要根据另一个组合框中选择的值来过滤一些数据。
I created a store using ExtJs and i want to load the value of store to ComboBox. But before loading values i need to filter some data based on value selected in another comboBox.
所以为了这个目的,我想我需要在商店上应用过滤器,请任何身体可以帮助我如何做到这一点。
So for that purpose i think i need to apply filter on store, please any body can help me how i can do that.
型号: -
Ext.define('City', {
extend: 'Ext.data.Model',
fields: [
{ name: 'StateId', type: 'string' },
{ name: 'City', type: 'string' },
]});
商店: -
var cityStore = Ext.create('Ext.data.Store', {
model: 'City',
data : [
{ StateId: '1', City: 'Bangalore'},
{ StateId: '1', City: 'Mysore'},
{ StateId: '1', City: 'Dharwad'},
{ StateId: '2', City: 'Mumbai'},
{ StateId: '2', City: 'Pune'},
{ StateId: '2', City: 'Nagpur'}
]});
现在我使用这个 cityStore 加载到Combobox中。但是在加载之前,如果 stateId为1 ,则只有3个记录(班加罗尔,迈索尔,Dharwad)加载到组合框中,如果 stateId为2 那么其他3条记录将加载到组合框中。
Now i am using this cityStore to load in Combobox. but before load i want if stateId is 1 then only 3 records (Bangalore, Mysore, Dharwad) are load in combobox and if stateId is 2 then other 3 records are load in combobox. How i can achive it.
根据 Ext.data.Store过滤器方法文档:
var stateId = 1; // your value
cityStore.clearFilter(true);
cityStore.filter('StateId', stateId);
更新
我发现ComboBox本身过滤数据,没有机会改变它的行为。但是我看到这个问题的两个解决方案:
I've found that ComboBox filters data by itself and there is no opportunity to change it's behaviour. But I see two solutions of this problem:
-
手动过滤数据(参见 Ext.util.MixedCollection过滤器)并将其加载到您的商店(请参阅 Ext.data.Store加载)
Filter data manually (see Ext.util.MixedCollection filter) and load it into your store (see Ext.data.Store load)
禁用商店的 clearFilter
和过滤器
方法,并使用自己的 cityFilter
:
Disable store's clearFilter
and filter
methods and use own cityFilter
:
Ext.define('CityStore', {
extend: 'Ext.data.Store',
filter: Ext.emptyFn,
clearFilter: Ext.emptyFn,
cityFilter: function (stateId) {
Ext.data.Store.prototype.clearFilter.call(this);
Ext.data.Store.prototype.filter.call(this, 'StateId', stateId);
}
});
然后使用 cityFilter()
/ p>
Then use cityFilter()
method.