插件網(wǎng)址:http://www.jq22.com/jquery-info15984
先引入css和js
html 添加以下代碼
<div class="ue-container">
<select style="height: 100%" multiple="multiple"
size="10" name="doublebox" class="demo">
</select>
</div>添加樣式
<style>
.ue-container {
width: 100%;
margin: 0 auto;
margin-top: 1%;
padding: 20px 30px;
border: 1px solid #ddd;
background: #fff;
}
</style>-
js代碼
Common.ajax({ type: "GET", dataType: 'json', url: "/sys/user/getUnownedRoles", data:{userId:SysUser.userId}, success: function (data) { if (data.code==0) { $.each(data.resultList,function (i,val) { var role={}; role.roleId = val.roleId; role.roleName = val.roleName; roles.push(role); }); $('.ue-container').empty(); $(".ue-container").append("<select style='height: 100%' multiple='multiple' size='10' name='doublebox' class='demo'> </select>"); $('.demo').doublebox({ nonSelectedListLabel: '選擇角色', selectedListLabel: '授權(quán)用戶角色', preserveSelectionOnMove: 'moved', moveOnSelect: false, nonSelectedList:roles, selectedList:MyRoles, optionValue:"roleId", optionText:"roleName", doubleMove:true }); $("#roleModal").modal('show'); } else { layer.msg(data.message,{icon:5}); } } });
保存js
function assign() {
var roles = [];
$("#bootstrap-duallistbox-selected-list_doublebox option").each(function(){
//遍歷所有option
var role ={roleId:$(this).val(),userId:SysUser.userId};
roles.push(role);
});
var user ={};
user.userId = SysUser.userId;
user.userRoleList = roles;
Common.ajax({
type:"POST",
url:"/sys/user/roleAssign",
data: JSON.stringify(user),//對象轉(zhuǎn)json
dataType:"json",
contentType: 'application/json',
traditional: true, //必須有,阻止深度序列化
success: function(data){
if (data.code==0) {
layer.msg(data.message,{icon:1,time:1000}, function() {
$("#roleModal").modal('hide');
});
} else {
layer.msg(data.message,{icon:5});
}
}
});
}