angularJs实现排序效果

<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>

posted @ 2017-11-21 21:08  烟雨扶苏  阅读(1664)  评论(0编辑  收藏  举报