简体   繁体   中英

Telerik grid: retrieve row count with Ajax binding

how can I access grid row count with Ajax binding in Telerik ASP.Net MVC grid? I need to display total in a footer, see code snippet below. The total has to update on insert and delete.

With server binding, there is @Model.Count(). How can I do the same thing with Ajax binding?

thank you!

    @{
    Html.Telerik()
        .Grid<ContractMonth>()
        .Name("contractMonthGrid")
        .DataBinding(dataBinding => dataBinding
            .Ajax()
            .Select("_AjaxBinding", "ContractMonth")
            .Insert("_AjaxInsert", "ContractMonth")
            .Delete("_AjaxDelete", "ContractMonth")
            )
        .DataKeys(keys => keys.Add(c => c.Id))
        .ToolBar(commands => commands.Insert())
        .Columns(columns =>
        {
            columns.Bound(o => o.StartDate).EditorTemplateName("Date").FooterTemplate(@<text>@Model.Count()</text>);

The Telerik MVC grid supports server and Ajax aggregates :

The following aggregates are supported:

  • Average
  • Count
  • Min
  • Max
  • Sum

To specify the aggregates for a column use the Aggregates method

So in your sample:

.Columns(columns =>
    {
        columns.Bound(o => o.StartDate)
               .EditorTemplateName("Date")
               .Aggregate(aggregates => aggregates.Count())
               .FooterTemplate(@<text>@item.Count</text>)
               .ClientFooterTemplate("<#= Count #>");
    }

If you need the row count "outside" the grid you can use the grid's client side API :

<script type="text/javascript">
$(function() {
    var totalRows = $("#contractMonthGrid").data("tGrid").total;
    //do something with totalRows 
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM