angularjs - Angular OrderBy by object property in sub array -
i have table...
<table width="100%"> <thead> <tr ng-repeat="row in data.rows | limitto:1"> <th ng-repeat="col in row.columns" ng-show="{{col.visible}}" ng-click="updatesortorder(col.headertext)"> <a href>{{ col.headertext }}</a> </th> </tr> </thead> <tbody> <tr ng-repeat="row in data.rows | orderby: ??? "> <td ng-repeat="col in row.columns" ng-show="{{col.visible}}"> {{ col.value }} </td> </tr> </tbody> </table>
and have json file following data structure...
"rows": [{ "columns": [{ "headertext": "policy number", "value": "a123456789", "visible": true }, { "headertext": "vrm", "value": "xx12xxx", "visible": true }, { "headertext": "event date", "value": "2014-12-08 08:39:38.000", "visible": true }, { "headertext": "road sl", "value": 30, "visible": true }, { "headertext": "speed", "value": 39, "visible": true }] }
i able sort data in table corresponding "value" property when "headertext" clicked. if user clicked on "policy number" header on table, table sort column object "policy number" in it's "headertext" property objects "value" property. (hope makes sense!?)
how can go doing this? i'm new angular, please gentle :)
edit:: following advice given in first answer , it's not quite working.
my updated code follows...
<div ng-controller="reportscontroller"> <h1>{{ data.reporttitle }}<small ng-show="predicate !== null">ordered by: {{data.rows[0].columns[predicate].headertext }} {{reverse ? "desc" : "asc"}}</small></h1> <div>{{ error }}</div> <table width="100%"> <thead> <tr ng-repeat="row in data.rows | limitto:1"> <th ng-repeat="col in row.columns" ng-show="{{col.visible}}" ng-click="updatesortorder($index)"> <a href>{{ col.headertext }}</a> </th> </tr> </thead> <tbody> <tr ng-repeat="row in data.rows | orderby:[predicate]:reverse "> <td ng-repeat="col in row.columns" ng-show="{{col.visible}}"> {{ col.value }} </td> </tr> </tbody> </table>
and controller...
var reportscontroller = function ($scope, $http, $routeparams) { $scope.data = {}; var ondataretreived = function (response) { $scope.data = response.data; } var onerror = function (reason) { controller.error = "could not fetch data."; } $http({ method: 'get', url: 'data/report.json' }) .then(ondataretreived, onerror); $scope.predicate = 0; $scope.reverse = true; $scope.updatesortorder = function (predicate) { $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; $scope.predicate = predicate; }; }; angular.module('portal').controller('reportscontroller', ['$scope', '$http', '$routeparams', reportscontroller]);
the sorting apears change, it's not sorting on value of column click on. ideas?
you can use orderby filter
change predicate on updatesortorder:
$scope.predicate = 'age'; $scope.reverse = true; $scope.updatesortorder = function(predicate) { $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; $scope.predicate = predicate; };
and use predicate on ng-repeat:
<tr ng-repeat="row in data.rows | orderby:predicate:reverse "> <td ng-repeat="col in row.columns" ng-show="{{col.visible}}"> {{ col.value }} </td> </tr>
Comments
Post a Comment