AngularJS的Filter的示例詳解
貼上幾個有關(guān)Filter使用的幾個示例。
1. 首先創(chuàng)建一個表格
<body ng-app="app"> <div class="divAll" ng-controller="tableFilter"> <input type="text" placeholder="輸入你要搜索的內(nèi)容" ng-model="key"> <br><br> <table cellspacing="0"> <thead> <tr> <th>名稱</th> <th>價格</th> <th>上架時間</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td>{{g.name}}</td> <td>{{g.price}}</td> <td>{{g.inTime}}</td> <td>{{g.desc}}</td> </tr> </tbody> </table> </div> <script src="js/angular.min.js"></script> <script> var app = angular.module('app',[]); app.controller('tableFilter',function($scope){ $scope.goods = [ {name:"HTML5",price:20,inTime:1488785356895,desc:"HTML5是HTML最新的修訂版本,2014年10月由萬維網(wǎng)聯(lián)盟(W3C)完成標準制定。"}, //時間秒數(shù)由 new Date().getTime();獲得 {name:"JavaScript",price:30,inTime:1488685355895,desc:"JavaScript一種直譯式腳本語言,是一種動態(tài)類型、弱類型、基于原型的語言,內(nèi)置支持類型。"}, {name:"CSS3",price:25,inTime:1468785355895,desc:"CSS即層疊樣式表。"}, {name:"AngularJS",price:50,inTime:1482785355895,desc:"AngularJS 是一款優(yōu)秀的前端JS框架,被用于Google的多款產(chǎn)品當中。。"} ]; } </script>
加上樣式,顯示如圖
2. 看到價格個時間顯示怪怪的,好,修改一下表格。
<td>{{g.name}}</td> <td>{{g.price | currency}}</td> <!--currency:貨幣--> <td>{{g.inTime | date:'yyyy-MM-dd'}}</td> <!--將秒數(shù)改成日期格式 年-月-日--> <td>{{g.desc}}</td>
重新運行
3. 這樣一下,確實不礙眼了。但是描述太長了吧,能不能超過一定字數(shù),就不顯示了,以...結(jié)尾?
好,在表格里加上過濾器,就叫descFilter。字數(shù)顯示。注意別忘了 ' | ' 過濾器符號。
<td>{{g.desc | descFilter : 10}}</td>
然后在js中為descFilter寫上方法
//定義一個過濾器,過濾desc里面的字數(shù),多余十個字的部分省略顯示 app.filter('descFilter',function(){ return function(content,num){ //傳兩個參數(shù),一個對應(yīng)內(nèi)容,一個對應(yīng)長度 if(content.length > num){ content = content.substring(0,num) + "..."; } return content; } });
運行看看
可以了。厲害。
4. 搜索框沒用嗎。別忘了,我們給它附上了ng-model="key",
好,修改一下tr。加上filter條件
<tr ng-repeat="g in goods | filter : key">
.保存運行,在里面搜索內(nèi)容試試呢
。好神奇,好厲害的Filter.
5.不能按價格排序嗎?當然可以。而且不僅升序還能降序。
給價格那個標題加上升降按鈕
<th>價格 <input type="button" ng-show="isAsc" value="▼" ng-click="sort()"> <input type="button" ng-show="!isAsc" value="▲" ng-click="sort()"></th>
。修改一下js
<pre name="code" class="javascript"> $scope.isAsc = false; //定義isAsc變量為false,默認升序; $scope.sort = function(){ $scope.isAsc = !$scope.isAsc; //升降切換 }</pre><br> <p></p> <pre></pre>
4 。更新一下過濾排序條件<br>
<pre name="code" class="html"><tr ng-repeat="g in goods | filter : key | orderBy : 'price' : isAsc"></pre><br> <p></p> <p>再次運行。升序降序都可以。大功告成!</p> <p><img src="http://img.blog.csdn.net/20170306190351615" alt=""><br> </p> <p><img src="http://img.blog.csdn.net/20170306190425762" alt=""><br> </p>
以上所述是小編給大家介紹的AngularJS的Filter的示例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用AngularJS對表單提交內(nèi)容進行驗證的操作方法
AngularJS是一款優(yōu)秀的前端JS框架,已經(jīng)被用于Google的多款產(chǎn)品當中。下面通過本文給大家分享使用AngularJS對表單提交內(nèi)容進行驗證的操作方法,需要的的朋友參考下吧2017-07-07從?Angular?Route?中提前獲取數(shù)據(jù)的方法詳解
這篇文章主要介紹了從?Angular?Route?中提前獲取數(shù)據(jù),通過本文,你將學(xué)會使用?resolver,?在?Angular?App?中應(yīng)用?resolver,應(yīng)用到一個公共的預(yù)加載導(dǎo)航,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07AngularJS ng-repeat數(shù)組有重復(fù)值的解決方法
不知道大家是否遇到過這個問題,在當Angular.JS ng-repeat數(shù)組中有重復(fù)項時,系統(tǒng)就會拋出異常,這是該怎么做?本文通過示例代碼介紹了詳細的解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10Angular中Lazy Loading懶加載陷阱避坑最佳實踐
這篇文章主要為大家介紹了Angular中Lazy Loading懶加載陷阱避坑最佳實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Angular+ionic實現(xiàn)折疊展開效果的示例代碼
這篇文章主要介紹了Angular+ionic實現(xiàn)折疊展開效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Angular將填入表單的數(shù)據(jù)渲染到表格的方法
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實用價值,需要的朋友可以參考下2017-09-09