شبكة MVCContrib - كيفية إضافة ID
-
12-11-2019 - |
سؤال
أريد إضافة معرف لعناصر "TR" من شبكة MVCContrib I Build: giveacodicetagpre.
حتى إذا كان الجدول يحتوي على 10 صفوف، فإن المعرفات هي 0 إلى 9.
طريقة واحدة هي إضافة عنصر إضافي إلى كولي لتخزين هذه القيمة ثم قم بإنشاء هذا كعمود مخفي مع المعرف كقيمة هذا العنصر - غير أنيق للغاية.
هل هناك طريقة أكثر أناقة للقيام بذلك؟ شكرا
لقد حصلت على هذا الحد الآن ولكن الآن يشكو من خط Renderusing أو أي أفكار؟ giveacodicetagpre.
}
المحلول
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>
نصائح أخرى
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",