PDA

View Full Version : Efficent way to get value by an x,y from a grid



gtsafas
6 Jul 2012, 1:02 PM
I have a datagrid, I want to get the record for whatever is at say row: 0, column: 5

Is there an efficent way to do this? I will then want to update this value

scottmartin
6 Jul 2012, 6:27 PM
You could use the following:



Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
fields:['name', 'email', 'change'],
data:{'items':[
{ 'name': 'Lisa', "email":"[email protected]", "change":100 },
{ 'name': 'Bart', "email":"[email protected]", "change":-20 },
{ 'name': 'Homer', "email":"[email protected]", "change":23 },
{ 'name': 'Marge', "email":"[email protected]", "change":-11 }
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});

var grid = Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ header: 'Name', dataIndex: 'name' },
{ header: 'Email', dataIndex: 'email', flex: 1 },
{ header: 'Change', dataIndex: 'change' }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});

// courtesy of StackOverflow post
function getAttributeByIndex(obj, index){
var i = 0;
for (var attr in obj){
if (index === i){
return obj[attr];
}
i++;
}
return null;
}

// row 2, col 1; 0 based
var myrow = grid.store.data.items[1].data;
var first = getAttributeByIndex(myrow, 0);
console.log(first);


Scott.

Animal
6 Jul 2012, 10:28 PM
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-getAt

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.header.Container-method-getHeaderAtIndex

Then use the dataIndex of the returned header to get the field from the record.