We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exchange of data between two grids looses data

Hi,

I exchange data between two grids in a javascript function:

                var firstGrid = document.getElementById('firstFilesGrid').ej2_instances[0];
                var secondGrid = document.getElementById("secondFilesGrid").ej2_instances[0];

                firstGrid.addRecord(secondGrid.getSelectedRecords()[0]);
                secondGrid .deleteRecord("Id", firstGrid.getSelectedRecords()[0]);
                firstGrid.refresh();

Once the firstGrid gets the record from secondGrid, a column value gets missed though both the grids have same columns.

<e-grid-column field="Some" headerText="Some" textAlign="Left" width="160"></e-grid-column>
       

5 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 28, 2019 10:16 AM UTC

Hi John,  

Greetings from Syncfusion.  

The reported issue might have occurred due to the shallow copy of the data from the Grid records. To overcome this problem, we suggest to stringify and re-parse the JSON data as shown in the following code example.  

var firstGrid = document.getElementById('firstFilesGrid').ej2_instances[0]; 
                var secondGrid = document.getElementById("secondFilesGrid").ej2_instances[0]; 

                firstGrid.addRecord(JSON.parse(JSON.stringify(secondGrid.getSelectedRecords()[0]))); 
                secondGrid .deleteRecord("Id", JSON.parse(JSON.stringify(firstGrid.getSelectedRecords()[0]))); 
                firstGrid.refresh(); 

Regards, 
Seeni Sakthi Kumar S. 



JS John Stephen Mangam September 3, 2019 01:53 PM UTC

Hi,

I tried but this solution didn't work. Could you please provide me a working sample for this scenario?

Thank you.
John


TS Thavasianand Sankaranarayanan Syncfusion Team September 4, 2019 02:12 PM UTC

Hi John, 

Before proceeding on this please provide the following details. 

  1. Are you using the same dataSource for both Grids ?
  2. Please explain in detail of your requirement.
  3. Share the Grid rendering code example.

Regards, 
Thavasianand S. 



JS John Stephen Mangam September 18, 2019 11:25 AM UTC

Hi Thavasianand, 

  1. Are you using the same dataSource for both Grids ?
No, different datasource but same datatype [Books]

2. Please explain in detail of your requirement.

1st grid shows all books from which we select and add to second grid.

From the second grid, if we choose to remove a book, it will get added back to first grid.

3. Example

In the column templates, we have a javascript method calls and then 


                var firstGrid = document.getElementById('firstFilesGrid').ej2_instances[0];
                var secondGrid = document.getElementById("secondFilesGrid").ej2_instances[0];

                firstGrid.addRecord(secondGrid.getSelectedRecords()[0]);
                secondGrid .deleteRecord("Id", firstGrid.getSelectedRecords()[0]);
                firstGrid.refresh();

Even when I use your proposed JSON.parse(JSON.stringify(secondGrid.getSelectedRecords()[0])) 
it looses data like title or other details of the book while adding to other grid.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team September 19, 2019 11:27 AM UTC

Hi John, 
 
Thanks for your update. 
 
Query: 1st grid shows all books from which we select and add to second grid. From the second grid, if we choose to remove a book, it will get added back to first grid. 
 
Based on your requirement, we have prepared a sample. Initially, we have rendered two grid with different dataSource. Then, we have added the selected records in the gridOne to gridTwo in a button click event. If we delete the record from gridTwo, the deleted records are added in the gridOne. Refer the below code snippets and sample for your reference. 
 
[code snippets] 
<div> 
    <ejs-button id="show" cssClass="e-flat" content="Add Selected Records to Second Grid"></ejs-button> 
    <h2>gridOne</h2> 
    <ejs-grid id="Grid" dataSource="@ViewBag.datasource" allowPaging="true" allowSorting="true" allowSelection="true"> 
        ... 
   </ejs-grid> 
</div> 
 
<div> 
    <h2>gridTwo</h2> 
    <ejs-grid id="DestGrid" dataSource="@ViewBag.datasource1" allowSelection="true" allowPaging="true" toolbar="@(new List<string>() { "Delete" })" actionBegin="actionBegin"> 
        <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings> 
        ... 
   </ejs-grid> 
</div> 
 
<script> 
    var sel = []; 
    document.getElementById('show').onclick = () => { 
        var destGrid = document.getElementById("DestGrid").ej2_instances[0]; 
        var grid = document.getElementById("Grid").ej2_instances[0]; 
        if (destGrid.dataSource.length == 0) {    //check whether the second grid has records or not 
            sel = grid.getSelectedRecords(); 
            destGrid.dataSource = sel;    //added data to gridTwo 
        } else { 
            for (var i = 0; i < grid.getSelectedRecords().length; i++) { 
                var newData = destGrid.dataSource; 
                newData.push(grid.getSelectedRecords()[i]);    //push the selected records from the gridOne with the already in the gridTwo dataSource 
                destGrid.dataSource = []; 
                destGrid.dataSource = newData;      //added data to gridTwo 
            }   
        } 
        for (var i = 0; i < grid.getSelectedRecords().length; i++) { 
            grid.deleteRecord(grid.getPrimaryKeyFieldNames()[i], grid.getSelectedRecords()[i])   //delete the record from gridOne 
        } 
    }; 
 
    function actionBegin(args) { 
        if (args.requestType == "delete") { 
            var deletedData = args.data;    //get the deleted record details 
            var grid = document.getElementById("Grid").ej2_instances[0]; 
            grid.addRecord(deletedData[0], 0)     //added the record in gridOne 
 
        } 
    } 
</script> 
 
We have prepared a solution as per your requirement. Please follow this solution. If you still face any issue with the solution, please provide the following details to analyze the problem at our end. 
 
  1. Complete code example of the Grid
  2. Customized scripts used your requirement
  3. You can also modify the attached sample and reproduce the issue
 
 
Regards, 
Seeni Sakthi Kumar S 


Loader.
Live Chat Icon For mobile
Up arrow icon