Complete example and working with angularjs
<head>
<link data-require="[email protected]" data-semver="3.0.3" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<script src="js/teste/angular.min.js"></script>
<script src="js/teste/jquery-1.7.2.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script>
var app = angular.module("app", []);
app.controller('table_controller', function($scope) {
$scope.ordered_columns = [];
$scope.all_columns = [{
"title": "name",
"type": "string",
"checked": true
}, {
"title": "age",
"type": "number",
"checked": true
}, {
"title": "city",
"type": "string",
"checked": true
}, {
"title": "state",
"type": "string",
"checked": false
}, {
"title": "job",
"type": "string",
"checked": false
}];
// i.e. the rows
$scope.data = [{
"name": "aleck",
"age": 33,
"city": "Portland",
"state": "OR",
"job": "developer"
}, {
"name": "john",
"age": 40,
"city": "Portland",
"state": "OR",
"job": "designer"
}, {
"name": "erik",
"age": 34,
"city": "Portland",
"state": "OR",
"job": "sales"
}];
$scope.$watch('all_columns', function() {
update_columns();
}, true);
var update_columns = function() {
$scope.ordered_columns = [];
for (var i = 0; i < $scope.all_columns.length; i++) {
var column = $scope.all_columns[i];
if (column.checked) {
$scope.ordered_columns.push(column);
}
}
};
});
</script>
</head>
<body ng-app="app" ng-controller="table_controller">
<h2>Dynamic table columns with AngularJS!</h2>
<div class="form-group">
<div class="col-sm-12">
<p>Columns</p>
<div ng-repeat="c in all_columns">
<label>
<input type="checkbox" ng-model="c.checked"> {{ c.title }}
</label>
</div>
</div>
</div>
<table class="table table-striped">
<tr>
<th ng-repeat="c in ordered_columns">{{ c.title }}
</th>
</tr>
<tbody>
<tr ng-repeat="d in data">
<td ng-repeat="c in ordered_columns">{{ d[c.title] }}</td>
</tr>
</tbody>
</table>
</body>
</html>