mvccontrib grid - How to add <tr> id

故事扮演 提交于 2019-12-14 04:20:01

问题


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

}


回答1:


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>



回答2:


You can always show hide columns without adding id to particular row or columns like below

$(".mvcGridDollarHeader th:nth-child(16)").hide();
$(".mvcGrid td:nth-child(16)").hide();

Where mvcGrid is tableStyle and mvcGridDollarHeader is header style.

@grid1.GetHtml(
    tableStyle: "mvcGrid",
    displayHeader: true,
    emptyRowCellValue: "",
    headerStyle: "mvcGridDollarHeader",


来源:https://stackoverflow.com/questions/9430538/mvccontrib-grid-how-to-add-tr-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!