Angular實(shí)現(xiàn)搜索框及價(jià)格上下限功能
閑來(lái)無(wú)事,寫(xiě)一個(gè)簡(jiǎn)單的angular的搜索框。
1.要求:
利用 AngularJS 框架實(shí)現(xiàn)手機(jī)產(chǎn)品搜索功能,題目要求:
1)自行查找素材,按照原有數(shù)據(jù)格式將手機(jī)產(chǎn)品數(shù)據(jù)豐富到至少10個(gè)以上
2)自行設(shè)計(jì)頁(yè)面,需要包含“搜索條件部分”,“手機(jī)信息顯示部分”
3)當(dāng)更改任何搜索條件時(shí),需要實(shí)時(shí)顯示搜索結(jié)果在“顯示部分”中
4)搜索條件具體要求:
搜索框(匹配操作系統(tǒng)、產(chǎn)品名、產(chǎn)商進(jìn)行模糊查詢(xún))
價(jià)格區(qū)間(開(kāi)始價(jià)格~結(jié)束價(jià)格)
2.需求分析:
首先,我們需要將商品渲染到頁(yè)面上。
其次,當(dāng)我們輸入搜索框文本時(shí),動(dòng)態(tài)顯示符合搜索框文本的商品。
其中,動(dòng)態(tài)指的時(shí)我們每輸入一個(gè)字符,都會(huì)進(jìn)行產(chǎn)品的篩選。
最后,價(jià)格的上下限也是同樣的原理。
那么,這樣一來(lái),我們使用angular是最為方便的。因?yàn)閍ngular對(duì)雙向數(shù)據(jù)的支持非常好。
3.實(shí)際代碼:
1)HTML代碼:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>AngularJS Page Useing Bootstrap Framework</title>
<link rel="stylesheet" href="">
<script src="./lib/angular/angular-v1.6.6.js"></script>
</head>
<body ng-app="searchApp">
<div ng-controller="dataCtrl">
<input type="text" name="搜索框" ng-model="content" placeholder="請(qǐng)輸入要搜索的物品">
<input type="text" name="價(jià)格上限" ng-model="top" placeholder="價(jià)格上限">
<input type="text" name="價(jià)格下限" ng-model="bottom" placeholder="價(jià)格下限">
<div>
<ul>
<li ng-repeat="p in datas">
{{p.name}}
</li>
</ul>
</div>
</div>
</body>
</html>
2)JS代碼:
let httpApp = angular.module( 'searchApp', [] );
httpApp.controller( 'dataCtrl', [ "$scope", "$http", function( $scope, $http ){
let http = $http.get( "conf.json" );
//模擬從后端獲取的json數(shù)據(jù)。
$scope.content = '';
$scope.$watch("content + top + bottom",function(){
http.then(
// success callback
function success( response ){
$scope.datas = response.data;
//進(jìn)行價(jià)格篩選。
$scope.datas=$scope.datas.filter(function( x,index ){
if($scope.top===undefined&&$scope.bottom===undefined)
{
return 1;
}
else if($scope.top===undefined){
return x.price>=$scope.bottom
}
else if($scope.bottom===undefined){
return x.price<=$scope.top;
}
else{
return x.price>=$scope.bottom&&x.price<=$scope.top;
}
});
//進(jìn)行搜索內(nèi)容篩選。
$scope.datas=$scope.datas.filter(function( x,index ){
system=x.system.indexOf($scope.content)+1;
name = x.name.indexOf($scope.content)+1;
producer=x.producer.indexOf($scope.content)+1;
if(system+name+producer>=1){
return 1;
}
else{
return 0;
}
})
},
// error callback
function error( response ){
console.log( response );
}
);
});
} ] );
PS:為了偷懶,我并沒(méi)有寫(xiě)很好看的樣式。如果你需要,可以自己添加。
3)conf.json代碼:
[
{
"system": "ios",
"name": "Apple iPhone 6s 16GB 玫瑰金色",
"price": 4698,
"producer": "Apple",
"pic": "01.jpg"
},
{
"system": "MIUI",
"name": "小米手機(jī)4S 全網(wǎng)通版 2GB內(nèi)存 16GB 白色",
"price": 1499,
"producer": "小米",
"pic": "02.jpg"
},
{
"system": "Android",
"name": "魅藍(lán)note3 (16GB) 銀色 全網(wǎng)通公開(kāi)版 雙卡雙待",
"price": 1099,
"producer": "魅族科技",
"pic": "03.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6587,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6578,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6788,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6878,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6528,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6988,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6388,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6378,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6738,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6568,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6558,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6738,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6428,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 652488,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 654588,
"producer": "Apple",
"pic": "04.jpg"
},
{
"system": "ios",
"name": "Apple iPhone 6s Plus 64GB 銀色 移動(dòng)聯(lián)通電信4G手機(jī)",
"price": 6545645688,
"producer": "Apple",
"pic": "04.jpg"
}
]
PS:通過(guò)對(duì)象模擬了服務(wù)器傳輸?shù)膉son數(shù)據(jù)。另外,圖片可以自行添加,實(shí)現(xiàn)。
4.最后問(wèn)題:
當(dāng)然,我上傳上來(lái)的代碼,還留了一個(gè)坑。如何在輸入價(jià)格,再清空后,取消對(duì)應(yīng)價(jià)格區(qū)間的限制。
最后,搜索的方法,可以怎樣優(yōu)化,可以思考一下,作為一個(gè)拓展吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angularjs material 實(shí)現(xiàn)搜索框功能
- Angular實(shí)現(xiàn)跨域(搜索框的下拉列表)
- Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果
- 在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例
- angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來(lái)
- angularjs實(shí)現(xiàn)分頁(yè)和搜索功能
- Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例
- angularJs使用$watch和$filter過(guò)濾器制作搜索篩選實(shí)例
- Angular封裝搜索框組件操作示例
相關(guān)文章
AngularJS入門(mén)教程(二):AngularJS模板
這篇文章主要介紹了AngularJS入門(mén)教程(二):AngularJS模板,本文是系列文章的第三篇,本系列會(huì)用一個(gè)項(xiàng)目來(lái)講解AngularJS的使用,需要的朋友可以參考下2014-12-12
詳解Angular4中路由Router類(lèi)的跳轉(zhuǎn)navigate
這篇文章主要介紹了詳解Angular4中路由Router類(lèi)的跳轉(zhuǎn)navigate,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
詳解Angular的內(nèi)置過(guò)濾器和自定義過(guò)濾器【推薦】
在實(shí)際的開(kāi)發(fā)過(guò)程中,很多后端返回給我們的數(shù)據(jù)都是需要格式化處理的,在angular中為我們內(nèi)置提供了filter指令,可以很方便的對(duì)數(shù)據(jù)進(jìn)行處理。本文將對(duì)Angular的內(nèi)置過(guò)濾器和自定義過(guò)濾器進(jìn)行詳細(xì)介紹,下面跟著小編一起來(lái)看下吧2016-12-12
AngularJS自定義過(guò)濾器用法經(jīng)典實(shí)例總結(jié)
這篇文章主要介紹了AngularJS自定義過(guò)濾器用法,結(jié)合實(shí)例形式總結(jié)分析了AngularJS自定義過(guò)濾器進(jìn)行包含、替換、篩選、過(guò)濾、排序等操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
解決angularjs service中依賴(lài)注入$scope報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決angularjs service中依賴(lài)注入$scope報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Angular實(shí)現(xiàn)下載安裝包的功能代碼分享
本文通過(guò)實(shí)例代碼給大家介紹了angular實(shí)現(xiàn)下載安裝包的功能以及基于angularjs代碼實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)的功能,需要的朋友參考下吧2017-09-09
AngularJs ng-repeat 嵌套如何獲取外層$index
這篇文章主要介紹了AngularJs ng-repeat 嵌套如何獲取外層$index的相關(guān)資料,需要的朋友可以參考下2016-09-09
AngularJS動(dòng)態(tài)加載模塊和依賴(lài)的方法分析
這篇文章主要介紹了AngularJS動(dòng)態(tài)加載模塊和依賴(lài)的方法,結(jié)合實(shí)例形式分析了AngularJS使用ocLazyLoad實(shí)現(xiàn)動(dòng)態(tài)加載的相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例
今天小編就為大家分享一篇對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

