Get record count in Kendo Grid after dataSource.read

gardarvalur picture gardarvalur · Jun 3, 2013 · Viewed 61.3k times · Source

I want to be able to push the record count from my Kendo grid after read (refresh).

Here is my Kendo Grid:

    @(Html.Kendo().Grid(Model)
      .Name("SearchWindowGrid")
      .Columns(columns =>
          {
              columns.Bound(p => p.SYSTEM_ITEMS_SEGMENT1).Hidden();
          })
      .ClientRowTemplate(
          "<tr>" +
            "<td>" +
                "<span><b>#: SYSTEM_ITEMS_SEGMENT1#</b></span>&nbsp;<br/>" +
                "<span>#: DESCRIPTION# </span>" +
            "</td>" +
          "</tr>"
      )
      .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("PopulateSearchWindow", "Item").Data("additionalSearchWindowInfo"))
        .Events(ev => ev.Error("onErrorSearchWindow"))
      )
      .Selectable(s => s.Enabled(true).Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
      .Scrollable(s => s.Enabled(true).Height(450))
  )

My Controller action:

    public ActionResult PopulateSearchWindow([DataSourceRequest] DataSourceRequest request, string option, string searchText, string searchDesc)
    {
        try
        {
            var derps= _idg.SearchItems(searchText, searchDesc, _adg.OrganizationCode).ToList();

            return Json(derps.ToDataSourceResult(request, ModelState));
        }
        catch (Exception e)
        {
            ModelState.AddModelError("ExceptionErrors", e.Message);
            return Json(new List<Derp>().ToDataSourceResult(request, ModelState));
        }
    }

Here is my function that forces data refresh:

    function refreshData(){
        $("#SearchWindowGrid").data("kendoGrid").dataSource.read();
        //TODO: get the total count and push to #countElement
        var count = $("#SearchWindowGrid").data("kendoGrid").length; //not sure what to do here
        $("#countElement").val(count);
    }

Where I put my TODO in the jQuery function I want to be able to get the number of rows and push that number into a specific elemnt on my page.

Answer

Quinton Bernhardt picture Quinton Bernhardt · Jun 3, 2013

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);
    }