To determine if a column is empty, you need some kind of column configuration that is created by iterating over the data to see if all rows contain data for any of the headers (object keys).
Then you can use this column configuration array as a repeater for <th> and <td> .
Configuration Example:
[ { "heading": "Id", "display": true }, { "heading": "Title", "display": true }, { "heading": "Description", "display": true }, { "heading": "Test", "display": false } ]
HTML
<thead> <tr> <th ng-repeat="col in colConfig" ng-if="col.display">{{col.heading}}</th> </tr> </thead> <tbody> <tr ng-repeat="item in data"> <td ng-repeat="col in colConfig" ng-if="col.display"> {{item[col.heading]}} </td> </tr> </tbody>
Config create example
var keys = Object.keys(data[0]); function createConfig() { var validKeyCounts = {}; var colConfig; keys.forEach(function (key) { validKeyCounts[key] = 0; }) data.forEach(function (row, idx) { keys.forEach(function (key) { if (row.hasOwnProperty(key) && row[key] !== null) { validKeyCounts[key]++; } }) }); colConfig = keys.map(function (key) { return { heading: key, display: validKeyCounts[key] > 0 } }); return colConfig }
I'm sure it can be optimized, but it's just a way to get started with the functionality.
Demo
source share