控制属性为multiple的select
需求:实现点击查询,搜索对应渠道已投放、未投放批次。如图:
html:
1 <div class="form-inline margin-top-20"> 2 <div class="form-group has-feedback"> 3 <label control-label margin-left-15 style="font-size:16px">🔍渠道搜索:</label> 4 <div class="dlb ui-select-bg"> 5 <input type="text" class="form-control" ng-model="name" class="w120" placeholder="请输入渠道名称"> 6 </div> 7 <div class="form-group margin-left-20"> 8 9 <input ng-click="submit()" class="btn btn-info btn-xs" type="submit" value="查询"> 10 </div> 11 </div> 12 <div class="margin-top-20"> 13 <form name="form"> 14 <div style="display:inline-block"> 15 <span style="vertical-align: top;">未投放批次</span> 16 <select multiple="multiple" name="se1" style="height:400px;width:100px;"> 17 <option ng-repeat='(index,item) in unput' value="{{item.batch_id}}">{{item.batch_name}}</option> 18 19 </select> 20 </div> 21 <input class="btn btn-info btn-xs margin-left-20" type="button" value="新增" ng-click="add()"/> 22 <input class="btn btn-info btn-xs" type="button" value="删除" ng-click="plu()"/> 23 <div style="display:inline-block"> 24 <select multiple="multiple" name="se2" style="height:400px;width:100px;margin-left:20px"> 25 <option ng-repeat='(index,item) in put' value="{{item.batch_id}}">{{item.batch_name}}</option> 26 27 </select> 28 <span style="vertical-align: top;">已投放批次</span> 29 30 </div> 31 32 </form> 33 </div>
js:
1 /** 2 * Created by huangli on 17/3/27. 3 */ 4 var path = require('path'); 5 angular.module('App').controller('activityConfigCtrl',['$scope','$http','queryList','description','dateGather','$uibModal','$rootScope','userInfo','tips',function($scope,$http,queryList,description,dateGather,$uibModal,$rootScope,userInfo,tips){ 6 7 //默认url 8 var url = '/Rest/Urlchannel/searchChannel'; 9 var urlId = ''; 10 11 $scope.name='51test'; 12 13 var se1 = document.form.se1; 14 var se2 = document.form.se2; 15 var arr = new Array(); 16 var arrAdd = []; 17 var arrDel = []; 18 19 urlId = JSON.parse(localStorage.urlId); 20 21 //搜索渠道 22 $scope.submit = function(){ 23 tips.text('加载中,请稍候...', false); 24 25 26 $http({ 27 method: "POST", 28 contentType: "application/json", 29 url: "/Rest/Urlchannel/searchChannel", 30 data: { 31 name:$scope.name, 32 url_id:urlId 33 }, 34 dataType : "json", 35 headers: { 36 'Content-Type': 'application/x-www-form-urlencoded', 37 'token':userInfo.token 38 }, 39 transformRequest: function(obj) { 40 var str = []; 41 for (var p in obj) { 42 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 43 } 44 return str.join("&"); 45 } 46 47 }).then(function(res) { 48 49 se1.options.length=0; 50 se2.options.length=0; 51 52 if(res.data.code==200){ 53 $scope.unput = res.data.data.unput; 54 $scope.put = res.data.data.put; 55 $scope.channelId = res.data.data.channel_id; 56 tips.text(res.data.msg); 57 }else{ 58 tips.text(res.data.msg); 59 } 60 }).then(null,function(xhr) { 61 if (xhr && xhr.message) { 62 tips.text(xhr.message); 63 } else { 64 tips.text('网络出错,请稍候再试!'); 65 } 66 }); 67 } 68 69 //设置渠道 70 $scope.setChannel = function (sign,batchs) { 71 $http({ 72 method: "POST", 73 contentType: "application/json", 74 url: "/Rest/Urlchannel/setChannel", 75 data: { 76 url_id:urlId, 77 sign:sign, 78 batchs:batchs 79 80 }, 81 dataType : "json", 82 headers: { 83 'Content-Type': 'application/x-www-form-urlencoded', 84 'token':userInfo.token 85 }, 86 transformRequest: function(obj) { 87 var str = []; 88 for (var p in obj) { 89 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); 90 } 91 return str.join("&"); 92 } 93 94 }).then(function(res) { 95 if(res.data.code == 200){ 96 97 if(sign=='add'){ 98 for(var i=0;i<se1.length;i++) { 99 if(se1[i].selected) { 100 se2.options.add(new Option(se1[i].text, se1[i].value)); 101 } 102 103 } 104 105 $scope.refresh(se1); 106 tips.text(res.data.msg); 107 } else{ 108 for(var i=0;i<se2.length;i++) { 109 if(se2[i].selected) { 110 se1.options.add(new Option(se2[i].text, se2[i].value)); 111 } 112 } 113 $scope.refresh(se2); 114 tips.text(res.data.msg); 115 } 116 }else { 117 tips.text(res.data.msg); 118 } 119 120 }).then(null,function(xhr) { 121 if (xhr && xhr.message) { 122 tips.text(xhr.message); 123 } else { 124 tips.text('网络出错,请稍候再试!'); 125 } 126 }); 127 } 128 129 $scope.add=function(){ 130 var j=0; 131 for(var i=0;i<se1.length;i++) { 132 if(se1[i].selected) { 133 // se2.options.add(new Option(se1[i].text, se1[i].value)); 134 j= j+1; 135 arr.push(se1[i]); 136 $scope.str = $scope.channelId+'_'+se1[i].value; 137 arrAdd.push($scope.str); 138 } 139 } 140 if(j==0){ 141 tips.text('请选择批次!'); 142 return; 143 } 144 $scope.sign = 'add'; 145 $scope.setChannel ($scope.sign,arrAdd); 146 //$scope.refresh(se1); 147 } 148 $scope.refresh=function(o){ 149 for(var i=0;i<arr.length;i++) { 150 o.removeChild(arr[i]); 151 } 152 153 arr.length=0; 154 } 155 $scope.plu=function() { 156 var k = 0; 157 for(var i=0;i<se2.length;i++) { 158 if(se2[i].selected) { 159 k= k+1; 160 //se1.options.add(new Option(se2[i].text, se2[i].value)); 161 arr.push(se2[i]); 162 $scope.str = $scope.channelId+'_'+se2[i].value; 163 arrDel.push($scope.str) 164 } 165 } 166 if(k==0){ 167 tips.text('请选择批次!'); 168 return; 169 } 170 $scope.sign = 'del'; 171 $scope.setChannel ($scope.sign,arrDel); 172 173 } 174 175 176 }]);
se1.options.length=0;
se2.options.length=0; 这两句的作用是:在每次点击查询之前,把option中之前选中的数据清空,不然,每次点击还是会出现两次刚才操作的数据。
参考资料:1、http://bbs.csdn.net/topics/390214988
努力将自己的温暖带给身边的人!!!!!