mvccontrib grid - 如何添加< tr> ID [英] mvccontrib grid - How to add <tr> id

查看:233
本文介绍了mvccontrib grid - 如何添加< tr> ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < tr id 

我想为我构建的mvccontrib网格的tr元素添加一个id。 = 0/>
< tr id =1/>

所以如果表格包含10行,则ID为0到9。 b
$ b

一种方法是向我的实体添加一个额外的项目来存储此值,然后将其创建为隐藏列,其中id为此项的值 - 不是非常优雅。



有没有更好的方法来做到这一点?
谢谢



我有这么远,但现在它在RenderUsing线抱怨,有什么想法?

  @model IEnumerable< Tens.Models.UserPreviousNamesView> 

< div class =demo_jui>
@ {
var userId = 0;

foreach(模型中的var项)
{
userId = item.Id;
休息;


$ b @(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(您目前没有以前的名字)
.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不能statr用你的例子中显示的数字。

样本输出:

 < table class =grid > 
< thead>
< tr>
Foo< / th>
Bar< / th>
< / tr>
< / thead>
< tbody>
< tr id =id0class =gridrow>
< td> foo 1< / td>
< td>栏1< / td>
< / tr>
< tr id =id1class =gridrow_alternate>
< td> foo 2< / td>
< td> bar 2< / td>
< / tr>
< tr id =id2class =gridrow>
< td> foo 3< / td>
< td>栏3< / td>
< / tr>
< / tbody>
< / table>


I want to add an id to the "tr" elements of the mvccontrib grid I build:

<tr id="0"/>
<tr id="1"/>

so if the table contains 10 rows, the ids are 0 through to 9.

One way is to add an additional item to my entity to store this value and then create this as a hidden column with the id as the value of this item - not very elegant.

Is there a more elegant way to do this? Thanks

I've got this far but now it complains at the RenderUsing line, any ideas?

@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>()));

}

解决方案

You could transform the model to add it a row index and then use the RowAttributes method:

@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)))
)

Also I have pre-pended the id with the id keyword as ids in HTML cannot statr with a number as shown in your example.

Sample output:

<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>

这篇关于mvccontrib grid - 如何添加&lt; tr&gt; ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆