我想为我构建的mvccontrib网格的"tr"元素添加一个id:
<tr id="0"/>
<tr id="1"/>
因此,如果表包含10行,则id为0到9。
一种方法是向我的实体添加一个额外的项来存储这个值,然后将其创建为一个隐藏列,id作为这个项的值——这不是很优雅。
有更优雅的方法吗?感谢
我已经走了这么远,但现在它在RenderUsing线上抱怨,有什么想法吗?
@model IEnumerable<Tens.Models.UserPreviousNamesView>
<div class="demo_jui">
@{
var userId = 0;
foreach (var item in Model)
{
userId = item.Id;
break;
}
@(Html.Grid(Model.Select((item,index) => new { Item = item, Index = index}))
.Columns(col =>
{
col.For(p => p.Item.Title);
col.For(p => p.Item.Name);
col.Custom(@<text>
@Ajax.ActionLink("Delete", "DeleteUserPreviousName", "Summary", null, null, new { id = item.Item.Id, @class = "deleteUserPreviousName" })
</text>).Encode(false);
})
.RowAttributes(p => new Hash(Id => "id"+p.Item.Index.ToString()))
.Attributes(Id => "userPreviousNamesTable")
.Empty("You currently have no Previous Names.")
.RenderUsing(new Tens.GridRenderers.UserPreviousNamesGridRenderer<Tens.Models.UserPreviousNamesView>()));
}
您可以转换模型,为其添加行索引,然后使用RowAttributes
方法:
@model IEnumerable<MyViewModel>
@(Html
.Grid(Model.Select((item, index) => new { Item = item, Index = index }))
.Columns(column =>
{
column.For(x => x.Item.Foo);
column.For(x => x.Item.Bar);
})
.RowAttributes(x => new Hash(id => string.Format("id{0}", x.Item.Index)))
)
此外,我已经用id
关键字预挂了id,因为HTML中的id不能用数字统计,如您的示例所示。
样本输出:
<table class="grid">
<thead>
<tr>
<th>Foo</th>
<th>Bar</th>
</tr>
</thead>
<tbody>
<tr id="id0" class="gridrow">
<td>foo 1</td>
<td>bar 1</td>
</tr>
<tr id="id1" class="gridrow_alternate">
<td>foo 2</td>
<td>bar 2</td>
</tr>
<tr id="id2" class="gridrow">
<td>foo 3</td>
<td>bar 3</td>
</tr>
</tbody>
</table>
您可以始终显示隐藏列,而无需向特定行或列添加id,如下面的
$(".mvcGridDollarHeader th:nth-child(16)").hide();
$(".mvcGrid td:nth-child(16)").hide();
其中mvcGrid
是tableStyle,mvcGridDollarHeader
是表头样式。
@grid1.GetHtml(
tableStyle: "mvcGrid",
displayHeader: true,
emptyRowCellValue: "",
headerStyle: "mvcGridDollarHeader",