PDA

View Full Version : Using filter method from Data Store



andre_mendesc
8 Oct 2012, 8:07 AM
Good morning everyone!


I have this piece of code to filter some json results retrieved from a database


{
xtype: 'button',
text: 'Pesquisar',
renderTo: 'alunos',
handler: function(){
load.clearFilter();
load.filter('name', Ext.getCmp("nomeAluno").getValue() , true, false );
}
}



I use this in a search field. The problem?
Let's say I want to find everyone named 'Bruno' on my database, I type 'Bruno' on the textfield, click on search and I will get a lot of Brunos


Bruno Miranda
Bruno Fonseca
Etc...


BUT if I search for Miranda, it won't return the first Bruno. It only looks up the first word of every record, but I'd like it to match "Bruno Miranda" too, if I search for "Miranda"

Does anyone have a suggestion?
Thanks in advance!

loiane
8 Oct 2012, 11:05 AM
Hi,

First of all, do not forget to set the remoteFilter config in your store to true.

Second, to list all the matches from the database depends on how you are hadling the search on the server side; in your sql query.

If you are doing
WHERE name = 'Bruno'

it is only going to retrieve the Brunos.

If you use
WHERE name LIKE '%Miranda%'

it is going to retrieve all the records that have 'Miranda' in it.

Hope it helped!

andre_mendesc
8 Oct 2012, 12:35 PM
Thanks for the reply!
I think I didn't explain myself properly.


This application consist in a system to record students frequency, I retrieve all students from database once, so the operator will lookup for the students among that set of data. I think this approach is better because I want to avoid doing lots of querys, I think it should be enough to just filter the records each time the operator search for a student name (this way is super fast!).


So, is it possible to do this way? If I had to do remote querys all the time, it would be daam slow..


Thanks Loiane (I'm a big fan of your work :)) )

loiane
8 Oct 2012, 1:09 PM
Thanks!

I got you now!

The filter method from Store accepts an object called Ext.util.Filter.

So you can do like this:


store.filter([Ext.create.('Ext.util.Filter', {property: 'name', value: 'some value', anyMatch: true})]);

And with the config anyMatch, it will return what you are looking for. :)

andre_mendesc
9 Oct 2012, 11:06 AM
Thanks but it still not working, I got a blank response as result of using this


handler: function(){ load.clearFilter(); load.filter([Ext.create('Ext.util.Filter', {property: 'name', value: Ext.getCmp("nomeAluno").getValue(), anyMatch: true})]); } }


and this way, filter doesn't work at all.

I'm still trying, but if someone find out a solution, I'd be grateful to see!

andre_mendesc
9 Oct 2012, 12:34 PM
Just got it working!



handler: function(){
load.clearFilter();
load.filter([
{
property: 'name',
value: Ext.getCmp('nomeAluno').getValue(),
anyMatch: true,
caseSensitive: false
}]);
}
}


I don't know why it doesn't work with Ext.create inside but it's working very well now.
Thanks for the help.

rupamkhaitan
2 Apr 2013, 5:43 AM
I have a datagrid and a textfield on top of grid

If i type something in textbox it should basically match in all columns and display the result.
Right now I am using the below code by using filterby method to filter a record based on single column

var searchvalue = text.getValue();
this.getGridPanel().store.filterBy(function(record){
var name= record.data.firstname;
if(name.indexOf(value.toLowerCase()) == 0){
return true;
}
else{
return false;
}
})


Please help me as my grid has many columns and i want to search in all the columns and return if any1 is matched
I saw the above approach of filter method, but can you post some code on how to apply to my code via using filter method.

rupamkhaitan
2 Apr 2013, 6:32 AM
One way to my above code is i write multiple if condition



if(record.data.firstName == searchValue || record.data.lastName==searchValue)
return true;
else
return false;

But I want a generic search where I use the same thing for different grid and it will take care of finding the column name and return the filtered record