javascriptangularjsscopeangularjs-orderby

AngularJS | OrderBy is not an Array


I want to add a sorting capability to a table column, but I get this error:

[orderBy:notarray] Expected array but received: 0

Also, the sorting doesn't work at all. While the up and down arrows change places, and the debugger seems to be getting the right info.

My HTML:

<th class="text-center"><a style="color:black"
    ng-click="sortColumn('severity')">
    Level
    <span ng-show="sortTable('severity')" class="fa fa-caret-down"></span>
    <span ng-show="sortTable('severity', false)" class="fa fa-caret-up"> 
    </span>
    </a>
</th>
<th><a style="color:black" ng-click="sortColumn('getTimeAgo')">
    Timestamp
    <span ng-show="sortTable('getTimeAgo')" class="fa fa-caret-down"> 
    </span>
    <span ng-show="sortTable('getTimeAgo', false)" class="fa fa-caret-up"> 
    </span>
    </a>
</th>

<tbody ng-repeat="t in Ctrl.notifications | filter : search | 
         filter : Ctrl.customFilter track by $index | 
         orderBy:sortingFn:sortReverse"
         ng-style="$index != 0 ? 'border-top:0px' : ''">

The CTRL:

  $scope.sortColumn = (sortBy) => {
    $scope.sortReverse = !$scope.sortReverse;
    $scope.sortBy = sortBy;
  };

  $scope.sortTable = (sortBy, sortReverse = true) =>
    $scope.sortBy === sortBy && $scope.sortReverse === sortReverse;

  $scope.sortAlerts = (alert, sortBy) => {
    switch (sortBy) {
      case 'severity':
        return alert.severity;
      case 'getTimeAgo':
        return alert.getTimeAgo;
      default:
        return alert[sortBy]
    }
  };

  $scope.sortingFn = (alert) => {
    return sortAlerts(alert, $scope.sortBy)
  };

Solution

  • I simply changed the following:

      $scope.sortingFn = (alert) => {
        return sortAlerts(alert, $scope.sortBy)
      };
    

    TO

      const sortingFn = (alert) => {
        return sortAlerts(alert, $scope.sortBy)
      };
    

    Now it works. No errors, nothing... Thanks for the help guys!!