Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip repeat for some columns in ng-repeat

I wanted to have a table like this using angular repeat

HTML

<table border="1">
  <tr>
    <th>Month</th>
    <th>Savings</th>
    <th>Savings for holiday!</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
    <td rowspan="2">$50</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>

  <tr>
    <td>January</td>
    <td>$100</td>
    <td rowspan="2">$50</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

I have written an Angularjs code for the above as follows.

<table border="1">
  <tr>
    <th>Month</th>
    <th>Savings</th>
    <th>Savings for holiday!</th>
  </tr>
  <tr ng-repeat="row in rows">
    <td>{{row.month}}</td>
    <td>{{row.savings}}</td>
    <td rowspan="2">{{row.holidaysavings}}</td>      
  </tr> 
</table>

The td with rowspan gets repeated using ng-repeat which is not required. How can I avoid this?

like image 473
Ronny K Roy Avatar asked Dec 12 '13 10:12

Ronny K Roy


1 Answers

If you want the last td element only if row.holidaysavings is set (you didn't mention it, but it sounds like it!), you can use ngIf.

ngIf is similar to ngShow with the exception that it removes it from the DOM instead of setting display: none if the expression is falsy (for example 0 or the empty string), which sounds like what you want:

<td rowspan="2" data-ng-if="row.holidaysavings">{{row.holidaysavings}}</td>
like image 153
Steve Klösters Avatar answered Oct 17 '22 20:10

Steve Klösters