mvccontrib grid - 如何添加; ID

发布于 2025-01-08 20:27:25 字数 1249 浏览 1 评论 0原文

我想向我构建的 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>()));

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么这么可爱啊 2025-01-15 20:27:25

您可以转换模型以添加行索引,然后使用 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>

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>
吝吻 2025-01-15 20:27:25

您始终可以显示隐藏列,而无需向特定行或列添加 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",

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",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文