angular-ui-select 支持搜索的 下拉选择框 的使用

github地址:https://github.com/angular-ui/ui-select

默认支持所有唯一性字段的匹配。可以配置只有一种。通过channelList | filter: {description: $select.search},或者通过自己对数据组装。


//异步获取渠道类型
	  	SalesService.getChannelList().then(function(data) {
			data.forEach(function(item){
				var obj = {};
				obj.id = item.id;
				obj.text = item.description;
				$scope.channelTypes.push(obj);
			});
		});


                ChannelService.queryUserList().then(function (res) {
				var data = res.data || [];
				for (var i = 0, len = data.length; i < len; i++) {
					var obj = {
						id: data[i].id,
						name: data[i].realName + ' ' + data[i].uid
					};
					$scope.userList.push(obj);
				}
			});

这样也可以组装出2个字段,3个字段,看业务需求。比如需要同时支持对realName和uid的匹配,可以拼接起来放到新的name字段中。


组件的使用: 

(1) Html中:


没有匹配的渠道

ui-select: controller中绑定的被选定的数据

ui-select-match: 匹配到的数据,变色处理

ui-select-choices: 待选择的数据

ui-select-no-choice: 提示用户没有匹配的数据

filter: $select.search: 组件里面定义的过滤器


Controller中获取数据:

  1. $scope.channelList = [{ id: 0, description: '全部' }];  
  2.   
  3.         // 获取渠道列表  
  4.         function getChannelList() {  
  5.             FinalStatementService.channelList().then(function (res) {  
  6.                 var data = res.data;  
  7.                 $scope.channelList = $scope.channelList.concat(data);  
  8.             });  
  9.         }  
  10.         getChannelList();


 (2) 不使用组件filter中的单字段过滤:


  1. class="col-sm-3">  
  2.     "search.type" uis-open-close="onOpenClose(isOpen)">  
  3.          "请选择渠道">  
  4.               "$select.selected.text">  
  5.            
  6.          "channel in (channelTypes | filter: $select.search) track by $index">  
  7.               "channel.text">  
  8.            
  9.            
  10.               没有匹配的渠道  
  11.            
  12.        
  13.  


 自己对数据处理,得到除了ID之外,只包含description字段的匹配。


  1. $scope.channelTypes = [  
  2.             {id:0,text:'全部'}  
  3.         ];  
  4.   
  5.         //异步获取渠道类型  
  6.         SalesService.getChannelList().then(function(data) {  
  7.             data.forEach(function(item){  
  8.                 var obj = {};  
  9.                 obj.id = item.id;  
  10.                 obj.text = item.description;  
  11.                 $scope.channelTypes.push(obj);  
  12.             });  
  13.         });  
  14.   
  15.         //查询条件  
  16.         $scope.search = {  
  17.             type: $scope.channelTypes[0],  
  18.             skuId: '',  
  19.             startTime: '',  
  20.             endTime: ''  
  21.         };

效果:

angular-ui-select 支持搜索的 下拉选择框 的使用_第1张图片



备注:

ng-options="t.id as t.type for t intypes"   代表生成的option标签

这个组件里面也可以这样:

channel.description for chanel in (channelList | filter: $select.search) track by $index">


你可能感兴趣的:(Angularjs)