Get record count in Kendo Grid after dataSource.read Get record count in Kendo Grid after dataSource.read jquery jquery

Get record count in Kendo Grid after dataSource.read


According to the API Reference here

the dataSource has a total() function. So you should be able to do the following, in theory:

function refreshData(){        var grid = $("#SearchWindowGrid").data("kendoGrid");        grid.dataSource.read();        var count = grid.dataSource.total();        $("#countElement").val(count);    }


I found that when you request the .total() after a .read() function the grid would not be really refreshed, even if you call .refresh() right after the read function. By defining a change event, the following would make getting the total more elegant and accurate:

@(Html.Kendo().Grid(Model)  .Name("SearchWindowGrid")  ...       .DataSource(dataSource => dataSource    .Ajax()    .Read(read => read.Action("PopulateSearchWindow", "Item").Data("additionalSearchWindowInfo"))    .Events(ev => ev.Error("onErrorSearchWindow").Change("OnGridChange"))  ))

with the following scripts:

function refreshData(){    var grid = $("#SearchWindowGrid").data("kendoGrid");    grid.dataSource.read();    grid.refresh();}function OnGridChange() {    var grid = $("#SearchWindowGrid").data("kendoGrid");    var count = grid.dataSource.total();    $("#countElement").val(count);}


The gardarvalur code works fine too:

 var gridElements = $("#MyGri").data("kendoGrid").dataSource; gridElements.fetch(function () {var total = gridElements.total(); })