基于jquery实现左右上下移动效果

最近再做报表时候,直接把全部字段都导出了,但这不太灵活。回忆老师讲过的项目做报表如下左右上下移功能灵活控制,所以就尝试下咯。

基于jquery实现左右上下移动效果

js代码

function selected(thiz) { var name = thiz.name; if(name=="right") $("select[name='left']").val(""); else $("select[name='right']").val(""); } function Shift(thiz) { var right = $("select[name='right']"); var left = $("select[name='left']"); if(thiz=="left" && right.val() != ""){ lrShift(right,left); }else if(thiz=="right" && left.val() != ""){ lrShift(left,right); } //获取选中的值 } //从dest移动到target function lrShift(dest,target) { var childrens = dest.children(); var args = ""; //alert(dest.val()); var dests = dest.val() for(var x = 0; x < dests.length; x++) { var vaTemp = dests[x]; target.append("<option value='"+vaTemp+"'>"+vaTemp+"</option>");//追加 target.find("option[value='"+vaTemp+"']").attr("selected",true);//给追加获取焦点 for(var y = 0; y <childrens.length;y++ )//删除选中的元素 { if(childrens.get(y).value==vaTemp) $(childrens.get(y)).remove(); } } dest.val(""); } function ShiftValue(address) { var right = $("select[name='right']"); var left = $("select[name='left']"); if(right.val()!=null) shift(right,address); else if(left.val()!=null) shift(left,address); } function shift(obj,address){ //获取选中的值 var objData = obj.val(); var childrens = obj.children(); var strs = ""; for(var x = 0; x < objData.length; x++) { strs+="@"+objData[x]; } //获取要添加位置对象 var temp = null; if(address=="top"){ var number = findSelect(childrens,objData[0]); if((--number) < 0) return; temp = childrens.get(number); } else{ var number = findSelect(childrens,objData[objData.length-1]); if((++number) > childrens.length-1) return; temp = childrens.get(number); } //删除选中的值 var n = 0; var buffer = new Array(childrens.length-objData.length); for(var x = 0; x < childrens.length;x++) { var value = childrens.get(x).value; if(strs.indexOf(value)==-1) buffer[n++] = value; } //添加新排序的值 obj.empty(); if(address=="top") { for(var y = 0; y < buffer.length;y++) { if(buffer[y]==temp.value) { for(var x = 0; x < objData.length ; x++) { obj.append("<option>"+objData[x]+"</option>"); } } obj.append("<option>"+buffer[y]+"</option>"); } }else{ for(var y = 0; y < buffer.length;y++) { obj.append("<option>"+buffer[y]+"</option>"); if(buffer[y]==temp.value) { for(var x = 0; x < objData.length; x++) { obj.append("<option>"+objData[x]+"</option>"); } } } } //选中值 obj.val(objData); } function findSelect(selects,objValue) { var number = -1; for(var x = 0; x < selects.length; x++) { if(objValue==selects.get(x).value) number = x; } return number; }

页面调用

<div> <div> <b>未导出字段</b> </div> <div> <select multiple="multiple" onchange="selected(this)"> <option value="姓名">姓名</option> <option value="快件号">快件号</option> <option value="快递公司">快递公司</option> <option value="首重">首重</option> <option value="续重">续重</option> </select> </div> </div> <div> <div> <input type="button" value="<<"/> </div> <div> <input type="button" value=">>"/> </div> </div> <div> <div> <b>需导出字段</b> </div> <div> <select multiple="multiple" onchange="selected(this)"> <option value="首价">首价</option> <option value="续价">续价</option> <option value="大大">大大</option> <option value="小小">小小</option> </select> </div> </div> <div> <div> <input type="button" value="向上" /> </div> <div> <input type="button" value="向下" /> </div> </div>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/62add238a86ded71a58b11d489fdd58a.html