19

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.

4

3 回答 3

36

根据此处的 API 参考

dataSource 有一个 total() 函数。所以理论上你应该能够做到以下几点:

function refreshData(){
        var grid = $("#SearchWindowGrid").data("kendoGrid");
        grid.dataSource.read();
        var count = grid.dataSource.total();
        $("#countElement").val(count);
    }
于 2013-06-03T17:06:24.670 回答
12

我发现当您在 .read() 函数之后请求 .total() 时,即使您在读取函数之后立即调用 .refresh() ,网格也不会真正刷新。通过定义一个更改事件,以下内容将使总数变得更加优雅和准确:

@(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"))
  )
)

使用以下脚本:

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);
}
于 2013-12-23T05:04:38.430 回答
0

gardarvalur代码也可以正常工作

 var gridElements = $("#MyGri").data("kendoGrid").dataSource;
 gridElements.fetch(function ()
 {var total = gridElements.total(); })
于 2015-06-08T12:42:16.483 回答