Angular實現(xiàn)下拉框模糊查詢功能示例
本文實例講述了Angular實現(xiàn)下拉框模糊查詢功能。分享給大家供大家參考,具體如下:
前兩天研究了一下angularjs,不得不說angularjs的mvc思想還是很強(qiáng)大的。對應(yīng)偏重于數(shù)據(jù)處理的項目還是非常有優(yōu)勢的。
寫了個搜索下拉框的demo,注釋在里邊都寫了,就不再這羅嗦了。
1. 普通方式實現(xiàn)
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="utf-8">
<title>www.dbjr.com.cn Angular模糊匹配</title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
<link rel="external nofollow" rel="external nofollow" rel="stylesheet">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body >
<div ng-app="myApp" ng-controller="myCtrl">
<input type = 'test' ng-change="changeKeyValue(searchField)" ng-model="searchField" style = 'display:block;width:200px' ng-click = 'hidden=!hidden' value="{{searchField}}"/></input>
<div ng-hide="hidden">
<select style = 'width:200px' ng-change="change(x)" ng-model="x" multiple>
<option ng-repeat="data in datas" >{{data}}</option>
</select>
</div>
</div>
<div>
<p><h1>angular輸入選擇框</h1></p>
<p><h2>邏輯實現(xiàn)步驟</h2></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個下拉項進(jìn)行比較</p>
<p>2如果包含則只顯示包含的部分,不包含則顯示全部</p>
<div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.datas = ["key4","xyz","key3","xxxx","key2","value2","key1","value1"]; //下拉框選項
$scope.tempdatas = $scope.datas; //下拉框選項副本
$scope.hidden=true;//選擇框是否隱藏
$scope.searchField='';//文本框數(shù)據(jù)
//將下拉選的數(shù)據(jù)值賦值給文本框
$scope.change=function(x){
$scope.searchField=x;
$scope.hidden=true;
}
//獲取的數(shù)據(jù)值與下拉選逐個比較,如果包含則放在臨時變量副本,并用臨時變量副本替換下拉選原先的數(shù)值,如果數(shù)據(jù)為空或找不到,就用初始下拉選項副本替換
$scope.changeKeyValue=function(v){
var newDate=[]; //臨時下拉選副本
//如果包含就添加
angular.forEach($scope.datas ,function(data,index,array){
if(data.indexOf(v)>=0){
newDate.unshift(data);
}
});
//用下拉選副本替換原來的數(shù)據(jù)
$scope.datas=newDate;
//下拉選展示
$scope.hidden=false;
//如果不包含或者輸入的是空字符串則用初始變量副本做替換
if($scope.datas.length==0 || ''==v){
$scope.datas=$scope.tempdatas;
}
console.log($scope.datas);
}
});
</script>
</html>
2. 指令方式實現(xiàn)
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="utf-8">
<title>www.dbjr.com.cn Angular模糊匹配</title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
<link rel="external nofollow" rel="external nofollow" rel="stylesheet">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body >
<div ng-app="myApp" ng-controller="myCtrl">
<div>
<select-search datas="datas"></select-search>
</div>
</div>
<div>
<p><h1>angular輸入選擇框 自定義指令方式</h1></p>
<p><h2>邏輯實現(xiàn)步驟</h2></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個下拉項進(jìn)行比較</p>
<p>2如果包含則只顯示包含的部分,不包含則顯示全部</p>
<div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.datas = ["key4","xyz","key3","xxxx","key2","value2","key1","value1"]; //下拉框選項
});
app.directive('selectSearch', function($compile) {
return {
restrict: 'AE', //attribute or element
scope: {
datas: '=',
//bindAttr: '='
},
template:
'<input type = "test" ng-change="changeKeyValue(searchField)" ng-model="searchField" style = "display:block;width:200px" '+
'ng-click = "hidden=!hidden" value="{{searchField}}"/></input>'+
'<div ng-hide="hidden">'+
' <select style = "width:200px" ng-change="change(x)" ng-model="x" multiple>'+
' <option ng-repeat="data in datas" >{{data}}</option>'+
' </select>'+
'</div>',
// replace: true,
link: function($scope, elem, attr, ctrl) {
$scope.tempdatas = $scope.datas; //下拉框選項副本
$scope.hidden=true;//選擇框是否隱藏
$scope.searchField='';//文本框數(shù)據(jù)
//將下拉選的數(shù)據(jù)值賦值給文本框
$scope.change=function(x){
$scope.searchField=x;
$scope.hidden=true;
}
//獲取的數(shù)據(jù)值與下拉選逐個比較,如果包含則放在臨時變量副本,并用臨時變量副本替換下拉選原先的數(shù)值,如果數(shù)據(jù)為空或找不到,就用初始下拉選項副本替換
$scope.changeKeyValue=function(v){
var newDate=[]; //臨時下拉選副本
//如果包含就添加
angular.forEach($scope.datas ,function(data,index,array){
if(data.indexOf(v)>=0){
newDate.unshift(data);
}
});
//用下拉選副本替換原來的數(shù)據(jù)
$scope.datas=newDate;
//下拉選展示
$scope.hidden=false;
//如果不包含或者輸入的是空字符串則用初始變量副本做替換
if($scope.datas.length==0 || ''==v){
$scope.datas=$scope.tempdatas;
}
console.log($scope.datas);
}
}
};
});
</script>
</html>
最終效果如下:

注意這里對select標(biāo)簽設(shè)置了multiple屬性,所以在頁面上input標(biāo)簽?zāi)芨采wselect標(biāo)簽
如果不用multiple屬性,需自行用div模擬實現(xiàn)select標(biāo)簽效果
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于Angularjs中跨域設(shè)置白名單問題
這篇文章主要介紹了Angularjs中關(guān)于跨域設(shè)置白名單問題,需要的朋友可以參考下2018-04-04
AngularJS自定義指令實現(xiàn)面包屑功能完整實例
這篇文章主要介紹了AngularJS自定義指令實現(xiàn)面包屑功能,結(jié)合完整實例形式分析了AngularJS自定義指令的定義、調(diào)用及面包屑功能的具體實現(xiàn)技巧,需要的朋友可以參考下2017-05-05
使用Angular內(nèi)置模塊進(jìn)行HTTP請求
這篇文章主要介紹了使用Angular內(nèi)置模塊進(jìn)行HTTP請求方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

