angular實(shí)現(xiàn)商品篩選功能
一、demo功能分析
1、通過service()創(chuàng)建數(shù)據(jù)并遍歷渲染到頁面
2、根據(jù)輸入框的輸入值進(jìn)行字段查詢
3、點(diǎn)擊各列實(shí)現(xiàn)排序功能。
二、實(shí)現(xiàn)
1.1 數(shù)據(jù)定義與渲染
angular更偏向于是一個(gè)MVVM模式的框架,所以它的controller很薄,里面的業(yè)務(wù)邏輯也是少的,因此應(yīng)該養(yǎng)成把邏輯寫在service或者Factory等angular提供的可以自定義服務(wù)的方法中。此次demo通過angular的service方法來注冊并定義商品數(shù)據(jù)。
angular.module("app",[])
.service("productData",function(){
//通過service方法來定義數(shù)據(jù),也可以通過factory方法來定義
return [
{
id:1002,
name:'HuaWei',
quantity:200,
price:4300
},
{
id:2123,
name:'iphone7',
quantity:38,
price:6300
},
{
id:3001,
name:'XiaoMi',
quantity:3,
price:2800
},
{
id:4145,
name:'Oppo',
quantity:37,
price:2100
},
{
id:5563,
name:'Vivo',
quantity:23,
price:2100
}
]
})
//傳入用service定義的productData數(shù)據(jù)依賴
.controller("myCtrl",function($scope,productData){
$scope.data=productData; //進(jìn)行相應(yīng)賦值
$scope.order=''; //單列排序用,后面詳解
$scope.changeOrder=function(type){
$scope.orderType=type;
if($scope.order===''){
$scope.order='-';
}else{
$scope.order='';
}
}
})
數(shù)據(jù)渲染部分:
<table class="table">
<thead>
<tr>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('id')">產(chǎn)品編號<span class="caret"></span></th>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('name')">產(chǎn)品各稱<span class="caret"></span></th>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('price')">產(chǎn)品價(jià)錢<span class="caret"></span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="value in data|filter:{id:search}|orderBy:order+orderType">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>{{value.price}}</td>
</tr>
</tbody>
</table>
說明:上面利用了bootstrap的caret類名來顯示出三角符號,并通過給父元素加dropup使三角符號轉(zhuǎn)向。
1、三角符號的轉(zhuǎn)向與否由ng-class指令確定,傳入表達(dá)式,當(dāng)order===‘ '時(shí),為true,則給th加上dropup類名
2、用ng-click指令綁定點(diǎn)擊事件,實(shí)現(xiàn)點(diǎn)擊就切換排序方式
2.2 搜索功能
采用angular的filter過濾器,搜索輸入字段
<!--輸入框采用ng-model綁定一個(gè)值-->
搜索:<input type="text" ng-model="search">
<!--通過filter:{id:search}實(shí)現(xiàn)以id為搜索內(nèi)容,以search的值為搜索基準(zhǔn)-->
<tr ng-repeat="value in data|filter:{id:search}|orderBy:order+orderType">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>{{value.price}}</td>
</tr>
2.3 排序功能
1、定義order屬性用于設(shè)置正序還是反序
2、定義orderType屬性用于設(shè)置參考排序的值
3、定義changeOrder()方法用于實(shí)現(xiàn)點(diǎn)擊就切換排序的功能
$scope.order=''; //當(dāng)order為‘'時(shí)正序
$scope.changeOrder=function(type){ //傳入屬性名,以此為基準(zhǔn)排序
$scope.orderType=type;
if($scope.order===''){
$scope.order='-'; //order為'-'時(shí),反序
}else{
$scope.order='';
}
}
頁面中:changeOrder()函數(shù)傳入“類型”作為參數(shù),并在函數(shù)內(nèi)部通過\ $scope.orderType=type;設(shè)定排序的參考類型
<table class="table">
<thead>
<tr>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('id')">產(chǎn)品編號<span class="caret"></span></th>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('name')">產(chǎn)品各稱<span class="caret"></span></th>
<th ng-class="{dropup:order===''}" ng-click="changeOrder('price')">產(chǎn)品價(jià)錢<span class="caret"></span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="value in data|filter:{id:search}|orderBy:order+orderType">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>{{value.price}}</td>
</tr>
</tbody>
</table>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于angular2 的 http服務(wù)封裝的實(shí)例代碼
這篇文章主要介紹了基于angular2 的 http服務(wù)封裝實(shí)例代碼,2017-06-06
實(shí)例詳解angularjs和ajax的結(jié)合使用
本篇文章給大家介紹angularjs和ajax的結(jié)合使用,本文介紹的非常詳細(xì),對angularjs和ajax感興趣的朋友一起參考下吧2015-10-10
AngularJS使用ngMessages進(jìn)行表單驗(yàn)證
這篇文章主要介紹了AngularJS使用ngMessages進(jìn)行表單驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2015-12-12
AngularJs Injecting Services Into Controllers詳解
本文主要介紹AngularJs Injecting Services Into Controllers的知識,這里整理了一下相關(guān)資料,及示例代碼,幫助大家學(xué)習(xí)和理解,有興趣的小伙伴可以參考下2016-09-09
解析AngularJS中g(shù)et請求URL出現(xiàn)的跨域問題
本文主要介紹了AngularJS中g(shù)et請求URL出現(xiàn)跨域問題。需要的朋友可以參考下2016-12-12
詳解JavaScript的AngularJS框架中的表達(dá)式與指令
這篇文章主要介紹了JavaScript的AngularJS框架中的表達(dá)式與指令,文中羅列了幾個(gè)常用的指令屬性加以說明,需要的朋友可以參考下2016-03-03

