javascript - Why are my ng-repeat items not filtering as expected here? -
plnkr: http://plnkr.co/edit/q34j9szbleggc4jkcnum?p=preview
i have simple array called tags, contains 4 objects tweets value , vol value.
when click order tweets, or order vol buttons expect numbers make sense, going high low or low high, numbers out of order.
markup:
<div class="sidebar" ng-controller="sidebar"> <header class="my-header"> <button ng-click="reorderbytweets()">order tweets</button> <button ng-click="reorderbyvol()">order vol</button> </header> <ul> <li ng-repeat="t in tags" ng-mouseleave="leavetag(t)"> <div class="tag-container"> <div class="tag border1" ng-mouseover="showtagdetails(t)">{{t.name}} | tweets: {{t.tweets}} | vol: {{t.vol}}</div> <tag-details tag="t"></tag-details> </div> </li> </ul> </div>
javascript:
$scope.tags.push( { name: 'item 1 ', tweets: '412', vol: '50' }, { name: 'item 2 ', tweets: '10', vol: '500' }, { name: 'item 3 ', tweets: '67', vol: '5' }, { name: 'item 4 ', tweets: '0', vol: '30' } );
the functions:
function reorderbytweets() { console.log('reorderbytweets'); vs.orderreverse = !vs.orderreverse; $scope.tags = $filter('orderby')($scope.tags, 'tweets', vs.orderreverse); console.log('vs.orderreverse = ', vs.orderreverse); // true or false console.log('$scope.tags = ', $scope.tags); } function reorderbyvol() { console.log('reorderbyvol'); vs.orderreverse = !vs.orderreverse; $scope.tags = $filter('orderby')($scope.tags, 'vol', vs.orderreverse); console.log('vs.orderreverse = ', vs.orderreverse); // true or false console.log('$scope.tags = ', $scope.tags); }
as can see screenshot below, while ordering vol. item vol:5
should come last after vol:30
update: looks it's filtering on first digit in numbers? why not take entire number?
filtering tweets clicked once:
filtering tweets clicked 2nd time:
you're ordering string value done alphabetical order.
'10' '5' '30'
the string order of 10 / 30 / 5.
changing 'vol' & 'tweets' numbers:
$scope.tags.push( { name: 'item 1 ', tweets: 412, vol: 50 }, { name: 'item 2 ', tweets: 10, vol: 216 }, { name: 'item 3 ', tweets: 67, vol: 15 }, { name: 'item 4 ', tweets: 0, vol: 30 } );
sorts tweets correctly.
Comments
Post a Comment