怎么在Angular中實現(xiàn)一個下拉框模糊查詢功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)是專業(yè)的句容網(wǎng)站建設(shè)公司,句容接單;提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行句容網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
1. 普通方式實現(xiàn)
<!DOCTYPE html>
<html>
<head lang="zh_CN">
<meta charset="utf-8">
<title>www.jb51.net Angular模糊匹配</title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" 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><h2>angular輸入選擇框</h2></p>
<p><h3>邏輯實現(xiàn)步驟</h3></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個下拉項進行比較</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.jb51.net Angular模糊匹配</title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" 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><h2>angular輸入選擇框 自定義指令方式</h2></p>
<p><h3>邏輯實現(xiàn)步驟</h3></p>
<p>1文本框做輸入,并監(jiān)控器change事件,在change事件中獲取輸入值,獲取的輸入值與選擇框中的各個下拉項進行比較</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>看完上述內(nèi)容,你們掌握怎么在Angular中實現(xiàn)一個下拉框模糊查詢功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:怎么在Angular中實現(xiàn)一個下拉框模糊查詢功能
本文URL:http://www.chinadenli.net/article36/gccssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、服務(wù)器托管、面包屑導(dǎo)航、商城網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)