tags:

views:

1419

answers:

3

Probably a dumb question but I'm new to MVC and jQuery. I want to alternate the row colors of my tables and I've decided that I'm going to use jQuery to do it. I know that I could write an extension method (http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx), etc but after reading SH's comment on the article at http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx I've picked jQuery as the solution I want to implement.

I want to implement the method described at http://www.packtpub.com/article/jquery-table-manipulation-part2 but I haven't figured out where to put the initial jQuery call (eg: $(document).ready(function() {...});

Like I said, I'm new to jQuery...

+1  A: 

If you use the jQuery plugin Tablesorter, it has a built-in "widget" for this called "zebra" (among all of its other functionality).

Chad Birch
oh my, thats nice. With sorting and paging thrown in for good measure...
grenade
+10  A: 

You can accomplish this by setting a class on all the even rows of a table.

<html>
    <head>
        <title>Example Title</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(document).ready(function() {
                $('tr:even').addClass('alt-row-class');
            });
        </script>
    </head>
    <body>...</body>
</html>

Then apply style to that class using standard css:

.alt-row-class { background-color: green; }

A commenter correctly points out that you might wish to play around with the selector (tr:even) to get the results you want relative to tbody, thead and tfoot elements.

Ken Browning
I usually apply this to the tbody rows only, with separate classes for thead/tfoot.
tvanfosson
This is what I'm seeing in my google search, but I haven't worked out where that jQuery code goes. My first thought was to add a script tag, but that failed miserably. Where do I include your snippet in my view?
grenade
Thanks for the clarification, do I also need a script reference to jQuery?
grenade
Yes, you do. Sorry for leaving it out. I've added it.
Ken Browning
Got there in the end, cheers!
grenade
language="text/javascript" makes it so that firefox and safari will not run the javascript. language="javascript" works though. You may be thinking of type="text/javascript" I'm not sure.
ScArcher2
Thanks for your edit ScArcher2 :)
Ken Browning
+5  A: 

Be aware that if you use jQuery and the user has javascript turned off, your UI is not going to get styled the way you want. You may want to consider doing the assignment of CSS classes for styling in the view code itself.

   <%  var alternating = false;
       foreach (var row in Model) {
           if (alternating) {
    %>
            <tr class="alternating-row">
    <%
           } else {
    %>
            <tr class="normal-row">
    <%     }
           alternating = !alternating;
     %>

     ...

Even, better would be to use an extension method to the HtmlHelper for rows.

   <%  var alternating = false;
       foreach (var row in Model) {
    %>
      <%=  Html.BeginRow( alternating ) %>
   <%      
           alternating = !alternating;
    %>
       ...
      <%=  Html.EndRow() %>
tvanfosson
<tr class="<%= alternating == true ? "alternating-row" : "normal-row" %>">
Charlie Brown
Never test if a boolean is `== true`, just test the boolean: `<tr class="<%= alternating ? "alternating-row" : "normal-row" %>">`
Stephen P