185

How to reload or refresh a Kendo Grid using Javascript?

It is often required to reload or refresh a grid after sometime or after a user action.

Oxon
  • 4,901
  • 8
  • 40
  • 54

29 Answers29

336

You can use

$('#GridName').data('kendoGrid').dataSource.read(); <!--  first reload data source -->

$('#GridName').data('kendoGrid').refresh(); <!--  refresh current UI -->
J M
  • 396
  • 6
  • 23
Jaimin
  • 7,964
  • 2
  • 25
  • 32
  • 21
    @zespri `read` will request the server and reload **only** reload datasource. There will be no changes in the UI. `refresh` will re-render items in grid from the current datasource. That's why both are required. – macwier Apr 16 '14 at 08:25
  • 39
    I don't think you need the refresh in the latest version of Kendo. As it seems to work fine without it – GraemeMiller Oct 02 '14 at 15:59
  • Depends on what you're doing. For instance removing an item with a custom command as opposed to the default "Destroy" command from within a nested detail view, requires manually refreshing. – Matthew Pitts Oct 26 '14 at 17:51
  • 2
    Yeah! This work also with TreeList: $('#Gantt').data('kendoTreeList').dataSource.read(); $('#Gantt').data('kendoTreeList').refresh(); – Hernaldo Gonzalez Feb 11 '15 at 14:25
  • 27
    Developers explicitly say not to call refresh after read: http://www.telerik.com/forums/show-progress-spinner-during-load-refresh because it may prevent progress indicator from appearing. – Rustam Miftakhutdinov Mar 16 '15 at 15:29
  • 1
    Doesn't need .refresh(); as it will prevent the loading spinner to work properly. – Ferry Meidianto Dec 23 '15 at 19:23
  • 1
    Actually if you are on page different than 1 (> 1), you need to call `).dataSource.page(1);`. But be careful if serverPaging is true, it fires another request. To fire only one request call `dataSource.query({page: 1, pageSize: x})`. – broadband May 25 '16 at 08:36
  • 2
    I'm using a newer version and I only have to call .read. Calling .refresh after read causes two trips to the server for data. – Justin Jul 13 '16 at 21:50
  • In Angular just `dataSource.data([..your new data ])` works . – Mohamad Ali Jun 07 '17 at 15:29
  • "Maximum call stack size exceeded".... this happens when i call above mentioned methods – ARC Apr 06 '18 at 05:47
  • some one please show how to do it in angular 8 – Shaik Habeeb Jan 08 '21 at 08:26
63

I never do refresh.

$('#GridName').data('kendoGrid').dataSource.read();

alone works for me all the time.

Pang
  • 9,564
  • 146
  • 81
  • 122
Purna Pilla
  • 639
  • 5
  • 3
32
$('#GridName').data('kendoGrid').dataSource.read();
$('#GridName').data('kendoGrid').refresh();
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
Oxon
  • 4,901
  • 8
  • 40
  • 54
  • Thanks, but this gives "TypeError: $(...).data(...) is undefined" error. I also looked on many pages and tried different variations of this solution but still get the same error. Any idea? – Jack Dec 05 '15 at 15:35
  • If data('kendoGrid') returns null then most likely either the id is wrong or you haven't created the grid yet. NB you make the grid with $(..).kendoGrid() and access it later with $().data('kendoGrid') – tony Mar 11 '16 at 11:48
32

In a recent project, I had to update the Kendo UI Grid based on some calls, that were happening on some dropdown selects. Here is what I ended up using:

$.ajax({
        url: '/api/....',
        data: { myIDSArray: javascriptArrayOfIDs },
        traditional: true,
        success: function(result) {
            searchResults = result;
        }
    }).done(function() {
        var dataSource = new kendo.data.DataSource({ data: searchResults });
        var grid = $('#myKendoGrid').data("kendoGrid");
        dataSource.read();
        grid.setDataSource(dataSource);
    });

Hopefully this will save you some time.

asuciu
  • 1,234
  • 2
  • 21
  • 35
  • 1
    exactly what I was looking for grid.setDataSource(dataSource); for non-remote calls it's what you have to use. Thanks! – Rui Lima Feb 21 '16 at 22:28
22

Not a single one of these answers gets the fact that read returns a promise, which means you can wait for the data to load before calling refresh.

$('#GridId').data('kendoGrid').dataSource.read().then(function() {
    $('#GridId').data('kendoGrid').refresh();
});

This is unnecessary if your data grab is instant/synchronous, but more than likely it's coming from an endpoint that won't return immediately.

Zachary Dow
  • 1,897
  • 21
  • 36
  • 2
    Leveraging the built-in promise support is really convenient and removed a few lines of code too. I would wager that this should be the true answer. – FoxDeploy Jun 30 '19 at 14:50
  • 2
    Thank you Zachary! I have spent a couple of hours on this problem - your solution is the only one that worked for me. I am inserting rows to my grid database source via ajax, looped (one row at a time). After the loop finishes dataSource.read() worked only sometimes. "then" is what I needed. Much appreciated! – Antony D Jan 03 '20 at 00:10
10

If you do not want to have a reference to the grid in the handler, you can use this code:

 $(".k-pager-refresh").trigger('click');

This will refresh the grid, if there is a refresh button. The button can be enabled like so:

[MVC GRID DECLARATION].Pageable(p=> p.Refresh(true))
d.popov
  • 4,175
  • 1
  • 36
  • 47
10

Actually, they are different:

  • $('#GridName').data('kendoGrid').dataSource.read() refreshes the uid attributes of the table row

  • $('#GridName').data('kendoGrid').refresh() leaves the same uid

Kushan Randima
  • 2,174
  • 5
  • 31
  • 58
8

What you have to do is just add an event .Events(events => events.Sync("KendoGridRefresh")) in your kendoGrid binding code.No need to write the refresh code in ajax result.

@(Html.Kendo().Grid<Models.DocumentDetail>().Name("document")
    .DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Model(model => model.Id(m => m.Id))        
    .Events(events => events.Sync("KendoGridRefresh"))    
    )
      .Columns(columns =>
      {
          columns.Bound(c => c.Id).Hidden();              
          columns.Bound(c => c.UserName).Title(@Resources.Resource.lblAddedBy);                           
      }).Events(e => e.DataBound("onRowBound"))
          .ToolBar(toolbar => toolbar.Create().Text(@Resources.Resource.lblNewDocument))
          .Sortable()          
          .HtmlAttributes(new { style = "height:260px" })          
  )

And you can add the following Global function in any of your .js file. so, you can call it for all the kendo grids in your project to refresh the kendoGrid.

function KendoGridRefresh() {
    var grid = $('#document').data('kendoGrid');
    grid.dataSource.read();
}
Milan
  • 3,005
  • 1
  • 24
  • 26
8

In my case I had a custom url to go to each time; though the schema of the result would remain the same.
I used the following:

var searchResults = null;
$.ajax({
        url: http://myhost/context/resource,
        dataType: "json",
        success: function (result, textStatus, jqXHR) {
            //massage results and store in searchResults
            searchResults = massageData(result);
        }
    }).done(function() {
        //Kendo grid stuff
        var dataSource = new kendo.data.DataSource({ data: searchResults });
        var grid = $('#doc-list-grid').data('kendoGrid');
        dataSource.read();
        grid.setDataSource(dataSource);
    });
Amit Kapoor
  • 201
  • 3
  • 8
6

I used Jquery .ajax to get data. In order to reload the data into current grid, I need to do the following:

.success (function (result){
    $("#grid").data("kendoGrid").dataSource.data(result.data);
})
oopsdazie
  • 716
  • 1
  • 7
  • 22
5

You can use the below lines

$('#GridName').data('kendoGrid').dataSource.read();
$('#GridName').data('kendoGrid').refresh();

For a auto refresh feature have a look here

Nitin Rawat
  • 79
  • 1
  • 5
5

By using following code it automatically called grid's read method and again fill grid

$('#GridName').data('kendoGrid').dataSource.read();
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Jatin Patel
  • 57
  • 1
  • 4
5

An alternative way to reload the grid is

$("#GridName").getKendoGrid().dataSource.read();
vineel
  • 3,483
  • 2
  • 29
  • 33
5

You can always use $('#GridName').data('kendoGrid').dataSource.read();. You don't really need to .refresh(); after that, .dataSource.read(); will do the trick.

Now if you want to refresh your grid in a more angular way, you can do:

<div kendo-grid="vm.grid" id="grid" options="vm.gridOptions"></div>

vm.grid.dataSource.read();`

OR

vm.gridOptions.dataSource.read();

And don't forget to declare your datasource as kendo.data.DataSource type

Kristy Kavada
  • 113
  • 2
  • 9
4

I want to go back to page 1 when I refresh the grid. Just calling the read() function will keep you on the current page, even if the new results don't have that many pages. Calling .page(1) on the datasource will refresh the datasource AND return to page 1 but fails on grids that aren't pageable. This function handles both:

function refreshGrid(selector) {
     var grid = $(selector);
     if (grid.length === 0)
         return;

     grid = grid.data('kendoGrid');
     if (grid.getOptions().pageable) {
         grid.dataSource.page(1);
     }
     else {
         grid.dataSource.read();
     }
}
AndyMcKenna
  • 2,607
  • 3
  • 26
  • 35
4

In order to do a complete refresh, where the grid will be re-rendered alongwith new read request, you can do the following:

 Grid.setOptions({
      property: true/false
    });

Where property can be any property e.g. sortable

Faran Shabbir
  • 423
  • 1
  • 6
  • 18
4

You may try:

    $('#GridName').data('kendoGrid').dataSource.read();
$('#GridName').data('kendoGrid').refresh();
Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21
4

You can also refresh your grid with sending new parameters to Read action and setting pages to what you like :

var ds = $("#gridName").data("kendoGrid").dataSource;
ds.options.page = 1;
var parameters = {
    id: 1
    name: 'test'
}
ds.read(parameters);

In this example read action of the grid is being called by 2 parameters value and after getting result the paging of the grid is in page 1.

sajadre
  • 1,141
  • 2
  • 15
  • 30
3

Just write below code

$('.k-i-refresh').click();
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
user2951849
  • 111
  • 1
  • 9
  • 1
    This would be true only if you have initialized the grid with a pageable.refresh = true... which isn't by default. Anyway, you should not use a UI workaround when you are able to do it using an API function (see accepted answer) – The_Black_Smurf Jul 06 '15 at 02:22
3
$("#theidofthegrid").data("kendoGrid").dataSource.data([ ]);
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
3

If you are desiring the grid to be automatically refreshed on a timed basis, you can use the following example which has the interval set at 30 seconds:

   <script type="text/javascript" language="javascript">
      $(document).ready(function () {
         setInterval(function () {
            var grid = $("#GridName").data("kendoGrid");
            grid.dataSource.read();
         }, 30000);
      });
   </script>
sonyisda1
  • 422
  • 1
  • 8
  • 21
2

The default/updated configuration/data of the widgets is set to automatically bind to an associated DataSource.

$('#GridId').data('kendoGrid').dataSource.read();
$('#GridId').data('kendoGrid').refresh();
parvezalam khan
  • 480
  • 2
  • 11
  • Was there something wrong with the accepted answer (from 2013), since your answer looks so similar. You should at least comment that some way -- and the comments in that answer even say that you should not call `refresh` – James Z Feb 02 '17 at 19:02
1

The easiest way out to refresh is using the refresh() function. Which goes like:

$('#gridName').data('kendoGrid').refresh();

while you can also refresh the data source using this command:

$('#gridName').data('kendoGrid').dataSource.read();

The latter actually reloads the data source of the grid. The use of both can be done according to your need and requirement.

MBT
  • 21,733
  • 19
  • 84
  • 102
1

I see that a lot of answers here suggest calling both dataSource.read and grid.refresh, however, internally the grid listens for dataSource changes and upon a change it will refresh itself. In other words executing both dataSource.read and grid.refresh will result in refreshing the grid twice, which is unnecessary. Calling just dataSource.read is enough.

Georgi Yankov
  • 411
  • 2
  • 9
0

common js function for refresh kendo grid

function refreshKendoGrid(id) {
var grid = $("#" + id).data("kendoGrid");
if (grid) {
    grid.dataSource.read();
}

}

0

Piling on with an observation. Zachary Dow gave me some interesting to work with. I took at face value the common suggestion of:

var grid = $("#grid");
grid.data("kendoGrid").dataSource.read();
grid.data("kendoGrid").dataSource.refresh();

My experience (YMMV) was that the refresh took place, but the spinner didn't work. Data just refreshed after a 2-second visual desert of nothing. I then tried this:

var grid = $("#grid");
kendo.ui.progress(grid, false);
grid.data("kendoGrid").dataSource.read();
grid.data("kendoGrid").dataSource.refresh();
kendo.ui.progress(grid, true);

That accomplished nothing.

Then, thanks to Zachary's observation, I tried something like this.

var grid = $("#grid");
kendo.ui.progress(grid, true);
grid
  .data("kendoGrid")
  .dataSource
  .read()
  .then(function() {
    kendo.ui.progress(grid, false);
  });
grid.data("kendoGrid").refresh();
}

(I'm going by memory here, so a step in this progress might be off.)

That had the interesting effect of producing two spinners.

At this point I tried another person's answer from this post, and removed all the bling. (And I'll get to my point.)

var grid = $("#grid");
grid.data("kendoGrid").dataSource.read();

That did the trick. So this is a long story with the same punchline. My problem turned out to be the refresh() call. Remove it, and I got a refresh with a function spinner.

Wellspring
  • 1,128
  • 1
  • 10
  • 19
-2

My solution is:

var gridObj = $('#GridName').data('kendoGrid');
gridObj.dataSource.read();
gridObj.refresh();

Works also for other object functions

Severin
  • 183
  • 2
  • 15
-3
$("#grd").data("kendoGrid").dataSource.read();
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • 1
    While this is at least no 1to1 copy paste, it provides no additional information. Almost every answer in this post with more than one upvote recommended using `dataSource.read()` – Fabian N. Jul 21 '18 at 08:35
-3

$('#GridName').data('kendoGrid').dataSource.read(); //first you have to read the datasource data $('#GridName').data('kendoGrid').refresh(); // after that you can refresh

Ali Soussi
  • 1
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '21 at 12:37
  • Isn't it the same solution as in [the accepted answer](https://stackoverflow.com/a/18399994/2227743)? – Eric Aya Nov 19 '21 at 13:28