-
10 May 2012 6:33 AM #1
GridPanel: get selected record id vs. sorting
GridPanel: get selected record id vs. sorting
Hi forum
I would like to detect the id of a selected record in a grid using the RowSelectionModel:
The above code works fine, but I am facing the following problems:Code:MyGrid = Ext.extend(Ext.grid.GridPanel, { sm: new Ext.grid.RowSelectionModel({singleSelect:true}), [...] tbar: { id: 'gridTbar', items: [{ text: 'Show record id', id: 'myButton', handler: function() { var grid = this.ownerCt.ownerCt; var selectionModel = grid.getSelectionModel(); var record = selectionModel.getSelected(); var recordId = grid.store.indexOf(record); alert(recordId); } }] } initComponent:function() { // This renders the grid [...] } listeners:{ 'beforeexpand': function() { // This loads the grid (column definitions and data) [...] }, } });
- store.indexOf() returns an index which depends on the (client side) sorting of the grid. This means the record has a different index everytime I click on one of the column headers.
- The grid/store columns are built dynamically (read from the database). I do not know what columns the store contains and I can not rely on the store to contain any 'id' or 'key' column. I can not add such a column to the store because this might overwrite one of the existing columns in the store (which I do not know in advance).
Is there a possibility to unambiguously identify the selected record through a cached id or key, which does not change when sorting/filtering the store?
Thanks a lot.
BTW, is there a better solution to access the grid whithin the button handler than
?Code:var grid = this.ownerCt.ownerCt;
-
10 May 2012 10:39 AM #2
For accessing the grid, you can create a private function and then use:
:: return this.getComponent('myGrid); where 'myGrid' is itemId for the grid.
As for accessing a record, you will need to pass a unique id from the server as the grid uses this to distinguish the difference between records. I understand that your tables are random, but the client needs a way to reference.
Regards,
Scott.
-
10 May 2012 11:43 AM #3
Thanks Scott.
True, that's definitely more elegant
Hmm, so if I pass two identical records from the server, the grid has no way to distinguish them? I was hoping that the grid/store would assign a reference by itself which I could then access as well (like an index of an array or something).
So I pass the reference from the server, but of course I don't want this (key-) column to be displayed in the grid. I can hide it using, but then the user would still be able to unhide the column using the header menu. Normally I would just remove the column from the columnModel, but in this case that would be rather pointlessCode:myGrid.getColumnModel().setHidden(0, true);
How can I prevent this column from being displayed?
Thanks a lot!
-
10 May 2012 1:24 PM #4
I usually like to create several column modules and then using grid.reconfigure to display different columns in the grid. You could build a dynamic list.
As for the unique records, an id is created, but it will not have any relation to your records on the server.
You can poke around the record object and see if there is anything you can use.
Regards,
Scott.
-
11 May 2012 4:19 AM #5
I solved the issue by adding a reference column to the store. The column is added to the columnModel to be easily acccessible through record.get(), but it can be kept hidden by setting the properties
.Code:hidden:true, hideable:false
Although this is not exactly what I was hoping for, I think I can live with that
Thanks for your input.


Reply With Quote
