jQuery 在 dataSource.read 之后获取 Kendo Grid 中的记录数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16901798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get record count in Kendo Grid after dataSource.read
提问by gardarvalur
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> <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.
我将 TODO 放在 jQuery 函数中的位置,我希望能够获取行数并将该数字推送到页面上的特定元素中。
回答by Quinton Bernhardt
According to the API Reference here
根据此处的 API 参考
the dataSource has a total() function. So you should be able to do the following, in theory:
dataSource 有一个 total() 函数。因此,理论上您应该能够执行以下操作:
function refreshData(){
var grid = $("#SearchWindowGrid").data("kendoGrid");
grid.dataSource.read();
var count = grid.dataSource.total();
$("#countElement").val(count);
}
回答by Shadi
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:
我发现当您在 .read() 函数之后请求 .total() 时,网格不会真正刷新,即使您在 read 函数之后立即调用 .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"))
)
)
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);
}
回答by Hernaldo Gonzalez
The gardarvalurcode works fine too:
该gardarvalur代码工作正常,太:
var gridElements = $("#MyGri").data("kendoGrid").dataSource;
gridElements.fetch(function ()
{var total = gridElements.total(); })