0

I am populating a table like the following using angularjs:

<table>
  <thead>
    <tr>
      <th>col1</th>
      <th>col2</th>
      <th>col3</th>
      <th>col4</th>
      <th>col5</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows">
      <td>{{row.col1}}</td>
      <td>{{row.col2}}</td>
      <td>{{row.col3}}</td>
      <td>{{row.col4}}</td>
      <td>{{row.col5}}</td>
    </tr>
  </tbody>
</table>

because the table has a lot of columns which often contain no values (NULL), I would like to hide those columns.
note: all rows would have that column value NULL.

Example (row.col1=NULL, row.col3=NULL):

<table>
  <thead>
    <tr>
      <th>col2</th>
      <th>col4</th>
      <th>col5</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows">
      <td>{{row.col2}}</td>
      <td>{{row.col4}}</td>
      <td>{{row.col5}}</td>
    </tr>
  </tbody>
</table>

so far I was not able to find/figure out a solution for this.

I'm starting to believe that this is not possible to do...

udo
  • 4,832
  • 4
  • 54
  • 82

2 Answers2

1

You could a global array that has a boolean variable for each column you want, and set that boolean accordingly.

 $scope.showColumn = [false, false, false, false];

Use a function to display your column value that way you get to set your booleans in case you encounter a non-null value.

  $scope.setVisible = function(colNum, colVal){
    if(colVal)
      $scope.showColumn[ colNum ] =true;
    return colVal;
  };

You'll then have to check the boolean for display and use the function for your column value:

<tr ng-repeat="friend in friends">
     <td ng-show="showColumn[0]">{{ setVisible(0, friend.name ) }}</td>
     ...

See Working plunk (try giving a 'none' property to any friend)

dev8080
  • 3,950
  • 1
  • 12
  • 18
0

This is very easy to do in an ng-repeat. Just use ng-if.

<table>
  <thead>
    <tr>
      <th>col2</th>
      <th>col4</th>
      <th>col5</th>
    </tr>
  </thead>
  <tbody>
    // ADD NG-IF TO YOUR REPEATER
    <tr ng-repeat="row in rows" ng-if="row.col1 != null">
      <td>{{row.col2}}</td>
      <td>{{row.col4}}</td>
      <td>{{row.col5}}</td>
    </tr>
  </tbody>
</table>