5

my code is working, but I was wondering if there is a better way of injecting empty rows into a table (just for look and feel). It seems a bit redundant, but cannot find the right example how to do it better. my code for now:

<tr class="{{row.class}}" ng-repeat="row in someList | filter:{class:'special'}">
  <td>{{row.name}}</td>
</tr>
<!-- empty row -->
<tr>
  <td>&nbsp;</td>
</tr>
<tr class="{{row.class}}" ng-repeat="row in someList | filter:{class:'other'}">
  <td>{{row.name}}</td>
</tr>
<!-- empty row -->
<tr>
  <td>&nbsp;</td>
</tr>
<tr class="{{row.class}}" ng-repeat="row in someList | filter:{class:'final'}">
  <td>{{row.name}}</td>
</tr>
6
  • Your approach is okay. You can't avoid it unless you create three separate tables.
    – dfsq
    Aug 22, 2015 at 13:34
  • Can use groupBy but requires repeating either a tbody or table as well
    – charlietfl
    Aug 22, 2015 at 13:39
  • You should use a directive for this stuff Aug 22, 2015 at 13:49
  • @MohanSingh ng-repeat is a directive ... so what would your suggested directive do that would be different?
    – charlietfl
    Aug 22, 2015 at 14:01
  • Give me little time, i can create a directive for you. Aug 22, 2015 at 15:13

1 Answer 1

2

I got two different solutions for you:

CSS combined with orderBy

You might might use a single ng-repeat to display all rows in the correct order with orderBy:'class':

<table>
  <tr class="{{row.class}}" ng-repeat="row in someList | orderBy:!'class'">
    <td>{{row.name}}</td>
  </tr>
</table>

With the help of CSS you may then add some visiual space between the three sections:

.other td,  .final td{
  padding-top:1em;
}

.other ~ .other td, .final ~ .final td{
  padding-top:0px;   
}

You can see the result in this plunker

Pure angularJS with groupBy of angular.filter module

For this solution you need to wrap each block into a <tbody> in order to use a nested ng-repeat. You might either style your <tbody> tags to leave some space inbetween or add an empty row as shown below. You can hide this row conditionally to get rid of the empty row at the end of the table.

<table>
  <tbody ng-repeat="(key, value) in someList | groupBy:'class'">
    {{key}}
    <tr class="{{key}}" ng-repeat="row in value">
      <td>{{row.name}}</td>
    </tr>
    <tr ng-if="key != 'final'"><td>&nbsp;</td></tr>
  </tbody>
</table>

This is the corresponding plunker.

1
  • 1
    you could probably also use $last from ng-repeat to check if it's the last entry
    – Aides
    Nov 18, 2016 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.