4

I have a table for which should display striped rows, but since I have nested ng-repeats, my output has groups of rows colored the same instead of striped. Any way to get the output I'm looking for?

<table class="table table-striped table-bordered table-hover table-condensed">  
 <tr ng-repeat-start="thing in app.things">
  <td>{{thing.label}}</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
 </tr>
 <tr ng-repeat-start="action in thing.actions">
  <td>{{thing.label}}</td>
  <td>{{action.label}}</td>
  <td>&nbsp</td>
 </tr>
 <tr ng-repeat-end ng-repeat="task in action.tasks">
  <td>{{thing.label}}</td>
  <td>{{action.label}}</td>
  <td>{{task.label}}</td>
 </tr>
 <tr ng-repeat-end></tr></tr>
</table>
user3918569
  • 83
  • 1
  • 1
  • 7
  • Check out this answer. [How to Assign Alternate Class To Rows](http://stackoverflow.com/questions/12179455/how-to-assign-alternate-class-to-rows-in-angular-js) – Zack Argyle Aug 07 '14 at 22:33
  • Simplest answer: use `ng-if="false"` on the `ng-repeat-end` row. See my answer below. – Sean Feb 09 '18 at 11:48

5 Answers5

10

First Option - Using CSS:

.table-striped > tbody > tr:nth-child(odd) > td {
    background-color: #000;
}

.table-striped > tbody > tr:nth-child(even) > td {
    background-color: #F00;
}

Second Option - Using CSS and Angular JS use the ng-class-odd="odd" and ng-class-even="even" to add a css class to each tablerow, for example:

 <tr ng-repeat-start="action in thing.actions" ng-class-odd="odd" ng-class-even="even">
      <td>{{thing.label}}</td>
      <td>{{action.label}}</td>
      <td>&nbsp</td>
 </tr>

And your CSS add the odd and even class, for example:

.odd { background-color: #000; }
.even { background-color: #F00; }

I hope help you.

Tiago Barreto
  • 822
  • 13
  • 31
  • Using your suggestion is not giving me the output I'm expecting. See here http://plnkr.co/edit/3pqsccyOMGMPW0pxD5mc?p=preview – user3918569 Aug 08 '14 at 14:25
1

This isnt really an angular question, it is a css question. I would have to question your data structure if this is the way you have to make tables. But what you can do is place all the grouped rows into a tbody tag then create a little custom styles to apply the stripe on tbody. Drop the table-striped class and then you could do something like this:

<tbody ng-repeat="action in thing.actions">
    <tr>
        <td>{{thing.label}}</td>
        <td>{{action.label}}</td>
        <td>&nbsp</td>
    </tr>
    <tr ng-repeat-start="action in thing.actions">
        <td>{{thing.label}}</td>
         <td>{{action.label}}</td>
         <td>&nbsp</td>
    </tr>
    <tr ng-repeat-end ng-repeat="task in action.tasks">
         <td>{{thing.label}}</td>
         <td>{{action.label}}</td>
         <td>{{task.label}}</td>
   </tr>
   <tr>
        <td>{{thing.label}}</td>
        <td>{{action.label}}</td>
        <td>{{task.label}}</td>
    </tr>
</tbody>

tbody:nth-child(odd) {
    background-color: #f2f2f2;
}
Jordan Papaleo
  • 1,463
  • 11
  • 24
1

In addition to Tiago answer - you must remove "table-striped" from the table class, because it overriding the ng-class-odd / ng-class-even CSS classes

eladc
  • 243
  • 2
  • 6
0

Try to add tbody element, like

<table class="table table-striped table-bordered table-hover table-condensed">  
 <tbody>
    <tr ng-repeat-start="thing in app.things">
     ....

and add this rule to the CSS:

.table-striped > tbody > tr:nth-of-type(odd) {
  background-color: #f9f9f9;
}
dubek
  • 11,447
  • 5
  • 30
  • 23
Calmont
  • 48
  • 6
0

The ng-repeat-end row is causing the stripe issue.

You need the row for AngularJs to do the repeating, but you don't want it for the browser/Bootstrap/css to alternate the styling.

So simply remove it from the table once Angular has used it:

<tr ng-repeat-end ng-if="false"></tr>
Sean
  • 14,359
  • 13
  • 74
  • 124