PDA

View Full Version : MVC - Trouble setting store sorters dynamically



robertb216
22 Mar 2012, 5:08 PM
I am storing a grid's display configuration (filters, sort, page, limit,etc) in a database. When the user returns to the grid (hours, days later), I want to set the grid/data to the last viewed status. I have successfully set the filters using:

grid.filters.addFilter(
{
dataIndex: records[i].get('id'),
type: records[i].get('type'),
value: records[i].get('value'),
}
);

Great, however, I am attempting to modify the store configuration with the last sort configuration and keep getting an error. It works correctly when I use this command:

grid.getStore().sorters.add(new Ext.util.Sorter({property:'name',direction:'DESC'} ));

The problem is that I don't want a "hard-coded" sorter definition. I and getting that information from another store and attempting to use the results to create a new sorter.

var myFilterStoreResponse = Ext.JSON.decode(operation.response.responseText);
grid.getStore().sorters.add(new Ext.util.Sorter(myFilterStoreResponse.sorters));

where JSON response looks like this:
{'success':true, 'sorters': "{property:'name',direction:'DESC'}", 'filters':[ { 'id':'product_id', 'type':'string', 'value':'xyz'} ] }

where console.log(myFilterStoreResponse.sorters) results in:
{property:'name',direction:'DESC'}

Using console.log(grid.getStore().sorters); returns a constructor with an undefined key.

Hopefully I explained myself well enough for someone to throw a life-line. Thanks.

vietits
22 Mar 2012, 6:56 PM
{'success':true, 'sorters':"{property:'name',direction:'DESC'}", 'filters':[ { 'id':'product_id', 'type':'string', 'value':'xyz'} ] }


With your json data above, after json decode, sorters is a string "{property:'name',direction:'DESC'}", not an object {property:'name',direction:'DESC'}. Just remove the double quotes and everything will be good. The other way is to decode sorters:


grid.getStore().sorters.add(new Ext.util.Sorter( Ext.decode( myFilterStoreResponse.sorters) ) );

robertb216
22 Mar 2012, 7:07 PM
Thanks, that was the problem exactly. sometimes you stare at something too long and have to take a break.