可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to be able to push the record count from my Kendo grid after read (refresh).
Here is my Kendo Grid:
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().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.
回答1:
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); }
回答2:
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); }
回答3:
The gardarvalur code works fine too:
var gridElements = $("#MyGri").data("kendoGrid").dataSource; gridElements.fetch(function () {var total = gridElements.total(); })