I have a table that is generated dynamically using Angular 1.6. The data comes from a web API in JSON format. Here is a sample of the data.
{
"CallData": [
{
"TableHeaders": [
{
"id": "0",
"text": "Time"
},
{
"id": "1",
"text": "January"
},
{
"id": "2",
"text": "February"
}
],
"TableRows": [
{
"id": "0",
"cells": [
{
"id": "0",
"text": "7:00"
},
{
"id": "1",
"text": "0"
},
{
"id": "2",
"text": "0"
}
]
},
{
"id": "1",
"cells": [
{
"id": "0",
"text": "8:00"
},
{
"id": "1",
"text": "18"
},
{
"id": "2",
"text": "83"
}
]
}
]
}
]
}
Here is my HTML that is generating the table:
<table>
<thead>
<tr>
<th ng-repeat="header in data.TableHeaders">
{{header.text}}
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in data.TableRows">
<td ng-repeat="cell in row.cells">
{{cell.text}}
</td>
</tr>
</tbody>
</table>
I want to be able to sort my data when I click on the headers. However, I have so far tried to set a sort variable to the id of the headers, using ng-click, and I can see that it updates the variable, but it doesn't sort the data. This was using ng-repeat="row in data.TableRows | orderBy: sortVar"
My sorting code:
// Initialise variable
$scope.sortVar = '0';
<!-- HTML snippet -->
<th ng-repeat="header in data.TableHeaders" ng-click="updateSort(header.id)"></tr>
// function
$scope.updateSort = function(id) {
$scope.sortVar = id;
}
Is there any way at all to be able to sort my dynamic tables? Am I going to need to add another value to my JSON data in order to do this?
I'm relatively new to angular, so not 100% sure how to achieve this.
If anything is unclear, let me know and I'll try to explain it a bit better.
Cheers
softVar.