1. #1
    Ext User
    Join Date
    Jan 2009
    Posts
    23
    Vote Rating
    0
    Zoze is on a distinguished road

      0  

    Default Copy/Paste all rows. How to accelerate procedure ?

    Copy/Paste all rows. How to accelerate procedure ?


    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):

    Code:
    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:

    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!

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    96
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Use:
    Code:
    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:
    Code:
    this.recordExists = function (id, grid) {
    	var index = grid.getStore().indexOfId(id);
    	return index >= 0;
    }

  3. #3
    Ext User
    Join Date
    Jan 2009
    Posts
    23
    Vote Rating
    0
    Zoze is on a distinguished road

      0  

    Default


    Thank you Condor !!!

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

Thread Participants: 1