a--
a--

Reputation: 558

AngularJS sort dynamic table

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

Upvotes: 0

Views: 1763

Answers (2)

Hoyen
Hoyen

Reputation: 2519

This sort is a bit complicated since it requires going into nested arrays and objects. Here is a sample of how it can be achieved using JavaScript Array's sort function:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.updateSort = function(index) {

    $scope.data.TableRows = $scope.data.TableRows.sort(function(rowA, rowB) {
      return rowA.cells[index].text.localeCompare(rowB.cells[index].text);
    });
  }
  $scope.sortVar = '0';
  $scope.data = {
    "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": "29"
      }, {
        "id": "2",
        "text": "0"
      }]
    }, {
      "id": "1",
      "cells": [{
        "id": "0",
        "text": "8:00"
      }, {
        "id": "1",
        "text": "18"
      }, {
        "id": "2",
        "text": "83"
      }]
    }]
  }


});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="[email protected]" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <table>
    <thead>
      <tr>
        <th ng-repeat="header in data.TableHeaders" ng-click="updateSort($index)">
          {{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>
</body>

</html>

Upvotes: 2

Related Questions