Hybrid View
-
22 May 2008 9:48 AM #1
Finding Record in the Store
Finding Record in the Store
I have JsonStore object and I need to find record with exact match for specific field. I tried 2 things:
1) when building JSON output on server side I added "id" param like that:
{"grid":[{"id":"1","m_regnum":"1610002346"}]}
then on browser side I call store.getById("1") but get undefined as return
2) I try calling store.find("id","1") and it does return correct index but... its partialmatch. In other words if I have another record with "id":13, it also will be found.
Is there way to either do complete match for the field or what do I make wrong in my #1 approach?
Thank you.
-
22 May 2008 11:27 AM #2
does the reader have an 'id' config property defined that maps to the field you wish to use as an id?
-
24 May 2008 8:56 AM #3
Yes it does but I think it creates "id" property instead of Reader internal id. Anyhow I solved this by just scanning through records:
Code:store.each(function(rec) { if(ReloadTable._items[rec.get("id")]) /* do something*/{break;} });
-
24 May 2008 11:10 PM #4
That's what find is for.
If you want to get by ID, use getById!
http://extjs.com/deploy/dev/docs/?cl...member=getByIdSearch the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
25 May 2008 9:02 PM #5
But that was my original problem. See my #1 last phrase:
then on browser side I call store.getById("1") but get undefined as return
My point was that looks like getById() uses internal id generated by Store and ignores "id" property passed by the server in the JSON.
-
25 May 2008 10:56 PM #6
I think I know what is going on.
Since Im using JsonStore object I do not explicitly specify "reader". Documentation says following: JsonStore is pre-configured with a built-in Ext.data.HttpProxy and Ext.data.JsonReader. Question now is what is the "id" config property that JsonStore sets for the JsonReader. Apparently nothing. I was hoping that JsonStore would at least setup id:"id" as default but its not the case.
I set "id":id in the JsonStore and everything started to work.
Note to myself: pay attention to the bold statements in the help.
Here is what I missed from the documentation: Note: Although they are not listed, this class inherits all of the config options of Store, JsonReader.
-
26 May 2008 7:49 AM #7
This confused me in the beginning. Don't confuse the id you pass from the server and assign in your store with the record id used by ext. I went through my code and changed all references to "id" that were coming from server to "primaryKey" for example just to clarify in my head what was going on.
MJ
API Search || Ext 3: docs-demo-upgrade guide || User Extension Repository
Frequently Asked Questions: FAQs
Tutorial: Grid (php/mysql/json) , Application Design and Structure || Extensions: MetaGrid, MessageWindow
-
2 Jul 2009 7:24 AM #8
It took me a couple of tries to get this right. When defining the store, it worked when I specified the field name for the 'id' property in the store, not the field number as I saw in another note.
store: new Ext.data.JsonStore(
{
id: 'item_id',
totalProperty:'totalCount',
root:'list',
url:'items.php',
fields:
[ { name:'item_id', type:'int' },
{ name:'name' },
{ name:'item_type' },
{ name:'item_lvl', type:'int' }
]
} ),
Then, to find the row number, you can use the following to select a grid item.
var itemId = 6; // whichever item should be selected
var rnum = grid.getStore().indexOfId( itemId );
grid.getSelectionModel().selectRow( rnum );
grid.getView().ensureVisible(rnum,0,true);
grid.getView().focusRow(rnum);
Hope I saved someone some time.


Reply With Quote