如何在 ExtJs 商店中使用过滤器? [英] how to use filter in ExtJs store?

查看:23
本文介绍了如何在 ExtJs 商店中使用过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ExtJs 创建了一个商店,我想将 store 的值加载到 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 条记录 (班加罗尔、迈索尔、达瓦德) 加载到组合框中,如果 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 filter 方法文档:

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:

  1. 手动过滤数据(参见Ext.util.MixedCollection 过滤器)并将其加载到您的商店中(参见 Ext.data.Store 加载)

  1. Filter data manually (see Ext.util.MixedCollection filter) and load it into your store (see Ext.data.Store load)

禁用商店的 clearFilterfilter 方法并使用自己的 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() 方法.

这篇关于如何在 ExtJs 商店中使用过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆