ligerUI---ListBox(列表框可移動的實(shí)例)
寫在前面:
對于可移動的列表框,ligerui中也對其進(jìn)行了封裝,可以直接照著demo拿來用,不過那都是直接在頁面上靜態(tài)初始化的數(shù)據(jù),那么如何從后臺獲取?
前面有了對ligerui的一些組件的使用經(jīng)驗(yàn)后,在這里其實(shí) 對于從后臺獲取數(shù)據(jù)在前臺頁面進(jìn)行顯示,都大同小異。也不是很難。
即要么是在ligerui組件中直接使用其url屬性向后臺發(fā)送請求,要么是單獨(dú)發(fā)送一個ajax請求拿到數(shù)據(jù)后,通過獲取組件,然后設(shè)置其data屬性。嘿嘿。。
下面就直接使用url屬性來發(fā)送請求吧。。。。。
前臺頁面:
<script type="text/javascript">
var box1,box2;
$(function() {
//初始化8個listbox
box1 = $("#listbox1").ligerListBox({
isShowCheckBox: true,
isMultiSelect: true,
height: 140,
//發(fā)送給后臺的請求
url: '${baseURL}/getDeviceByAll.action',
});
box2 = $("#listbox2").ligerListBox({
isShowCheckBox: true,
isMultiSelect: true,
height: 140,
});
var tempData2 = [{id:1,text:"aa"},{id:2,text:"bb"}];
//button點(diǎn)擊事件
$("#btn1").click(function(){
setListBoxData(tempData2);
});
});
function setListBoxData(tempData2){
//貌似只能通過id來移除了 用removeItems不可以達(dá)到效果
//例如移除id為1,2的然后添加到左邊
for(var i=0;i<tempData2.length;i++){
box1.removeItem(tempData2[i].id);
}
box2.addItems(tempData2);
}
//===========listbox四個按鈕點(diǎn)擊相關(guān)函數(shù)===========
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="點(diǎn)擊" 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對應(yīng)的數(shù)據(jù)格式要有text、id字段
obj.put("id",device.getDevId());
obj.put("text",device.getDevName());
jsonArray.add(obj);
}
return SUCCESS;
}
好啦,這樣就成功了,當(dāng)然 我這里是省略了后臺如何將json數(shù)據(jù)傳遞到前臺,因?yàn)樵谖覍憀igerui的其他組件(ligerGrid,ligerForm)的時候已經(jīng)寫過了,就不再重復(fù)說了
效果演示截圖:(省略向左向右的移動效果圖)

在不勾選數(shù)據(jù)的情況下,點(diǎn)擊“點(diǎn)擊”按鈕,的效果圖如下:

其實(shí)在移除的過程中,一開始使用的removeItems()方法,但是測試貌似不可以移除,故采用removeItem()的方法,根據(jù)id來移除。。
相關(guān)文章
Webpack設(shè)置環(huán)境變量的一些誤區(qū)詳解
這篇文章主要給大家介紹了關(guān)于Webpack設(shè)置環(huán)境變量的一些誤區(qū),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Webpack具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
JavaScript高級程序設(shè)計(jì)閱讀筆記(六) ECMAScript中的運(yùn)算符(二)
ECMAScript中的運(yùn)算符,學(xué)習(xí)js的朋友可以參考下2012-02-02
JS實(shí)現(xiàn)為排序好的字符串找出重復(fù)行的方法
這篇文章主要介紹了JS實(shí)現(xiàn)為排序好的字符串找出重復(fù)行的方法,涉及JavaScript字符串運(yùn)算相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-03-03
js在指定位置增加節(jié)點(diǎn)函數(shù)insertBefore()用法實(shí)例
這篇文章主要介紹了js在指定位置增加節(jié)點(diǎn)函數(shù)insertBefore()用法,實(shí)例分析了insertBefore()函數(shù)追加結(jié)點(diǎn)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01
javascript改變position值實(shí)現(xiàn)菜單滾動至頂部后固定
現(xiàn)在很多網(wǎng)站都有這樣的一個效果,當(dāng)頁面滾動到一定高度時,菜單欄會固定在頁面頂部;該效果在 ie6 下不支持,因?yàn)閕e6不支持 position:fixed,效果很不錯,感興趣的朋友可以了解下啊2013-01-01

