MVCCONTRIB-Gitter - Hinzufügen von -Um
-
12-11-2019 - |
Frage
Ich möchte den "TR" -EN-Elementen des MVCCONTRIB-Gitters eine ID hinzuzufügen, die ich baue: generasacodicetagpre. Wenn also die Tabelle 10 Reihen enthält, sind die IDs 0 bis 9.
Eine Möglichkeit ist, meiner Entität einen zusätzlichen Element hinzuzufügen, um diesen Wert zu speichern, und erstellen Sie dies dann als versteckte Spalte mit der ID als Wert dieses Artikels - nicht sehr elegant.
Gibt es eine elegante Möglichkeit, dies zu tun? Danke
Ich habe so weit, aber jetzt beschwert es sich an der renderusing-Linie, irgendwelche Ideen? generasacodicetagpre.
}
Lösung
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>
Andere Tipps
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",