PDA

View Full Version : Duplicate previous row in grid on button click



leowyatt
22 Nov 2010, 1:28 AM
Hi,

I have a grid which I add rows to using an add button. What I'm trying to do is get the previous row data and create a new row using that data.

I can't seem to select the row though, looking through the documentation and google it seems I need to use;


linesGrid.getSelectionModel().getSelections();However when I use this I get told;

linesGrid.getSelectionModel().getSelections is not a function

I'm not sure why I'm getting this error because if I remove the .getSelections(); I don't get an error out but I get an object in the console.

Here is the code for the grid;


var linesGrid = new Ext.grid.EditorGridPanel({
frame:true,
title: 'Lines',
height:380,
width: '100%',
clickstoEdit: 1,
store: store,
colModel: cm,
tbar: [{
text: 'Add Line',
handler: function(){

var count = linesGrid.store.getCount();

if(count > 0){
rowsSelected = linesGrid.getSelectionModel().getSelections();
console.log(rowsSelected);



}

linesGrid.getStore().insert(0, new Line({
item_type: '',
size: '',
quantity: '',
sku_reference: '',
colour: '',
length: '',
nett_weight: '',
gross_weight: '',
customer_reference: '',
lot: '',
bank: '',
customs: ''
})
);
linesGrid.startEditing(0,0);

}
}]
});

Condor
22 Nov 2010, 1:32 AM
EditorGridPanel uses a CellSelectionModel by default. You are expecting a RowSelectionModel.

I would recommend specifying a RowSelectionModel in your grid config (but you could also keep using the CellSelectionModel, and only get the current rowindex from the selection model and fetch the record from the store).

leowyatt
22 Nov 2010, 1:38 AM
EditorGridPanel uses a CellSelectionModel by default. You are expecting a RowSelectionModel.

I would recommend specifying a RowSelectionModel in your grid config (but you could also keep using the CellSelectionModel, and only get the current rowindex from the selection model and fetch the record from the store).

Hi Condor,

Thanks for your reply. So to use a row selection model I'd use something like this?


var selModel = new Ext.grid.RowSelectionModel({
singleSelect : true
})and then add this to my grid config?


selModel : selModel,

Condor
22 Nov 2010, 1:50 AM
Correct.

leowyatt
22 Nov 2010, 1:53 AM
I've tried that and it selects the 1st row in the grid, I need the previously inserted row. I'm assuming I need to provide an id to this so am just trying that now.

Condor
22 Nov 2010, 1:59 AM
How do you know what the previously inserted row is?

leowyatt
22 Nov 2010, 2:03 AM
sorry when I said id I meant like a count. I was going to count the rows in the grid and use that to select the row.

so I've gone with this


var count = linesGrid.store.getCount();

if(count > 0){
var lastRow = count - 1;
console.log(lastRow);
var rowsSelected = linesGrid.getSelectionModel().getSelections(lastRow);
console.log(rowsSelected);
}

Testing this it seems to be 2 rows behind and is only triggered if the user presses the enter key

leowyatt
22 Nov 2010, 2:14 AM
I've also realised that once I've got rowsSelected I'm not sure how I get the data back. I can see it in firebug just need to figure out how to access it.

Condor
22 Nov 2010, 3:41 AM
getSelections returns the currently selected records.

Do you want to select a record, e.g. grid.getSelectionModel().selectRow(rowIndex)
Or do you want to get the record at the index, e.g. grid.getStore().getAt(rowIndex)

leowyatt
22 Nov 2010, 3:59 AM
getSelections returns the currently selected records.

Do you want to select a record, e.g. grid.getSelectionModel().selectRow(rowIndex)
Or do you want to get the record at the index, e.g. grid.getStore().getAt(rowIndex)

The thing is the user won't be selecting any rows. They will fill in the row of data, click add, a new row will appear with the same data as the previous one.

I think the store is probably the way I need to go.

leowyatt
22 Nov 2010, 4:42 AM
getSelections returns the currently selected records.

Do you want to select a record, e.g. grid.getSelectionModel().selectRow(rowIndex)
Or do you want to get the record at the index, e.g. grid.getStore().getAt(rowIndex)

I can get it back using the following code;


var previousRow = linesGrid.getStore().getAt(1);

Now I just need to figure out how to access the data. I've tried
previousRow.get(fieldDataIndex); but I get told previousRow is undefined.

Will have a look at the documentation

leowyatt
22 Nov 2010, 5:13 AM
I've figured out it is undefined because when it runs through the first time the row 1 has not been created.

Only issue now is it is still 2 rows behind.

Solved - just needed to change the index from 1 to 0 as the store counts up from 0 not down to 0.

leowyatt
22 Nov 2010, 5:21 AM
Thank you very much for your help with this Condor :)