如何在Telerik MVC网格聚合标题中显示非聚合模型值?

时间:2011-11-08 16:10:08

标签: asp.net-mvc-3 telerik-mvc

我的网格:

    @( Html.Telerik().Grid<eGate.BackOffice.Core.Model.UI.EgateMenuRevisionViewData>()
        .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.ParentId)
            .Aggregate(a => a.Count()).ClientGroupHeaderTemplate(Html.ActionLink("Create a Revision for This Menu", "Edit", "Thing", new { menuid = "<#= Key #>" }, null).ToString());
        columns.Bound(c => c.ParentName);
        columns.Bound(c => c.ThingName);

    })

    .Groupable(grouping => grouping.Groups(groups => { 
        groups.Add(c => c.EgateMenu.EgateMenuId);
    }).Visible(false))

这很有效。但它给了我:

Create a revision for this menu
1          Parent 1     Thing 1.1
1          Parent 1     Thing 1.2
1          Parent 1     Thing 1.3
Create a revision for this menu
2          Parent 2     Thing 2.1
2          Parent 2     Thing 2.2
2          Parent 2     Thing 2.3

虽然这有效,但我更喜欢更直观的东西:

Create a thing for parent 1
Thing 1.1
Thing 1.2
Thing 1.3
Create a thing for parent 2
Thing 2.1
Thing 2.2
Thing 2.3

问题1: 为...创建一个东西需要将ParentId传递给actionlink但它需要显示客户端的ParentName,但一次只有一个存在于聚合中。

问题2: 我希望按ID分组而不在结果中显示Id列。但是将列设置为visible(false)会阻止clientgroupheadertemplate。

3 个答案:

答案 0 :(得分:0)

您是否尝试隐藏不需要的列?

@( Html.Telerik().Grid<eGate.BackOffice.Core.Model.UI.EgateMenuRevisionViewData>()
    .Name("Grid")
.Columns(columns =>
{
    columns.Bound(c => c.ParentId).Visible(false);
        .Aggregate(a => a.Count()).ClientGroupHeaderTemplate(Html.ActionLink("Create a Revision for This Menu", "Edit", "Thing", new { menuid = "<#= Key #>" }, null).ToString());
    columns.Bound(c => c.ParentName).Visible(false);
    columns.Bound(c => c.ThingName);

})

答案 1 :(得分:0)

Visible(false)添加到列绑定会抑制整个列本身甚至在客户端html中呈现 - 因此抑制了ClientGroupHeaderTemplate。

我会尝试添加ParentId作为数据键 - 例如

.DataKeys(keys =>
{
     keys.Add(k => k.ParentId);
}

我认为只有在使用内置网格(AJAX或Server)DataBinding时才会有所帮助(至少对于Insert)。然而,使用ActionLink ...我在客户端模板中使用mvc html帮助程序的经验不多 - 但是如果你说orignal示例使用它,那么这样的东西不应该这样吗?

columns.Bound(c => c.ParentId).ClientTemplate("")
        .Aggregate(a => a.Count()).ClientGroupHeaderTemplate(Html.ActionLink("Create a thing for \"<#= ParentName #>\"", "Edit", "Thing", new { menuid = "<#= Key #>" }, null).ToString());

我添加了一个空白的ClientTemplate,我认为它可以工作,因此不会显示ID。

答案 2 :(得分:-1)

根据telerik: 您可以使用以下值指定现在的Aggregate属性:Sum,Min,Max,Last,First,Count,Avg&amp;如果ShowFooter设置为true,则每个GridBoundColumn和网格的自定义将计算这些聚合。如果是自定义聚合,网格将引发事件OnCustomAggregate,您可以使用e.Result设置所需的结果。

因此,请尝试每个GridBoundColumn的First,Last或Custom选项,并将ShowFooter属性设置为false。

<telerik:GridBoundColumn Aggregate="First" DataField="CustomerID" DataType="System.String" 
  HeaderText="CustomerID" SortExpression="CustomerID" UniqueName="CustomerID"> 
</telerik:GridBoundColumn>