0

I have data like

{ column : [a,b,c], data : [["a",1,2],["b",4,5],["c",3,2]]}

table structure

<thead>
<tr>
  <th ng-repeat="n in column" ng-click="click($index)">n</th>  
</tr>
</thead>
<tr ng-repeat="i in data | orderBy:sortType:sortReverse ">
  <td ng-repeat="j in i">i</td>
</tr>

how to make a sort logic here??

4
  • Can you be a bit more specific? How to you want to sort? Commented Aug 31, 2016 at 8:29
  • @UjjwalOjha sorting when clicked in th (according heading) like usually done in datatables Commented Aug 31, 2016 at 9:46
  • Do you want to sort by index 0 when column 'a' is clicked and index 1 when column 'b' is clicked etc..? Commented Aug 31, 2016 at 10:00
  • @Sarathy exactly yes Commented Aug 31, 2016 at 10:09

1 Answer 1

4

You can try order by with a custom function. An example below. Hope this helps.

angular.module("app", []);
angular.module("app").controller("Test", function($scope) {
  $scope.sortReverse = false;
  $scope.column = ['a', 'b', 'c'];
  $scope.data = [
    ["a", "1", "2"],
    ["b", "4", "5"],
    ["c", "3", "2"]
  ];

  $scope.sort = function(index) {
    $scope.sortColumnIndex = index;
    $scope.sortReverse = !$scope.sortReverse;
  };

  $scope.sortByIndex = function(item) {
    return item[$scope.sortColumnIndex];
  };

});
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body>
  <div ng-controller="Test">
    <table>
      <thead>
        <tr>
          <th ng-repeat="n in column" ng-click="sort($index)">{{n}}</th>
        </tr>
      </thead>
      <tr ng-repeat="i in data | orderBy:sortByIndex:sortReverse">
        <td ng-repeat="j in i">{{j}}</td>
      </tr>
    </table>
  </div>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.