Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use filter in ExtJs store?

Tags:

extjs

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.

Model:-

Ext.define('City', {
extend: 'Ext.data.Model',
fields: [
        { name: 'StateId', type: 'string' },
        { name: 'City', type: 'string' },
]});

Store:-

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'}
   ]});

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.

like image 304
CodeGuru Avatar asked Sep 27 '12 10:09

CodeGuru


1 Answers

According to Ext.data.Store filter method documentation:

var stateId = 1; // your value
cityStore.clearFilter(true);
cityStore.filter('StateId', stateId);

Update

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. Filter data manually (see Ext.util.MixedCollection filter) and load it into your store (see Ext.data.Store load)

  2. 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);
        }
    });
    

    Then use cityFilter() method.

like image 52
Alexey Solonets Avatar answered Oct 18 '22 20:10

Alexey Solonets