Search results

Refresh the data source in JavaScript (ES5) TreeGrid control

30 Jul 2021 / 2 minutes to read

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.

Copied to clipboard
    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.

Copied to clipboard
    treegrid.refresh(); // refresh the Grid.
Source
Preview
index.js
index.css
index.html
Copied to clipboard
var treegrid = new ej.treegrid.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');

var add = new ej.buttons.Button({}, '#add');
var dele = new ej.buttons.Button({}, '#delete');

document.getElementById('add').onclick = () => {
    var data = { TaskID: 111, TaskName: "New Task", Duration: 40, StartDate: new Date(8367642e5), Priority: "High" };
    (treegrid.dataSource).unshift(data); // add new record.
    treegrid.refresh(); // refresh the TreeGrid.
};

document.getElementById('delete').onclick = () => {
    if (treegrid.getSelectedRowIndexes().length) {
        var selectedRow = treegrid.getSelectedRowIndexes()[0];
        (treegrid.dataSource).splice(selectedRow, 1); // delete the selected record.
    } else {
        alert("No records selected for delete operation");
    }
    treegrid.refresh(); // refresh the TreeGrid.
};
Copied to clipboard
#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;
}
Copied to clipboard
<!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://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
    
    <div id="container">
        <button class="e-flat" id="add">Add</button>
        <button class="e-flat" id="delete">Delete</button>
        <div id="TreeGrid"></div>
        </div>        
    

<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>