Note that there are some explanatory texts on larger screens.

plurals
  1. POColor all even items in a different color in a result table shown in a view
    primarykey
    data
    text
    <p>I am building an MVC-app in which items are displayed in a table.</p> <p>Here's an example of table included in a view:</p> <pre><code>&lt;table id="itemsID"&gt; &lt;tr&gt; &lt;th&gt;Obj Name&lt;/th&gt; &lt;th&gt;Number&lt;/th&gt; &lt;th&gt;Amount&lt;/th&gt; &lt;th&gt;Size&lt;/th&gt; &lt;th&gt;Prize&lt;/th&gt; &lt;/tr&gt; @for (int i = 0; i &lt; Model.Count; i++) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(model =&gt; model[i].m_ObjName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(model =&gt; model[i].m_Number) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(model =&gt; model[i].m_Amount) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(model =&gt; model[i].m_Size) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(model =&gt; model[i].m_Prize) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id = Model[i].m_ObjID}) | @Html.ActionLink("Details", "Details", new { id = Model[i].m_ObjID}) | @Html.ActionLink("Delete", "Delete", new { id = Model[i].m_ObjID }) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre> <p>What I am trying to do is color each item in the for loop which number is even / odd. Up to now I've wrote a short javascript which partially works:</p> <pre><code>$("#results tr:even").css("background-color", "#FFFFFF");; </code></pre> <p>Simple, yet evidently it affects ALL the items in the page, not the items displayed in the for loop. How can I target those items specifically?</p> <p><strong>EDIT</strong></p> <p>Based on everyone's kind explanations, I have made modifications to the code up there. Now ALL the even tr in the result list are affected, including the first <code>&lt;tr&gt;</code> right under the <code>&lt;table&gt;</code>, but I would like it to be ignored, how could I do this? I have tried to put the ID solely on the <code>&lt;tr&gt;</code> in the for loop, but it does not work...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload