AngularJs custom sorting wont accept value returned from a function -
i'm calling api company uses returns array of objects, 1 of them looks like:
{ip = 127.0.0.1, connected: 2015-01-01 00:00:00, disconnected: 2015 01-01 01:00:00, traffic:90000}
i able create anugular app produces table ng-repeat
sortable ip
, connected
, , disconnected
. traffic
var number of bytes transmitted during session subtracted dates number of bytes transmitted per second , able display on table table looks below. clicking headers sort table value , clicking again reverse , display appropriate arrow. results
response http post:
js:
$scope.reverseorder = function(predicate) { $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : true ; $scope.predicate = predicate; }; $scope.bytespersecond = function(startdate, enddate, traffic) { var = moment(startdate); var = moment(enddate); var seconds = to.diff(from, 'seconds'); return traffic/seconds; };
html:
<table class="..."> <thead> <th> <a href="" ng-click="reverseorder('ip')">ip address</a> <i ng-class="{'icon-arrow-down' : reverse && predicate==='ip', 'icon-arrow-up' : !reverse && predicate==='ip'}"></i> </th> <th> <a href="" ng-click="reverseorder('connected')">time connected</a> <i ng-class="{'icon-arrow-down' : reverse && predicate==='connected', 'icon-arrow-up' : !reverse && predicate==='connected'}"></i> </th> <th> <a href="" ng-click="reverseorder('disconnected')">time disconnected</a> <i ng-class="{'icon-arrow-down' : reverse && predicate==='disconnected', 'icon-arrow-up' : !reverse && predicate==='disconnected'}"></i> </th> <th> <a href="" ng-click="reverseorder(bytespersecond)">bytes per second</a> <i ng-class="{'icon-arrow-down' : reverse && predicate===bytespersecond, 'icon-arrow-up' : !reverse && predicate===bytespersecond}"></i> </th> </tr> </thead> <tbody> <tr ng-repeat="result in results | orderby:predicate:reverse"> <td style="text-align: left;">{{result.ip}}</td> <td style="text-align: center;">{{result.connected}}</td> <td style="text-align: center;">{{result.disconnected}}</td> <td style="text-align: right;">{{bytespersecond(result.connected,result.disconnected,result.traffic)| number:0}}</td> </tr> </tbody> </table>
the reverseorder
function works ip
, dates not work bytespersecond
function. read in angular documentation orderby
directive can used values functions i'm not sure why isn't working. displays , switches arrow next header , doesn't sort values @ all. how can table sort bytespersecond
function created?
while there way make work using function order, lazy programmer doesn't want find out how it, i'd calculate bytes per second each item , store attribute.
angular.foreach($scope.results, function(value) { value.bytespersecond = $scope.bytespersecond(value.connected, value.disconnected, value.traffic); });
and viola can share code other attributes =)
Comments
Post a Comment