PDA

View Full Version : Get Index of the selected rows



Foggy
31 Aug 2007, 3:35 PM
Hi there,
another question about indexes.
How can i get the index of the selected rows of a grid?
The Objects of all selected rows, i can get with:


var oldSelections = mainPanel.getExtPanel().getSelectionModel().getSelections();
Now i search a solution to get their index number to reselect this rows, my try is this snippet but it dont work :


Ext.each(oldSelections, function(selection){
var selIndex = mainPanel.getDataStore().indexOf(selection);
alert(selIndex);
});

Foggy
31 Aug 2007, 6:09 PM
Hm, ok at the time i have found a solution, but im sure thats the wrong way :)
Heres the code

// on datastore load event
this.ds.on('load', function(e){
// get current category id from west panel
var catId = westPanel.getCatId();
// if current catId same like last catId
if (catId == this.catId) {
// get old selections
var oldSelections = mainPanel.getExtPanel().getSelectionModel().getSelections();
// clear all selections
mainPanel.getExtPanel().getSelectionModel().clearSelections();
// for each all old selections
Ext.each(oldSelections, function(selection){
// get the row index by searching the data store <--- this is surely wrong :)
var rowIndex = mainPanel.getDataStore().find('articleNumber', selection.data.articleNumber);
// if any record was found
if (rowIndex != -1) {
// reselect the row to get the new data
mainPanel.getExtPanel().getSelectionModel().selectRow(rowIndex, true);
}
});
// get current selections
var selections = mainPanel.getExtPanel().getSelectionModel().getSelections();
// current recored = first selection or null
var record = selections[0] ? selections[0] : null;
} else {
// set the current record to null
var record = null;
}

// save the current catId
this.catId = catId;

// Render the detail panel in south
southPanel.showDetail(record);
});
Maybe someone has a better solution for this?

Animal
31 Aug 2007, 11:58 PM
What on earth are you trying to do?

Foggy
2 Sep 2007, 5:24 AM
Hi Animal, i know this code above must looking crazy for someone other than me :)
And im confused about it but the thing is the following:
I have a grid in a center panel and i have a detail view of any selected row in the south panel. So my eventListener for rowselect works really great:

this.panel.on('rowclick', function(e){
var selections = mainPanel.getExtPanel().getSelectionModel().getSelections();
var record = selections[0] ? selections[0] : null;
southPanel.showDetail(record);
});
Ok, but if i change the data and after change reload the store, i should render the south panel again, because there are new data. Ok im thinking the following shuld work:

this.ds.on('load', function(e){
var selections = mainPanel.getExtPanel().getSelectionModel().getSelections();
var record = selections[0] ? selections[0] : null;
southPanel.showDetail(record);
}); but in record.data.amount is still the value from beforeload, so i have to RESELECT :)

Hope you understand my bad english :)

Greets Claudio

Animal
2 Sep 2007, 11:06 PM
I can't see how it could be the data from before the load. There's some mistake somewhere. The data has been reloaded. What you get from the selections is what's there.

Foggy
4 Sep 2007, 11:37 AM
Yes i know what you mean, and im really confused too. But thought i wait till EXT 2.0 is released as stable :)
If you think theres something wrong in my code or database, here are some screenshots:

First (logical) try to render detail view on datastore load event:
Code:

this.ds.on('load', function(e){
var selections = eStockPanels.mainPanel.getExtPanel().getSelectionModel().getSelections();
var record = selections[0] ? selections[0] : null;
if (record != null) {
alert(record.data.amount);
}
eStockPanels.southPanel.showDetail(record);
});
Screenshot before update action and ds load:
www.uwd.ch/ext_screens/before_update.gif

Screenshot after update action, the alert is printing 0:
www.uwd.ch/ext_screens/after_update.gif



With my dirty hack...
Code:

this.ds.on('load', function(e){
var oldSelections = eStockPanels.mainPanel.getExtPanel().getSelectionModel().getSelections();
eStockPanels.mainPanel.getExtPanel().getSelectionModel().clearSelections();
Ext.each(oldSelections, function(selection){
var rowIndex = eStockPanels.mainPanel.getDataStore().find('articleNumber', selection.data.articleNumber);
if (rowIndex != -1) {
eStockPanels.mainPanel.getExtPanel().getSelectionModel().selectRow(rowIndex, true);
}
});

var selections = eStockPanels.mainPanel.getExtPanel().getSelectionModel().getSelections();
var record = selections[0] ? selections[0] : null;
if (record != null) {
alert(record.data.amount);
}
eStockPanels.southPanel.showDetail(record);
});

Screenshot before update action and ds load:
www.uwd.ch/ext_screens/with_before_update.gif

Screenshot after update action, the alert is printing 123456:
www.uwd.ch/ext_screens/with_after_update.gif

quixote
4 Sep 2007, 1:05 PM
I have seen this exact same behavior in my own application. Unfortunately I don't have any code to reference since my Ext project is a bit of a side project and I don't have it here with me.

I also was attempting to update a child table based on data in a parent table and found that the record in the selection model was out of sync with that of a reloaded table. There was a very similar workaround that I had to implement as well to solve the issue so that I could update the child table correctly.

If I think of it I will post back with exactly what I was trying to do and how exactly I solved it. Perhaps I was doing something incorrectly, but it seemed like the exact same thing you have brought up here.

Animal
4 Sep 2007, 11:24 PM
OK, I get you. I think this is an Ext bug.

On a reload, the GridView refreshes itself. It subscribes directory to the Store's "datachanged" event, and updates it's UI.

But the "selections" property of the Grid's SelectionModel remains unchanged. It's a seperate Array of selected records, and it remains undisturbed during a reload operation. The Grid object is not aware of reloads, and so does not inform its SelectionModel.

I think you should report this as a bug.

It may be that selections should be dropped during a reaload, but "preserveSelections:true" might be a good idea as an option. Suggest that in your bug report.

Perhaps the "selectionchange" should be refired as previously selected rows are reselected with new data in them if "preserveSelections" is true.