angularJS 过滤器练习

    本例展示产品列表,使用angular过滤器 orderBy对产品进行排序,具体是点击列标题时交替进行正序倒序排列;同时使用angular过滤器 filter 可根据输入的产品名称搜索出对应产品,实现模糊查询。

页面运行如下:


输入"pad" 则自动显示相关产品,点击“产品价格”按价格低到高排序:


html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../vendor/bootstrap3/css/bootstrap.css">
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script src="app/index2.js"></script>
    <style>
        .redColor{  color:red;  }
    </style>
</head>
<body>

<div class="container" ng-app="product" ng-controller="productController">
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <form class="navbar-form navbar-left" role="search">
                    <div class="form-group">
                        <input type="text" ng-model="search" class="form-control" placeholder="产品名称">
                    </div>
                </form>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
    <table class="table">
        <thead>
        <tr>
            <th ng-click="sortProduct('id')"  ng-class="{ dropup : orderSign=='' }">
                产品编号
                <span ng-class="{ redColor : orderColumn=='id' }" class="caret"></span>
            </th>
            <th ng-click="sortProduct('name')" ng-class="{ dropup : orderSign=='' }">
                产品名称
                <span ng-class="{ redColor : orderColumn=='name' }" class="caret"></span>
            </th>
            <th ng-click="sortProduct('price')" ng-class="{ dropup : orderSign=='' }">
                产品价格
                <span ng-class="{ redColor : orderColumn=='price' }" class="caret"></span>
            </th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="item in productList | filter:{'name':search} | orderBy:(orderSign + orderColumn) ">
            <td>
                {{item.id}}
            </td>
            <td>
                {{item.name}}
            </td>
            <td>
                {{item.price | currency:'(RMB)'}}
            </td>
        </tr>
        </tbody>
    </table>

</div>

</body>
</html>

index2.js
angular.module('product',[])
    .factory('productList',function(){
        return [
            { id:910,name:"imac",price:15400 },
            { id:80,name:"iphone",price:5400 },
            { id:29,name:"ipad",price:1420 },
            { id:500,name:"ipad air",price:2340 },
            { id:1200,name:"ipad mini",price:2200 }
        ]
    })
    .controller('productController',function($scope,productList){
        $scope.productList=productList;

        $scope.orderColumn='name'; //排序字段
        $scope.orderSign='-';      //为空时正序 为负号时倒序

        $scope.sortProduct=function(sortColumn){  //点击列标题排序事件
            $scope.orderColumn=sortColumn;
            if($scope.orderSign=="-"){
                $scope.orderSign="";
            }else{
                $scope.orderSign='-';
            }
        };

    });

相关指令:

ng-class   示例: ng-class=" {  redColor : bool表达式  } "  ,redColor 是一个css样式类

                 如果bool表达式值为真,则添加这个样式 redColor

重点:

<tr ng-repeat=" item in productList  |  filter : {'name':search}  |   orderBy : '-price' ">

search是搜索框输入数据的模型  ,  

 |   filter : {'name':search}  指明:根据搜索框的输入数据过滤产品名称 ,

 |   orderBy : '-price'   指明:根据产品价格由高到低排序

    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值