本文来之于:http://blog.csdn.net/jetsteven/article/details/5104380#
1、经常用到如下图的选择器,而且要支持排序的,所以萌生用JQUERY写一个。
功能如下:
- 左右多选,移动。
- 右边的项可以上下拖动
源代码如下
1 /* 2 name:TwoWay-Select Control for Jquery 3 author:arn 4 date:2009-12-29 5 remark: AJAX URL返回数据必须是json格式{state:1,msg:'',fields:[]} 6 1、右边可以上下拖动 7 2、$('#div').twowaylist({url:'',onBeforeSubmit:'',captionColumn''}); 8 3、$('#div').twowaylistinit({field_no:'xx',...}); 9 4、$('#div').twowaylistsubmit({'rptid':$('#txtid').val(),'act':'rpt'}) //AJAX URL用到的参数 10 */ 11 (function($){ 12 $.addList = function(t,p) 13 { 14 if (t.checkbox) return false; //return if already exist 15 16 // apply default properties 17 p = $.extend({ 18 items:[], //ALL ITEMS 19 captionColumn:'caption', //显示的名称 20 isgroupColumn:'group', //分组CHECKBOX用到的字段 21 idColumn:'FIELD_NO', //编号字段 22 height: 320, //default height 23 'auto', //auto width 24 url: false, //ajax url 25 method: 'POST', // data sending method 26 errormsg: 'Connection Error', 27 title: false, 28 dataType:'json', 29 onSuccess: false, // using a custom populate function 30 onBeforeSubmit: false //提交之前处理事项 31 }, p); 32 33 //create twoway-select class 34 var d = { 35 json2str:function(json) 36 { 37 var isArrObj = $.isArray(json); 38 var arr = []; 39 // 40 for (var key in json) 41 { 42 var k = isArrObj? '' : key+":" ; 43 44 if (typeof json[key] == 'object' && json[key] != null) 45 arr.push(k + d.json2str(json[key])); //JSON对象 46 else 47 arr.push(k + "'" + decodeURIComponent(json[key]) +"'"); //普通值 48 } 49 return isArrObj ? '[' + arr.join(',') + ']' : '{' + arr.join(',') + '}'; 50 }, 51 str2json:function(str) 52 { 53 return eval('('+str+')'); 54 }, 55 l2r:function(o){ 56 57 var json = d.str2json($(o).attr('json')); 58 var chk = document.createElement("input"); 59 //IF GROUP 60 var chked = json[p.isgroupColumn]==1?'checked':''; 61 $(chk).attr({'type':'checkbox','id':'li'+json[p.idColumn],'checked':chked}); 62 $(o).prepend(chk); 63 $(d.rdiv).find('ol').append(o); 64 //change json value,if checkbox state changed 65 $(o).find("input:checkbox").bind('click',function() 66 { 67 if($(this).attr('checked')) 68 { 69 json[p.isgroupColumn] = 1; 70 }else{ 71 json[p.isgroupColumn] = 0; 72 } 73 $(o).attr("json",d.json2str(json)); 74 }); 75 $(d.vinput).val(d.getselected()); 76 }, 77 r2l:function(o){ 78 $(o).find(":checkbox").remove(); 79 $(d.ldiv).find('ol').append(o); 80 $(d.vinput).val(d.getselected()); 81 }, 82 l2rAll:function(){ 83 $("li", $(d.ldiv).find('ol')).each(function(i,obj){ 84 d.l2r(obj); 85 }); 86 }, 87 r2lAll:function(){ 88 $("li", $(d.rdiv).find('ol')).each(function(i,obj){ 89 d.r2l(obj); 90 }); 91 }, 92 getselected:function(){ 93 var result=[]; 94 95 $(d.rdiv).find('li').each(function(i,obj){ 96 result.push($(this).attr('json')); 97 }); 98 return result; 99 }, 100 initData:function(data){ 101 $(d.ldiv).find('ol').empty(); 102 $(d.rdiv).find('ol').empty(); 103 var li; 104 var lbl; 105 for (i=0;i<data.length;i++){ 106 var li = document.createElement('li'); 107 lbl = document.createElement('label'); 108 if(true) 109 { 110 $(lbl).text(data[i][p.idColumn] + ' '+ data[i][p.captionColumn]); //.attr("for",'li' + data[i][p.idColumn]) 111 }else{ 112 $(lbl).text(data[i][p.captionColumn]); 113 } 114 $(li).attr('json',d.json2str(data[i])).append(lbl); 115 116 $(d.ldiv).find('ol').append(li); 117 } 118 $(d.ldiv).find('ol li').each(function(i,obj) 119 { 120 $(obj).bind('dblclick',function() 121 { 122 if($(this).parent().hasClass('twowaylist-lol')) 123 { 124 d.l2r(this); 125 }else{ 126 d.r2l(this); 127 } 128 }).bind('click',function() 129 { 130 $(this).toggleClass("ui-selected"); 131 }); 132 }); 133 134 }, 135 ajaxSubmit:function(para){ 136 if(p.url){ 137 138 if(p.onBeforeSubmit) para= p.onBeforeSubmit(); 139 $.ajax({ 140 type: p.method, 141 timeout:20 * 1000, 142 url: p.url, 143 async:false, 144 data: para, 145 dataType: p.dataType, 146 success: function(data){ 147 if(data.state == 1) 148 { 149 $(d.caption).html(''); 150 d.initData(data.fields); 151 if(p.onSuccess) //执行成功后 152 { 153 p.onSuccess(); 154 } 155 }else{ 156 $(d.caption).html('数据加载失败<br/>' + data.msg ); 157 } 158 }, 159 error: function(data) { try { alert(data.toString()) } catch (e) {} }, 160 beforeSend:function(data) { }, 161 complete :function(){ } 162 }); 163 } 164 165 } 166 }; 167 168 if(p.width == 'auto') 169 { 170 p.width = 600; 171 } 172 var mdivwidth = 80; 173 //init div 174 d.aDiv = document.createElement('div'); 175 176 d.ldiv = document.createElement('div'); 177 $(d.ldiv).addClass('twowaylist-oldiv').width((p.width - mdivwidth) /2 ).height(p.height); 178 179 d.rdiv = document.createElement('div'); 180 $(d.rdiv).addClass('twowaylist-oldiv').width((p.width - mdivwidth) /2 ).height(p.height); 181 182 d.mdiv = document.createElement('div'); 183 $(d.mdiv).width(mdivwidth); 184 185 d.mTable = document.createElement('table'); 186 d.mTable.cellPadding = 0; 187 d.mTable.cellSpacing = 0; 188 189 //add button 190 var br="<br />"; 191 //l2r 192 var btnDiv = document.createElement('input'); 193 $(btnDiv).addClass('twowaylist-button').attr({'type':'button','value':'>'}); 194 195 $(btnDiv).click( 196 function() 197 { 198 $('.ui-selected', d.ldiv).each(function() 199 { 200 d.l2r(this); 201 }); 202 } 203 ); 204 $(d.mdiv).append(btnDiv); 205 $(d.mdiv).append(br); 206 //r2l 207 btnDiv = document.createElement('input'); 208 $(btnDiv).addClass('twowaylist-button').attr({'type':'button','value':'<'}); 209 $(btnDiv).click( 210 function() 211 { 212 $('.ui-selected', d.rdiv).each(function() 213 { 214 d.r2l(this); 215 }); 216 } 217 ); 218 $(d.mdiv).append(btnDiv); 219 $(d.mdiv).append(br); 220 //l2r all 221 btnDiv = document.createElement('input'); 222 $(btnDiv).attr({'type':'button','value':'>>'}).addClass('twowaylist-button'); 223 $(btnDiv).click( 224 function() 225 { 226 d.l2rAll(); 227 } 228 ); 229 $(d.mdiv).append(btnDiv); 230 $(d.mdiv).append(br); 231 //r2l all 232 btnDiv = document.createElement('input'); 233 $(btnDiv).attr({'type':'button','value':'<<'}).addClass('twowaylist-button'); 234 $(btnDiv).click( 235 function() 236 { 237 d.r2lAll(); 238 } 239 ); 240 $(d.mdiv).append(btnDiv); 241 $('span',d.mdiv) 242 .css({paddingLeft:20}) 243 ; 244 245 246 //set div 247 d.caption = document.createElement("caption"); 248 $(d.mTable).append(d.caption); 249 //set table th 250 var thead = document.createElement("thead"); 251 var tr = document.createElement("tr"); 252 var th=document.createElement("th"); 253 $(th).html('待选项'); 254 $(tr).append(th); 255 th=document.createElement("th"); 256 $(tr).append(th); 257 th=document.createElement("th"); 258 $(th).html('已选项'); 259 $(tr).append(th); 260 $(thead).append(tr); 261 $(d.mTable).append(thead); 262 var tbody = document.createElement("tbody"); 263 264 tr = document.createElement("tr"); 265 266 var td = document.createElement("td"); 267 var lol= document.createElement("ol"); 268 $(lol).addClass('twowaylist-lol'); 269 $(d.ldiv).append(lol); 270 271 $(td).append(d.ldiv); 272 $(tr).append(td); 273 td = document.createElement("td"); 274 $(td).append(d.mdiv); 275 $(tr).append(td); 276 td = document.createElement("td"); 277 278 var rol= document.createElement("ol"); 279 $(rol).addClass("twowaylist-rol"); 280 $(d.rdiv).append(rol); 281 $(td).append(d.rdiv); 282 $(tr).append(td); 283 284 $(tbody).append(tr); 285 $(d.mTable).append(tbody); 286 $(t).append(d.mTable); 287 288 d.vinput = document.createElement("input"); 289 $(d.vinput).attr({'type':'hidden','id':'twowaylistValue'}); 290 $(t).append(d.vinput); 291 //leftlist selectable 292 //$(lol).selectable(); 293 //rightlist sortable 294 $(rol).sortable(); 295 296 t.list = d; 297 //load items 298 if(p.items) 299 { 300 d.initData(p.items); 301 } 302 return t; 303 }; 304 305 var docloaded = false; 306 $(document).ready(function () {docloaded = true} ); 307 //初始化 308 $.fn.twowaylist = function(p) { 309 return this.each( function() { 310 var t = this; 311 if (!docloaded) 312 { 313 $(this).hide(); 314 $(document).ready 315 ( 316 function () 317 { 318 $.addList(t,p); 319 } 320 ); 321 } else { 322 $.addList(this,p); 323 } 324 }); 325 }; //end twowaylist 326 //初始化列表,参数为json array 327 $.fn.twowaylistinit = function(data) { 328 return this.each(function(){ 329 if(this.list) 330 { 331 this.list.initData(data); 332 } 333 }); 334 }; //end twowaylistinit 335 //AJAX提交,获取字段列表,参数如{id:'2'} 336 $.fn.twowaylistsubmit = function(pa) { 337 return this.each(function(){ 338 if(this.list) 339 { 340 this.list.ajaxSubmit(pa); 341 } 342 }); 343 }; //end twowaylistsubmit 344 })(jQuery);