This is a migrated thread and some comments may be shown as answers.

Insert Array into Grid

3 Answers 810 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 08 Feb 2016, 06:58 PM

I have a page where the user is able to select objects from Grid A to either 1) insert them into Grid B or 2) insert them into Grid C. One way to expedite use of the page is we want to allow users to have a button that does "Add all Visible in Grid A to Grid B". I have some javascript that does here

 

$("#addAllSelectedToGridB").click(function () {
    var gridA = $("#gridA").data("kendoGrid");
    var gridB = $("#gridB").data("kendoGrid");
    var gridBDatasource = gridB.dataSource;
    var alreadyInGridB = gridBDatasource.view();
 
    var itemsToBeAdded = new Array();
 
    gridA.select().each(function () {
        var dataItem = gridA.dataItem($(this));
 
        var result = $.grep(alreadyInGridB, function (e) { return e.ID == dataItem.ID; });
        if (result.length == 0) {
            itemsToBeAdded.push(dataItem);
        }
    });
 
    for (i = 0; i < itemsToBeAdded.length; i++) {
        gridBDatasource.insert(itemsToBeAdded[i].toJSON());
    }
});

This was working fine when we were displaying 25 items per page in Grid A. The customer has requested that we up that total to 300 and now an operation that took less than a second now takes 10-12 seconds.

I tried looking through the grid documentation but I couldn't find an alternative method for inserting items into a gird. I'm just curious if there is a method explicitly for adding a collection of items that may possibly be more efficient?

Thanks

3 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 08 Feb 2016, 06:59 PM
I forgot to mention that I have narrowed down the time cost to "gridBDatasource.insert", the rest of the code takes maybe a millisecond on average.
0
Accepted
Radoslav
Telerik team
answered on 10 Feb 2016, 08:22 AM
Hello Chris,

I assume that the performance hit is caused by the fact that when the insert function of the datasource is called the grid is refreshed and re-rendered. In your case with 300 rows the browser renders 300 times 300 rows, then 299 row, etc. and this takes a few seconds. To optimize this I suggest you to use following approach:
Handle the databinding event and prevent it 299 times and let it to be executed only for the last (300) insertion. On the following link I attached a small example which demonstrates this approach:
http://dojo.telerik.com/EZoma/3

Please give it try and let me know if it helps you.

Regards,
Radoslav
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Chris
Top achievements
Rank 1
answered on 10 Feb 2016, 04:29 PM

That did the trick, thanks for your help.

If anyone else runs into this issue:

For me Grid B is constructed using the MVC HtmlHelper extension, so i just had to add

.Events(e => e.DataBinding("gridBDataBinding"))

and then the javascript was

var gridBDatabindingEnabled = true;
function gridBDataBinding(e)
{
    if(!gridBDatabindingEnabled)
    {
        e.preventDefault();
    }
}
 
$("#addAllSelectedToGridB").click(function () {
    var gridA = $("#gridA").data("kendoGrid");
    var gridB = $("#gridB").data("kendoGrid");
    var gridBDatasource = gridB.dataSource;
    var alreadyInGridB = gridBDatasource.view();
  
    var itemsToBeAdded = new Array();
  
    gridA.select().each(function () {
        var dataItem = gridA.dataItem($(this));
  
        var result = $.grep(alreadyInGridB, function (e) { return e.ID == dataItem.ID; });
        if (result.length == 0) {
            itemsToBeAdded.push(dataItem);
        }
    });
  
    gridBDatabindingEnabled = false;
    for (i = 0; i < itemsToBeAdded.length; i++) {
        if(i === (itemsToBeAdded.length - 1))
        {
            gridBDatabindingEnabled = true;
        }
        gridBDatasource.insert(itemsToBeAdded[i].toJSON());
    }
});

Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or