Search results

Refresh the data source in JavaScript TreeGrid control

How to refresh the datasource

You can add/delete the datasource records through an external button. To reflect the datasource changes in Tree Grid, you need to assign the modified data to dataSource property.

Please follow the below steps to refresh the Tree Grid after datasource change.

Step 1:

Add/delete the data source record by using the following code.

    treegrid.dataSource.unshift(data); // add a new record.

    treegrid.dataSource.splice(selectedRow, 1); // delete a record.

Step 2:

Refresh the Tree Grid after the data source change by using the refresh method.

    treegrid.refresh(); // refresh the Grid.
Source
Preview
index.ts
index.css
index.html
import { TreeGrid ,extendArray} from '@syncfusion/ej2-treegrid';
import { Button } from '@syncfusion/ej2-buttons';
import { projectData } from './datasource.ts';

let treegrid: TreeGrid = new TreeGrid({
    dataSource: projectData,
    idMapping: 'TaskID',
    parentIdMapping: 'parentID',
    treeColumnIndex: 1,
     columns: [
     { field: 'TaskID', headerText: 'Task ID', width: 70, textAlign: 'Right' },
     { field: 'TaskName', headerText: 'Task Name', width: 100, textAlign: 'Left' },
     { field: 'StartDate', headerText: 'Start Date', width: 90, textAlign: 'Right', type: 'date', format: 'yMd'},
     { field: 'EndDate', headerText: 'End Date', width: 90, textAlign: 'Right', type: 'date', format: 'yMd'},
     { field: 'Duration', headerText: 'Duration', width: 90, textAlign: 'Right' },
     { field: 'Priority', headerText: 'Priority', width: 90, textAlign: 'Right' }
    ],
});
treegrid.appendTo('#TreeGrid');

let add: Button = new Button({}, '#add');
let dele: Button = new Button({}, '#delete');

document.getElementById('add').onclick = () => {
 let treegrid = document.getElementsByClassName("e-treegrid")[0].ej2_instances[0];   //take treegrid instance here
     let dataSource = extendArray(treegrid.dataSource);
     dataSource.unshift({ TaskID: 99, TaskName: "New Data", StartDate: new Date('09/07/2020'), Duration: 10, Priority: "High" }); // Add record
        treegrid.dataSource = dataSource;  // Refresh the TreeGrid.
};

document.getElementById('delete').onclick = () => {
    let treegrid = document.getElementsByClassName("e-treegrid")[0].ej2_instances[0]; //take treegrid instance here
    let selectedRow = treegrid.getSelectedRowIndexes().length;
     let selectedRowIndex = treegrid.getSelectedRowIndexes()[0];
        let dataSource = extendArray(treegrid.dataSource);
        if (selectedRow > 0) {
            dataSource.splice(selectedRowIndex, 1); // Delete record.
        }
        else {
            alert("No records selected for delete operation");
            }
        treegrid.dataSource = dataSource; // Refresh the TreeGrid.
};
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  font-family: 'Helvetica Neue','calibiri';
  font-size: 14px;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

.disabletreegrid {
  pointer-events: none;
  opacity: 0.4;
}
.wrapper {
  cursor: not-allowed;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Grid</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Grid Control" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-treegrid/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-calendars/styles/material.css" rel="stylesheet" />
    
    
    
    <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>
<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <button class="e-flat" id='add'>Add</button>
        <button class="e-flat" id='delete'>Delete</button>
        <div id='TreeGrid'></div>
        </div>        
    </div>
</body>
</html>