標籤:
關鍵代碼:html
<th class="col-md-3"><a href="" ng-click="desc(‘2‘,la=!la)">單價</a></th> <th class="col-md-3"><a href="" ng-click="desc(‘3‘,la=!la)">銷售金額</a></th> <th class="col-md-1"><a href="" ng-click="desc(‘4‘,la=!la)">銷售數量</a></th>
其中 la=!la 是用來判斷當前點擊是true還是false
JS代碼
//開始預設為totalnum欄位 按照降序排列
$scope.foodsale =ret.sort(function ( x,y ) { return y.totalnum - x.totalnum;//這樣表示 按照ret【表示接收返回數組】裡面的totalnum欄位 按照降序排列 反之 return x.totalnum - y.totalnum; 為升序
})
$scope.desc= function (fla,bol) { if(fla=="4"){ if(bol==false){ $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return y.totalnum - x.totalnum; }) }else{ console.log("bbb") $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return x.totalnum - y.totalnum; }) } }else if(fla=="3"){//totalmoney if(bol==false){ $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return y.totalmoney - x.totalmoney; }) }else{ $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return x.totalmoney - y.totalmoney; } ) } }else if(fla=="2"){//price if(bol==false){ $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return y.price - x.price; }) }else{ $scope.foodsale =$scope.foodsale.sort(function ( x,y ) { return x.price - y.price; } ) } }
以上代碼 便可實現 雙擊排序
AngularJS 實現 雙擊排序