穿梭框layui.transfer
冰貂主人
2024-04-09 15:59:09
0次
<div id="test1" class="demo-transfer"></div>
<script type="text/javascript">
var leftNum=0;
layui.use(['form','transfer'],function(){
var form=layui.form;
var transfer=layui.transfer;
//transfer基础效果
transfer.render({
elem: '#test1'
,data: {$member}
,title: ['待选', '已选']
,showSearch: true
,id:'key123'
,onchange: function(data, index){
if(data.length>1){
layer.msg('只能选择一条数据',function(){
transfer.reload('key123', {
value:''
});
});
}
if(index==0){
leftNum+=1;
if(leftNum>=2){
leftNum=0;
transfer.reload('key123', {
value:''
});
layer.msg('只能选中一个,请重新选择',{icon:6},function(){
window.location.reload();
})
}
}
}
})
form.on('submit(myform)',function(obj){
var getData = transfer.getData('key123'); //获取右侧数据
obj.field.member_id = getData[0].value ;
fields=JSON.stringify(obj.field);
$.ajax({
url:'insert'
,type:'post'
,data:{'val':fields}
,success:function(data){
}
})
return false;
})
})
</script>
//加载添加页面
public function add(){
//从数据库获取所有会员的信息,使用闭包查询
$member=Db::name('member')
->field('id,nickname')
->select();
foreach($member as &$v){
$v['value']=$v['mid'];
$v['title']=$v['nickname'];
unset($v['mid']);
unset($v['nickname']);
}
// 分配变量,输出数组
$this->assign(['member'=>json_encode($member)]);
return view();
}
只能选择一条数据的第二种办法:
,onchange: function(data, index){
if(index==0){
rightnum++;
if(rightnum==1){
$('.layui-transfer-active button').first().hide();
rightnum=0;
}
}
if(index==1){
leftnum++;
if(leftnum==1){
$('.layui-transfer-active button').first().show();
leftnum=0;
}
}
}
相关内容