I have two tables. One that is working and one that is not working.
My goal is : I want to fix the table that is not working.
The only difference between them is that in the working table the header values are hard coded.
Below is the code :
<!-- Not working table -->
<table style="margin-left:100px">
<thead>
<tr>
<th style="width:100px;font-weight:bold;cursor:pointer"
ng-repeat="value in field_names" ng-click="orderByField='{{value}}'; reverseSort = !reverseSort">
{{value}} <span ng-show="orderByField == '{{value}}'">
<span ng-show="!reverseSort">^</span>
<span ng-show="reverseSort">v</span>
</span>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in dataArray|orderBy:orderByField:reverseSort">
<td>{{emp.a}}</td>
<td>{{emp.b}}</td>
<td>{{emp.c}}</td>
</tr>
</tbody>
</table>
<!-- working table -->
<table class="table table-bordered">
<thead>
<tr>
<th ng-click="orderByField='firstName'; reverseSort = !reverseSort">
First Name <span ng-show="orderByField == 'firstName'">
<span ng-show="!reverseSort">^</span>
<span ng-show="reverseSort">v</span></span>
</th>
<th ng-click="orderByField='lastName'; reverseSort = !reverseSort">
Last Name <span ng-show="orderByField == 'lastName'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span>
</th>
<th ng-click="orderByField='age'; reverseSort = !reverseSort">
Age <span ng-show="orderByField == 'age'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in data.employees|orderBy:orderByField:reverseSort">
<td>{{emp.firstName}}</td>
<td>{{emp.lastName}}</td>
<td>{{emp.age}}</td>
</tr>
</tbody>
and the javascript is :
$scope.field_names= [];
$scope.table_data={};
$scope.orderByField;
$scope.dataArray = [{
a: "Hans",
b: "Mueller",
c: "Leipzig"
}, {
a: "Dieter",
b: "Zumpe",
c: "Berlin"
}, {
a: "Bernd",
b: "Danau",
c: "Muenchen"
}];
$scope.data = {
employees: [{
firstName: 'John',
lastName: 'Doe',
age: 30
},{
firstName: 'Frank',
lastName: 'Burns',
age: 54
},{
firstName: 'Sue',
lastName: 'Banter',
age: 21
}]
};
$scope.reverseSort = false;
function getHeaders(){
$scope.field_names=Object.keys($scope.dataArray[0]);
}
getHeaders();