Angular實(shí)現(xiàn)下拉框模糊查詢功能示例
本文實(shí)例講述了Angular實(shí)現(xiàn)下拉框模糊查詢功能。分享給大家供大家參考,具體如下:
前兩天研究了一下angularjs,不得不說angularjs的mvc思想還是很強(qiáng)大的。對應(yīng)偏重于數(shù)據(jù)處理的項(xiàng)目還是非常有優(yōu)勢的。
寫了個(gè)搜索下拉框的demo,注釋在里邊都寫了,就不再這羅嗦了。
1. 普通方式實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="utf-8">
<title>www.dhdzp.com 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>邏輯實(shí)現(xiàn)步驟</h2></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個(gè)下拉項(xiàng)進(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"]; //下拉框選項(xiàng)
$scope.tempdatas = $scope.datas; //下拉框選項(xiàng)副本
$scope.hidden=true;//選擇框是否隱藏
$scope.searchField='';//文本框數(shù)據(jù)
//將下拉選的數(shù)據(jù)值賦值給文本框
$scope.change=function(x){
$scope.searchField=x;
$scope.hidden=true;
}
//獲取的數(shù)據(jù)值與下拉選逐個(gè)比較,如果包含則放在臨時(shí)變量副本,并用臨時(shí)變量副本替換下拉選原先的數(shù)值,如果數(shù)據(jù)為空或找不到,就用初始下拉選項(xiàng)副本替換
$scope.changeKeyValue=function(v){
var newDate=[]; //臨時(shí)下拉選副本
//如果包含就添加
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. 指令方式實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="utf-8">
<title>www.dhdzp.com 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>邏輯實(shí)現(xiàn)步驟</h2></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個(gè)下拉項(xiàng)進(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"]; //下拉框選項(xiàng)
});
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; //下拉框選項(xiàng)副本
$scope.hidden=true;//選擇框是否隱藏
$scope.searchField='';//文本框數(shù)據(jù)
//將下拉選的數(shù)據(jù)值賦值給文本框
$scope.change=function(x){
$scope.searchField=x;
$scope.hidden=true;
}
//獲取的數(shù)據(jù)值與下拉選逐個(gè)比較,如果包含則放在臨時(shí)變量副本,并用臨時(shí)變量副本替換下拉選原先的數(shù)值,如果數(shù)據(jù)為空或找不到,就用初始下拉選項(xiàng)副本替換
$scope.changeKeyValue=function(v){
var newDate=[]; //臨時(shí)下拉選副本
//如果包含就添加
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模擬實(shí)現(xiàn)select標(biāo)簽效果
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計(jì)有所幫助。
- Angularjs實(shí)現(xiàn)帶查找篩選功能的select下拉框示例代碼
- angularjs 實(shí)現(xiàn)帶查找篩選功能的select下拉框?qū)嵗?/a>
- Angular.JS中select下拉框設(shè)置value的方法
- Angularjs實(shí)現(xiàn)下拉框聯(lián)動的示例代碼
- AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框
- AngularJS中下拉框的基本用法示例
- AngularJS使用ng-options指令實(shí)現(xiàn)下拉框
- angularjs下拉框空白的解決辦法
- AngularJS動態(tài)生成select下拉框的方法實(shí)例
相關(guān)文章
詳解AngularJS通過ocLazyLoad實(shí)現(xiàn)動態(tài)(懶)加載模塊和依賴
本篇文章主要介紹了詳解AngularJS通過ocLazyLoad實(shí)現(xiàn)動態(tài)(懶)加載模塊和依賴 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
在Angular中實(shí)現(xiàn)一個(gè)級聯(lián)效果的下拉框的示例代碼
這篇文章主要介紹了在Angular中實(shí)現(xiàn)一個(gè)級聯(lián)效果的下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
詳解Angular Karma測試的持續(xù)集成實(shí)踐
這篇文章主要介紹了詳解Angular Karma測試的持續(xù)集成實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
對angularJs中2種自定義服務(wù)的實(shí)例講解
今天小編就為大家分享一篇對angularJs中2種自定義服務(wù)的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
徹底學(xué)會Angular.js中的transclusion
這篇文章主要給大家介紹Angular.js中transclusion的相關(guān)資料,希望通過這一篇文章大家能夠弄懂Angular.js中的transclusion,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
AngularJS模仿Form表單提交的實(shí)現(xiàn)代碼
本文通過一段實(shí)例代碼給大家簡單介紹了angularjs模仿form表單提交的方法,非常不錯,具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-12-12

