功能:实现点击排序,再点击排倒序。

实现方法如下

方法一:定义变量实现点击切换true或false,代码为:

         $scope.lidata = [
                {"name":"Terry","age":12},
                {"name":"Jenifer","age":45},
                {"name":"Garry","age":36},
                {"name":"Tao","age":24},
                {"name":"Lee","age":34},
         ];
     $scope.sortTmp = false;    $scope.sortFn = function(arg){   $scope.sortTmp = !$scope.sortTmp; //在这实现点击的切换   $scope.lidata = $filter('orderBy')($scope.lidata, arg, $scope.sortTmp); }

其中对应的html代码为:

        <table style="margin-left:20px">
            <tr>
                <th ng-click = "sortFn('name')">姓名</th>
                <th ng-click = "sortFn('age')">年龄</th>
            </tr>
            <tr ng-repeat = "data in lidata">
                <td>{{data.name}}</td>
                <td>{{data.age}}</td>
            </tr>
        </table>

方法二:函数也是对象,可以赋属性。

        $scope.sortFn = function(arg){
            arguments.callee["sortFn" + arg] = !arguments.callee["sortFn" + arg]
            $scope.lidata = $filter('orderBy')($scope.lidata,arg,arguments.callee["sortFn" + arg]);
        }    

html代码同上。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2021-10-23
  • 2021-07-18
  • 2021-04-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案