欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular實(shí)現(xiàn)下拉框模糊查詢功能示例

 更新時(shí)間:2018年01月03日 08:52:03   作者:qq_26222859  
這篇文章主要介紹了Angular實(shí)現(xiàn)下拉框模糊查詢功能,涉及AngularJS事件響應(yīng)及字符串查詢等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Angular實(shí)現(xiàn)下拉框模糊查詢功能。分享給大家供大家參考,具體如下:

前兩天研究了一下angularjs,不得不說(shuō)angularjs的mvc思想還是很強(qiáng)大的。對(duì)應(yīng)偏重于數(shù)據(jù)處理的項(xiàng)目還是非常有優(yōu)勢(shì)的。

寫了個(gè)搜索下拉框的demo,注釋在里邊都寫了,就不再這羅嗦了。

1. 普通方式實(shí)現(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>邏輯實(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);
   }
  });
  //用下拉選副本替換原來(lái)的數(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.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>邏輯實(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);
    }
   });
  //用下拉選副本替換原來(lái)的數(shù)據(jù)
   $scope.datas=newDate;
  //下拉選展示
   $scope.hidden=false;
  //如果不包含或者輸入的是空字符串則用初始變量副本做替換
   if($scope.datas.length==0 || ''==v){
    $scope.datas=$scope.tempdatas;
   }
   console.log($scope.datas);
  }
 }
 };
});
</script>
</html>

最終效果如下:

注意這里對(duì)select標(biāo)簽設(shè)置了multiple屬性,所以在頁(yè)面上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é)

希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論