輕鬆Angularjs實現表格按指定列排序

來源:互聯網
上載者:User

標籤:size   control   line   簡單   derby   under   $scope   ati   .json   

  angular表格點擊序號進行升序,再次點擊進行降序排序,在輸入框輸入資訊,出現相對應資料的那一行。

 

 html:

 

<input type="text" ng-model="search"/><table border="1" cellpadding="0" cellspacing="1" width="400px">    <tr>        <th ng-click="click()">編號</th>        <th ng-click="click1()">姓名</th>        <th ng-click="click2()">年齡</th>    </tr>    <tr ng-repeat="item in data|orderBy:a+b+c|filter:search">        <td>{{item.number}}</td>        <td>{{item.name}}</td>        <td>{{item.age}}</td>    </tr></table>

 

js:

 

var app=angular.module(‘mk‘,[]);        app.controller(‘test‘,function($scope,$http){                $http.get(‘json.json‘).success(function(data){                    $scope.data=data.nr                });            $scope.a=‘number‘;            $scope.b=‘name‘;            $scope.c=‘age‘;            $scope.click=function(){                if($scope.a==‘number‘){                    $scope.a=‘-number‘;                    $scope.b=‘‘;                    $scope.c=‘‘;                }else{                    $scope.a=‘number‘;                    $scope.b=‘‘;                    $scope.c=‘‘;                }            };            $scope.click1=function(){                if($scope.b==‘name‘){                    $scope.a=‘‘;                    $scope.b=‘-name‘;                    $scope.c=‘‘;                }else{                    $scope.a=‘‘;                    $scope.b=‘name‘;                    $scope.c=‘‘;                }            };            $scope.click2=function(){                if($scope.c==‘age‘){                    $scope.a=‘‘;                    $scope.b=‘‘;                    $scope.c=‘-age‘;                }else{                    $scope.a=‘‘;                    $scope.b=‘‘;                    $scope.c=‘age‘;                }            }        })

此方法還需引用json檔案:

{  "nr":[    {"number":34,"name":"Angular","age":24},    {"number":24,"name":"Blue","age":25},    {"number":14,"name":"Fertn","age":23},    {"number":43,"name":"Onfer","age":26},    {"number":36,"name":"Wang","age":21},    {"number":31,"name":"Linla","age":30},    {"number":4,"name":"San","age":28},    {"number":6,"name":"Ring","age":22},    {"number":76,"name":"Cone","age":31},    {"number":32,"name":"Perter","age":32},    {"number":32,"name":"Nert","age":27},    {"number":15,"name":"Ding","age":29}  ]}

 

 

我是用過濾器 orderBy:‘id‘:a+b+c來控制排序是升序還是降序,在點擊時通過判斷他們的來實現我們想要的效果。

你們還有什麼簡單方法麼,拿出來分享一下吧! 

 

輕鬆Angularjs實現表格按指定列排序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.