PDA

View Full Version : Copy/Paste all rows. How to accelerate procedure ?



Zoze
11 May 2009, 3:23 AM
Hi guys,

I have two grids (GridPanel) and I want to implement a functionality to copy all rows from
source grid to target grid. Here is a snippet of my code (destStore is JsonStore):


this.copyAllRows = function (srcGrid, destGrid, destStore) {
// select all rows from src grid
srcGrid.getSelectionModel().selectAll();

// records array contains all selected rows
var records = srcGrid.getSelectionModel().getSelections();

for (var i = 0; i < records.length; i++) { // iterate all records
var foundItem = this.recordExists(records[i].get('id'), destGrid);

if (!foundItem) {
destStore.add(records[i]);
}
}
srcGrid.getSelectionModel().clearSelections();
}

copyAllRows() calls the recordExists() which determines if a row already exists in
target grid. Here is its code:


this.recordExists = function (str, grid) {
grid.getSelectionModel().selectAll();
var records = grid.getSelectionModel().getSelections();

for (var i = 0; i < records.length; i++) {
if (str == records[i].get('id')) {
grid.getSelectionModel().clearSelections();
return true;
}
}
grid.getSelectionModel().clearSelections();
return false;
}

my problem is that the copy procedure is too slow. Initially I used the store.find(...)
method to determine if a row already exists. It was much faster, but find() was not
working properly. For example if target store contained 'id' with value '111' and I was
looking for id '1', find() returned 'true' and the row with id=1 was not copied.

Can anyone propose an alternative solution (if any). Thanks in advance!

Condor
11 May 2009, 3:45 AM
Use:

this.copyAllRows = function (srcGrid, destGrid) {
var records = srcGrid.getStore().getRange();
for (var i = 0, len = records.length; i < len; i++) {
var foundItem = this.recordExists(records[i].get('id'), destGrid);
if (!foundItem) {
destGrid.getStore().add(records[i]);
}
}
}
this.recordExists = function (id, grid) {
var index = grid.getStore().findBy(function(record){
return record.get('id') == id;
});
return index >= 0;
}

And if the id is also defined as the idProperty of the reader then this is even faster:

this.recordExists = function (id, grid) {
var index = grid.getStore().indexOfId(id);
return index >= 0;
}

Zoze
11 May 2009, 4:06 AM
Thank you Condor !!!

I am grateful for the immediate response and of course your solution that fixed my bug!