28

I am using Kendo grid.

I want to remove all rows from Kendo grid using JavaScript.

I removed them using a for loop but I want to find the best way to remove all rows.

Rikin Patel
  • 8,848
  • 7
  • 70
  • 78

4 Answers4

59

try following the code.

var grid = $("#Grid").data('kendoGrid');           

grid.dataSource.data([]); 
grid.setDataSource([]);

for demo click here

Update: Fixed sorting issue... Thanks @Windle

Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
  • 3
    This has the problem @PaulGorbas mentions in [his answer](http://stackoverflow.com/a/40941272/298278). Updated the fiddle in the answer to demonstrate [the issue](http://jsfiddle.net/fsvSE/115/). If you remove all the rows, and then click a column header to sort the data comes back. – Windle Dec 13 '16 at 17:43
9

That doesn't really move the underlying data of the grid, it just clears the rows being displayed. If you sort the "empty" grid, all the rows reappear form the underlying data.

If instead of removing your data as shown like this:

dataSource.data([]);

and instead replace it with a new array of data, say called result.Data.. like this:

dataSource.data(result.Data)

you will see the data swap, but if sort or page, again the original data is shown.

Anyone know how to actually change the data and have the new data replace the source data of the grid?

UPDATE: The answer is to ALSO use the setDataSource method:

var grid = $("#grid").data("kendoGrid");
var dataSource = grid.dataSource;
dataSource.data([]);//clear out old data
dataSource.data(result.Data);//add new data
grid.setDataSource(result.Data);//set the new data as the grids new datasource
dataSource.sync();//refresh grid
Paul Gorbas
  • 1,694
  • 20
  • 16
5

If you are working with Angualrjs, then try to follow this code:

 $scope.gridData.data([]);

Where gridData is k-data-source="gridData"

Javasick
  • 2,753
  • 1
  • 23
  • 35
Shafeeq
  • 59
  • 1
  • 3
2

This worked fine for me.

var grid = $("#Grid").data("kendoGrid");
var newDataSource = new kendo.data.DataSource({
    data: []
});
grid.setDataSource(newDataSource);
Mahesh
  • 21
  • 1
  • Will this wipe out the schema and transport settings of the grid as well? – GreySage Feb 06 '19 at 23:32
  • This completely wipes out the grid data source settings. If you need to make the grid work again, you'll need to assign a new data source with the correct settings. – Tawab Wakil Jul 30 '20 at 13:24