这篇博客素材的由来是我在玩一个小游戏的时候,它的原理很像穿梭框,而且我觉得在开发中,穿梭框还挺有用的,于是我就去找了layui官网的文档来学习,并且将它分享出来
模块加载名称:transfer 基础的参数: 基础方法:
方法含义transfer.set(options);设定全局默认参数。options 即各项基础参数transfer.getData(id);获得右侧数据transfer.reload(id, options);重载实例js文件 elem:加id data:数据
layui.use(['transfer', 'layer', 'util'], function(){ var $ = layui.$ ,transfer = layui.transfer ,layer = layui.layer ,util = layui.util; //模拟数据 var data1 = [ {"value": "1", "title": "李白"} ,{"value": "2", "title": "杜甫"} ,{"value": "3", "title": "苏轼"} ,{"value": "4", "title": "李清照"} ,{"value": "5", "title": "鲁迅", "disabled": true} ,{"value": "6", "title": "巴金"} ,{"value": "7", "title": "冰心"} ,{"value": "8", "title": "矛盾"} ,{"value": "9", "title": "贤心"} ] ,data2 = [ {"value": "1", "title": "瓦罐汤"} ,{"value": "2", "title": "油酥饼"} ,{"value": "3", "title": "炸酱面"} ,{"value": "4", "title": "串串香", "disabled": true} ,{"value": "5", "title": "豆腐脑"} ,{"value": "6", "title": "驴打滚"} ,{"value": "7", "title": "北京烤鸭"} ,{"value": "8", "title": "烤冷面"} ,{"value": "9", "title": "毛血旺", "disabled": true} ,{"value": "10", "title": "肉夹馍"} ,{"value": "11", "title": "臊子面"} ,{"value": "12", "title": "凉皮"} ,{"value": "13", "title": "羊肉泡馍"} ,{"value": "14", "title": "冰糖葫芦", "disabled": true} ,{"value": "15", "title": "狼牙土豆"} ] //基础效果 transfer.render({ elem: '#test1' ,data: data1 }) });jsp页面
<div id="test1" class="demo-transfer"></div>js文件 elem:加id data:数据 title: 自定义标题 height: 定义高度
layui.use(['transfer', 'layer', 'util'], function(){ var $ = layui.$ ,transfer = layui.transfer ,layer = layui.layer ,util = layui.util; //模拟数据 var data1 = [ {"value": "1", "title": "李白"} ,{"value": "2", "title": "杜甫"} ,{"value": "3", "title": "苏轼"} ,{"value": "4", "title": "李清照"} ,{"value": "5", "title": "鲁迅", "disabled": true} ,{"value": "6", "title": "巴金"} ,{"value": "7", "title": "冰心"} ,{"value": "8", "title": "矛盾"} ,{"value": "9", "title": "贤心"} ] ,data2 = [ {"value": "1", "title": "瓦罐汤"} ,{"value": "2", "title": "油酥饼"} ,{"value": "3", "title": "炸酱面"} ,{"value": "4", "title": "串串香", "disabled": true} ,{"value": "5", "title": "豆腐脑"} ,{"value": "6", "title": "驴打滚"} ,{"value": "7", "title": "北京烤鸭"} ,{"value": "8", "title": "烤冷面"} ,{"value": "9", "title": "毛血旺", "disabled": true} ,{"value": "10", "title": "肉夹馍"} ,{"value": "11", "title": "臊子面"} ,{"value": "12", "title": "凉皮"} ,{"value": "13", "title": "羊肉泡馍"} ,{"value": "14", "title": "冰糖葫芦", "disabled": true} ,{"value": "15", "title": "狼牙土豆"} ] //定义标题及数据源 transfer.render({ elem: '#test2' ,title: ['候选文人', '获奖文人'] //自定义标题 ,data: data1 ,height: 210 //定义高度 }) });jsp页面
<div id="test2" class="demo-transfer"></div>js文件 elem:加id data:数据 value:右侧初始数据的value
transfer.render({ elem: '#test3' ,data: data2 ,value: ["1", "3", "5", "7", "9", "11"] })jsp页面
<div id="test3" class="demo-transfer"></div>js文件 elem:加id data:数据 title:自定义标题 showSearch:是否开启搜索
//显示搜索框 transfer.render({ elem: '#test4' ,data: data1 ,title: ['文本墨客', '获奖文人'] ,showSearch: true })jsp页面
<div id="test4" class="demo-transfer"></div>js文件
//穿梭时的回调 transfer.render({ elem: '#test6' ,data: data1 ,onchange: function(obj, index){ var arr = ['左边', '右边']; layer.alert('来自 <strong>'+ arr[index] + '</strong> 的数据:'+ JSON.stringify(obj)); //获得被穿梭时的数据 } })jsp页面
<div id="test6" class="demo-transfer"></div>数据格式一定要合法 合法的数据:
[ {"value": "1", "title": "李白", "disabled": "", "checked": ""} ,{"value": "2", "title": "杜甫", "disabled": "", "checked": ""} ,{"value": "3", "title": "贤心", "disabled": "", "checked": ""} ]导css文件和js文件,路径,文件名,位置都要弄清楚 如果没有css文件和js文件的话可以去下载: 下载链接