angularJs實現排序效果

來源:互聯網
上載者:User

標籤:table   sel   person   --   ice   idt   file   order   put   

<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table border="1" cellspacing="0" cellpadding="5" width="400">
<tr>
<th ng-click="sort($event)">序號</th>
<th ng-click="sort($event)">歌名</th>
<th ng-click="sort($event)">歌手</th>
</tr>
<tr ng-repeat="n in names|orderBy:flag" align="center">
<td>{{n.no}}</td>
<td>{{n.name}}</td>
<td>{{n.singer}}</td>
</tr>
</table>
</div>
<script>
var app=angular.module(‘myApp‘,[]);
app.controller("myCtrl",function($scope){
$scope.names=[
{no:1,name:"在人間",singer:‘王建房‘},
{no:2,name:"非酋",singer:‘薛明媛‘},
{no:3,name:"Wonderwall",singer:‘Oasis‘},
{no:4,name:"我們不一樣",singer:‘大壯‘},
{no:5,name:"童話鎮",singer:‘陳一發兒‘},
{no:6,name:"乘著時光的風",singer:‘萬思妤‘},
{no:7,name:"Supersonic",singer:‘Oasis‘},
{no:8,name:"London Loves",singer:‘Blur‘},
];

$scope.sort=function(e){
//console.log(e);
var th=e.target,
tr=th.parentNode,
ths=tr.getElementsByTagName("th"),
filed="";


if(th.innerText==‘序號‘){
field=‘no‘;
}else if(th.innerText==‘歌名‘){
field=‘name‘;
}else if(th.innerText==‘歌手‘){
field=‘singer‘;
}


for(var i=0;i<ths.length;i++){
if(th!=ths[i]){
ths[i].className=‘‘;
}
}

if(th.className==‘desc‘){
th.className=‘asc‘;
$scope.flag=field;
}else if(th.className==‘asc‘){
th.className=‘desc‘;
$scope.flag="-"+field;
}else{
th.className=‘asc‘;
$scope.flag=field;
}

}

});
</script>
</body>

------------------------------------

進階版  如下

 

<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p><input type="text" ng-model=‘keyword‘></p>
<table border="1" cellspacing="0" cellpadding="5" width="400">
<tr>
<th ng-click="sort($event)">序號</th>
<th ng-click="sort($event)">歌名</th>
<th ng-click="sort($event)">歌手</th>
<th>頭像</th>
<th>性別</th>
</tr>
<tr ng-repeat="n in names|filter:{name:keyword,singer:keyword}" align="center">
<td>{{n.no}}</td>
<td>{{n.name}}</td>
<td>{{n.singer | shenglue}}</td>
<td ng-bind-html="n.face | img"></td>
<td ng-bind-html="n.sex | sex"></td>
</tr>
</table>

<p>姓名:{{mesg | reverse}}</p>
</div>
<script>
var app=angular.module(‘myApp‘,[]);
app.controller("myCtrl",function($scope){
$scope.names=[
{no:1,name:"在人間",singer:‘王建房‘,face:‘images/true.jpg‘,sex:‘男‘},
{no:2,name:"非酋",singer:‘薛明媛‘,face:‘images/true1.jpg‘,sex:‘女‘},
{no:3,name:"Wonderwall",singer:‘Oasis‘,face:‘images/true2.jpg‘,sex:‘男‘},
{no:4,name:"我們不一樣",singer:‘大壯‘,face:‘images/true.jpg‘,sex:‘男‘},
{no:5,name:"童話鎮",singer:‘陳一發兒‘,face:‘images/true1.jpg‘,sex:‘女‘},
{no:6,name:"乘著時光的風",singer:‘萬思妤‘,face:‘images/true.jpg‘,sex:‘女‘},
{no:7,name:"Supersonic",singer:‘Oasis‘,face:‘images/true2.jpg‘,sex:‘男‘},
{no:8,name:"London Loves",singer:‘Blur‘,face:‘images/true.jpg‘,sex:‘男‘},
];
$scope.mesg="上海自來水來自海上!";
});

app.service(‘yinsi‘,function(){ //建立服務
this.star=function(str){
return str.substr(0,1)+"**";
}
});

app.filter(‘shenglue‘,[‘yinsi‘,function(yinsi){
return function(text){
return yinsi.star(text);
}
}]);

app.filter("reverse",function(){
return function(text){
return text.split("").reverse().join("");
}
});

app.filter(‘img‘,[‘$sce‘,function($sce){
return function(text){
return $sce.trustAsHtml(‘<img src="‘+text+‘">‘);
}
}]);

app.filter(‘sex‘,[‘$sce‘,function($sce){
return function(text){
var str=‘<select>\
<option value="男" ‘+(text=="男"?"selected":"")+‘>男</option>\
<option value="女" ‘+(text=="女"?"selected":"")+‘>女</option>\
</select>‘;
return $sce.trustAsHtml(str);
}
}]);
</script>
</body>

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.