PDA

View Full Version : RegExp for filtering?



alejandroNNU
22 Nov 2013, 12:45 PM
I'm using the .filter method on a store to display certain results on a grid, for example:


store.filter('id', '1206');

works great and the grid will display only the record with ID 1206, however, what if I want to show all records with an id that start with 12, and the other two characters can be anything? I tried using a regexp but I'm not sure if it doesn't work here or if I'm just doing it wrong, I used:


store.filter('id', '/^12..$/');

what am I doing wrong and how can I achieve this? Any help would be great.

scottmartin
22 Nov 2013, 9:42 PM
Try something like this:



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

Ext.onReady(function() {

var stateStore = Ext.create('Ext.data.Store', {
autoDestroy: true,
model: 'State',
data: [
{"abbr":"AL","name":"AL - Alabama","slogan":"The Heart of Dixie"},
{"abbr":"AK","name":"AK - Alaska","slogan":"The Land of the Midnight Sun"},
{"abbr":"AZ","name":"AZ - Arizona","slogan":"The Grand Canyon State"}

]
});

Ext.create('Ext.Button', {
text: 'Click me',
renderTo: Ext.getBody(),
handler: function() {
console.log(stateStore.count());

stateStore.filterBy(function(r) {
var re = new RegExp('Alaska', 'i');
return re.test(r.get('name'));
});

console.log(stateStore.count());

}
});

});


Scott.

skirtle
23 Nov 2013, 4:00 AM
store.filter('id', '/^12..$/');

That's not a RegExp, it's a string. Try:


store.filter('id', /^12..$/);