Goal: Populate a table that has dynamic table headings (retrieved via GET). As such, the values (retrieved via another GET) to be used to populate the table have a header_id that can connect the two:
i.e.
headers = [{name:header1, id:1}, {name:header2, id2}]
list_entries = [{value:'pop', header_id:1},{value:'bop', header_id:3}]
I can't directly use ng-repeat on list_entries to populate the table since I must respect the fact that some table cells will be empty (no value matched to a header).
I want to access $scope.headings so that I can iterate through it and then use logic to match a value to it (compare header_id).
This may seem like a dumb question to others, but I really have tried looking all over for a good approach. I'd really appreciate just being pointed in the right direction with respect to this.
<script>
var list = angular.module('listApps', []);
list.config(function($httpProvider) {
... set defaults ...
});
list.controller('ListCtrl', function ($scope, $http, $timeout){
var table_headings = xxxxxx; //root + ending
/* Pull Table Headings*/
$http.get(table_headings.toString()).success(function(data,status){
$scope.headings = data.response.fields_names;
$scope.status = status;
console.log(data.response.fields_names);
}).error(function(result,status){
$scope.data = result.data || "Request failed";
$scope.status = status;
});
// match table headings to the respective values in order to populate table correctly
$scope.mapping = [];
// At this point, not even focusing on the function; just need to access $scope.headings
angular.forEach($scope.headings, function(value, key){
this.push(key+':'+value);
}, $scope.mapping);
});
</script>
You could probably do it like this too (the idea is to call the properties of your data using the string notation with the ng-repeat directive, ie: item['property'] instead of item.property):
Result [Link to the image]

As you can see, if the field doesn't exist for an object, it is blank.
Template
<table class="table table-bordered">
<thead>
<tr>
<th data-ng-repeat="head in headings">{{head.title}}</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="item in data">
<td data-ng-repeat="head in headings">{{item.id}} {{item[head.id]}}</td>
</tr>
</tbody>
</table>
Controller
function ListCtrl($scope, $http) {
$http.get('api/headings.json').then(function(result) {
$scope.headings = result.data;
});
$http.get('api/data.json').then(function(result) {
$scope.data = result.data;
});
}
Data fields corresponding to the template
[
{"id": "H1", "title": "Heading 1"},
{"id": "H3", "title": "Heading 3"},
{"id": "H5", "title": "Heading 5"}
] /* headings.json */
and
[
{"id": "D1", "H2": "Data H2D1", "H3": "Data H3D1", "H4": "Data H4D1", "H5": "Data H5D1"},
{"id": "D2", "H1": "Data H1D2", "H2": "Data H2D2", "H3": "Data H3D2", "H4": "Data H4D2"},
{"id": "D3", "H1": "Data H1D3", "H2": "Data H2D3", "H3": "Data H3D3", "H4": "Data H4D3", "H5": "Data H5D3"},
{"id": "D4", "H1": "Data H1D4", "H2": "Data H2D4", "H4": "Data H4D4", "H5": "Data H5D4"},
{"id": "D5", "H1": "Data H1D5", "H2": "Data H2D5", "H3": "Data H3D5", "H4": "Data H4D5", "H5": "Data H5D5"}
] /* data.json with missing fields*/