PDA

View Full Version : GridPanel - Finding a Row Index Based on a Value



djdevz
4 May 2011, 3:05 AM
Hi, my aim is to select a row in the grid (and focus on the row), based on the 'egId' value in a (plain HTML) textbox.

for instance if the 'egId' value = 10
then I would like the record in the grid to be selected where the column called 'id' contains a value of 10 (not the record id).

The first 'if' in the code below is for when the 'egId' field is null. But when it is not null, this is when I'd like the grid to select the record based on the 'egId' field.

I tried using findRowIndex(), but whatever I enter returns 'false',
and the focusRow() and selectRow() methods has '8' as the parameter as I was just testing to see how they work.



var id = Ext.getDom('egId').value;
var fieldValue = Ext.getDom('egFieldValue').value;

store.on('load', function() {
if (!id && fieldValue) {
grid.getSelectionModel().selectFirstRow();
}
else if(id && fieldValue) {

var rowIndex = sapLinkGrid.getView().findRowIndex(id);
alert(rowIndex);

grid.getSelectionModel().selectRow(8);
grid.getView().focusRow(8);
}
else {}

});
Any help is greatly appreciated, and this is my first post so apologies if I've missed anything out.

friend
4 May 2011, 3:52 AM
Try something like this:


var targetRowIndex = grid.store.find('id', id);

djdevz
4 May 2011, 4:11 AM
legend! it works!
except i used 'store.find' rather than 'grid.store.find'
(shouldn't make a difference though, right?)


var rowIndex = store.find('id', id);
grid.getSelectionModel().selectRow(rowIndex);
grid.getView().focusRow(rowIndex);


Just a little off topic, but the 'focusRow()' method works fine in IE, but will only work in FF if I place an alert after 'rowIndex':



var rowIndex = store.find('id', id);
alert(rowIndex);


Otherwise there is no focus in FF...Any thoughts?

Appreciate the help!

friend
4 May 2011, 4:50 AM
You're correct, 'store.find()' is perfectly okay, so long as variable 'store' is in scope.

For focusing a row in the grid, try this instead:


grid.selModel.selectRow(rowIndex)

djdevz
4 May 2011, 5:40 AM
Thanks 'friend' you've really helped me out here...

It seems there's a logical issue that I've come across though.

the if else if statement I posted is for 'add' and 'update',

- if you add a record, there won't be an 'id' in my field (it gets generated by the back end, and the grid picks it up when it's reloaded)
- if you update a record, there will be an 'id' in my field (because upon selection of a record in the grid, I transfer the fields from the grid to the html fields)

So with the first 'if' statment (adding), I select the first row, because the grid is sorted by 'id' (desc), so that the newest records are at the top, therefore selected the record that has been added.

However if the user sorts the grid using another column, the added record doesn't go to the top meaning when I select the first row, I'm selecting the wrong record!

My work around idea is to search for the record with the id greater than the current highest id, (as the generator will always produce a higher number), as to find the newly created record.

Is this possible/any ideas on how to do this?

Sorry for the rambling, but I wanted to make sure I portrayed my logic and problem properly.
Thanks again.

djdevz
4 May 2011, 6:12 AM
no worries I just sorted it by 'id' as default,
so the user has to sort the columns manually again after adding a record.

daplume
26 May 2017, 3:21 PM
For the record




var sto = grid.getStore();
var rowIndex = sto.find(layGridInfo.gridIdField, recno);

grid.getSelectionModel().select(rowIndex);
grid.getView().focusRow(rowIndex);



The select function on the selection model is NOT selectRow(n) as specified earlier. It is simply select();

Hope this helps.