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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Exchange of data between two grids looses data

Thread ID:

Created:

Updated:

Platform:

Replies:

146975 Aug 27,2019 11:27 AM UTC Sep 19,2019 11:27 AM UTC ASP.NET Core - EJ 2 5
loading
Tags: DataGrid
John Stephen Mangam
Asked On August 27, 2019 11:27 AM UTC

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>
       

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On 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. 


John Stephen Mangam
Replied On 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

Thavasianand Sankaranarayanan [Syncfusion]
Replied On 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. 


John Stephen Mangam
Replied On 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.

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon