ligerUI---ListBox(列表框可移动的实例)
程序员文章站
2022-04-09 20:53:59
写在前面:
对于可移动的列表框,ligerui中也对其进行了封装,可以直接照着demo拿来用,不过那都是直接在页面上静态初始化的数据,那么如何从后台获取?
前面有了对l...
写在前面:
对于可移动的列表框,ligerui中也对其进行了封装,可以直接照着demo拿来用,不过那都是直接在页面上静态初始化的数据,那么如何从后台获取?
前面有了对ligerui的一些组件的使用经验后,在这里其实 对于从后台获取数据在前台页面进行显示,都大同小异。也不是很难。
即要么是在ligerui组件中直接使用其url属性向后台发送请求,要么是单独发送一个ajax请求拿到数据后,通过获取组件,然后设置其data属性。嘿嘿。。
下面就直接使用url属性来发送请求吧。。。。。
前台页面:
<script type="text/javascript"> var box1,box2; $(function() { //初始化8个listbox box1 = $("#listbox1").ligerlistbox({ isshowcheckbox: true, ismultiselect: true, height: 140, //发送给后台的请求 url: '${baseurl}/getdevicebyall.action', }); box2 = $("#listbox2").ligerlistbox({ isshowcheckbox: true, ismultiselect: true, height: 140, }); var tempdata2 = [{id:1,text:"aa"},{id:2,text:"bb"}]; //button点击事件 $("#btn1").click(function(){ setlistboxdata(tempdata2); }); }); function setlistboxdata(tempdata2){ //貌似只能通过id来移除了 用removeitems不可以达到效果 //例如移除id为1,2的然后添加到左边 for(var i=0;i<tempdata2.length;i++){ box1.removeitem(tempdata2[i].id); } box2.additems(tempdata2); } //===========listbox四个按钮点击相关函数=========== function movetoleft1() { var selecteds = box2.getselecteditems(); if (!selecteds || !selecteds.length) return; box2.removeitems(selecteds); box1.additems(selecteds); } function movetoright1() { var selecteds = box1.getselecteditems(); if (!selecteds || !selecteds.length) return; box1.removeitems(selecteds); box2.additems(selecteds); } function movealltoleft1() { var selecteds = box2.data; if (!selecteds || !selecteds.length) return; box1.additems(selecteds); box2.removeitems(selecteds); } function movealltoright1() { var selecteds = box1.data; if (!selecteds || !selecteds.length) return; box2.additems(selecteds); box1.removeitems(selecteds); } </script> <style type="text/css"> .middle input { display: block;width:30px; margin:2px; } </style> </head> <body> <div> <div style="float:left;font-size:15px;width:150px;text-align: center">support devices:</div> <div style="margin:4px;float:left;"> <div id="listbox1"></div> </div> <div style="margin:4px;float:left;" class="middle"> <input type="button" onclick="movetoleft1()" value="<" /> <input type="button" onclick="movetoright1()" value=">" /> <input type="button" onclick="movealltoleft1()" value="<<" /> <input type="button" onclick="movealltoright1()" value=">>" /> </div> <div style="margin:4px;float:left;"> <div id="listbox2"></div> </div> </div> <input type="button" value="点击" id="btn1"> </body>
后台action:
private jsonarray jsonarray; public jsonarray getjsonarray() { return jsonarray; } public string getdevicebyall() throws exception{ list<device> devicelist = deviceservice.getall(device.class); jsonarray = new jsonarray(); for(device device:devicelist){ jsonobject obj = new jsonobject(); //listbox对应的数据格式要有text、id字段 obj.put("id",device.getdevid()); obj.put("text",device.getdevname()); jsonarray.add(obj); } return success; }
好啦,这样就成功了,当然 我这里是省略了后台如何将json数据传递到前台,因为在我写ligerui的其他组件(ligergrid,ligerform)的时候已经写过了,就不再重复说了
效果演示截图:(省略向左向右的移动效果图)
在不勾选数据的情况下,点击“点击”按钮,的效果图如下:
其实在移除的过程中,一开始使用的removeitems()方法,但是测试貌似不可以移除,故采用removeitem()的方法,根据id来移除。。
上一篇: JS实现的ajax和同源策略(实例讲解)
下一篇: 洛谷P4012 深海机器人问题(费用流)