Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all rows from a Kendo grid

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.

like image 376
Rikin Patel Avatar asked Jul 29 '13 07:07

Rikin Patel


People also ask

How do I turn off the column in Kendo grid?

You can do this by setting the field to editable: false on the data source.

How do I hide the delete button in kendo grid?

k-grid-delete", "#grid"). hide(); }); It hides the button when i click the button. You need to do the $(". k-grid-delete", "#grid").


2 Answers

try following the code.

var grid = $("#Grid").data('kendoGrid');             grid.dataSource.data([]);  grid.setDataSource([]); 

for demo click here

Update: Fixed sorting issue... Thanks @Windle

like image 93
Rikin Patel Avatar answered Sep 21 '22 16:09

Rikin Patel


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 
like image 31
Paul Gorbas Avatar answered Sep 20 '22 16:09

Paul Gorbas