PDA

View Full Version : Simple task: Pre-select rows and focus first selected after store.loadData()



whalethewise
20 Sep 2012, 11:54 AM
Simple task but gave me a lot of pain. When selection works fine - view.focusRow() or view.focusNode() is a killer. I ended up doing this, where focusOnSelect is a function of .mon
Rows are selected but not focused without DelayedTask.
I would appreciate any suggestions to simplify this if possible.

PS. Just to clarify, I first create window with empty grid. Than I call store.loadData().

Thanks



editAttrValuesGrid.mon(editAttrValuesGrid.getView(), 'refresh',editAttrValuesGrid.focusOnSelected, editAttrValuesGrid);





focusOnSelected: function (view) {
if (this.selectedIndexes.length > 0)
{
var task = new Ext.util.DelayedTask(function (view) {
var grid = view.ownerCt;
view.focusRow(grid.selectedIndexes[0]);
for (var i=0; i < grid.selectedIndexes.length; i++)
{
grid.getSelectionModel().select(grid.selectedIndexes[i], true);
}
grid.selectedIndexes = [];
});
task.delay(500, null, null, [this.getView()]);
}
},

metalinspired
20 Sep 2012, 11:57 PM
Have you setup mode of selection model to support multiple selections?

Try this:


focusOnSelected: function (view) {
if (this.selectedIndexes.length > 0)
{
var grid = view.ownerCt;
grid.getSelectionModel().select(this.selectedIndexes);
view.focusRow(grid.selectedIndexes[0]);
//grid.selectedIndexes = []; <----- not sure what you are trying to achieve here
}
},

whalethewise
21 Sep 2012, 5:20 AM
This is a first thing I tried because it is easiest. But didnt work for me.

First, when you pass array of indexes into selectionModel.select () it doesnt select. Either one-by-one with indexes or array of model objects.
Second, it doesnt focus. As soon as I use delayed task everything works.

metalinspired
21 Sep 2012, 5:26 AM
You're right about select().
Simple for() loop will suffice but add third param as true to suppress events.
I don't know why focus() doesn't work because I'm using exact code that I pasted, in one of my apps, and if works just fine.

whalethewise
21 Sep 2012, 5:43 AM
focus works fine when I have Grid generated and loaded . And when after that I come and do focusRow() - no problem. But during loadData - I have a problem.

So I ended up doing following:
I mon() to 'datachanged' event where I do selection.
And I mon() with delay to view 'refresh' event where I do focus.



editAttrValuesGrid.mon(editAttrsStore, 'datachanged',editAttrValuesGrid.storeLoaded, editAttrValuesGrid, {single: true});
editAttrValuesGrid.mon(editAttrValuesGrid.getView(), 'refresh',editAttrValuesGrid.focusOnSelected, editAttrValuesGrid, {delay: 1,single: true});

......
......
focusOnSelected: function (view) {


if (this.selectedIndexes.length > 0)
{
this.getView().focusRow(this.selectedIndexes[0]);
}
},
storeLoaded: function (store) {


if (this.selectedIndexes.length > 0)
{
Ext.suspendLayouts();
var view = this.getView();
var grid = this;
for (var i=0; i < grid.selectedIndexes.length; i++)
{
grid.getSelectionModel().select(grid.selectedIndexes[i]);
}
Ext.resumeLayouts(true);
}
},

metalinspired
21 Sep 2012, 12:32 PM
This is a long shot, but have tried using on() instead of mon()?

whalethewise
21 Sep 2012, 12:34 PM
on() without delay on view 'refresh'?

metalinspired
21 Sep 2012, 1:23 PM
try changing these two lines to on() with no delay and lets see if it has any effect


editAttrValuesGrid.mon(editAttrsStore, 'datachanged',editAttrValuesGrid.storeLoaded, editAttrValuesGrid, {single: true}); editAttrValuesGrid.mon(editAttrValuesGrid.getView(), 'refresh',editAttrValuesGrid.focusOnSelected, editAttrValuesGrid, {delay: 1,single: true});

ashu2289
14 Nov 2012, 11:05 PM
@whalethewise (http://www.sencha.com/forum/member.php?282081-whalethewise): Did you get any solution to this? I am also facing the similar issue.