From e5aa6c3a877713ca65007b85191d01c864001d04 Mon Sep 17 00:00:00 2001 From: morning-star <1453017105@qq.com> Date: Tue, 26 Dec 2023 22:32:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(transfer):=20=E6=96=B0=E5=A2=9E=20`dblclic?= =?UTF-8?q?k`=20=E9=80=89=E9=A1=B9=20(#1491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(transfer): 新增 `dblclick` 选项 * style(transfer): 优化代码书写风格 --------- Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com> --- docs/transfer/detail/options.md | 24 ++++ src/modules/transfer.js | 240 +++++++++++++++++--------------- 2 files changed, 150 insertions(+), 114 deletions(-) diff --git a/docs/transfer/detail/options.md b/docs/transfer/detail/options.md index c35fd62e..71ee758f 100644 --- a/docs/transfer/detail/options.md +++ b/docs/transfer/detail/options.md @@ -163,6 +163,30 @@ onchange: function(data, index){ `false` + + +
'+ (text || '') +'
'); if(thisDataElem.find('.'+ NONE)[0]){ @@ -286,11 +287,12 @@ layui.define(['laytpl', 'form'], function(exports){ text.replace(/\s/g, '') && thisDataElem.append(createNoneElem); }; - //同步 value 属性值 + // 同步 value 属性值 Class.prototype.setValue = function(){ - var that = this - ,options = that.config - ,arr = []; + var that = this; + var options = that.config; + var arr = []; + that.layBox.eq(1).find('.'+ ELEM_DATA +' input[type="checkbox"]').each(function(){ var isHide = $(this).data('hide'); isHide || arr.push(this.value); @@ -300,14 +302,14 @@ layui.define(['laytpl', 'form'], function(exports){ return that; }; - //解析数据 + // 解析数据 Class.prototype.parseData = function(callback){ - var that = this - ,options = that.config - ,newData = []; + var that = this; + var options = that.config; + var newData = []; layui.each(options.data, function(index, item){ - //解析格式 + // 解析格式 item = (typeof options.parseData === 'function' ? options.parseData(item) : item) || item; @@ -326,11 +328,11 @@ layui.define(['laytpl', 'form'], function(exports){ return that; }; - //获得右侧面板数据 + // 获得右侧面板数据 Class.prototype.getData = function(value){ - var that = this - ,options = that.config - ,selectedData = []; + var that = this; + var options = that.config; + var selectedData = []; that.setValue(); @@ -345,30 +347,30 @@ layui.define(['laytpl', 'form'], function(exports){ return selectedData; }; - //执行穿梭 + // 执行穿梭 Class.prototype.transfer = function (_index, elem) { - var that = this - ,options = that.config - ,thisBoxElem = that.layBox.eq(_index) - ,arr = [] + var that = this; + var options = that.config; + var thisBoxElem = that.layBox.eq(_index); + var arr = []; if (!elem) { - //通过按钮触发找到选中的进行移动 + // 通过按钮触发找到选中的进行移动 thisBoxElem.each(function(_index){ - var othis = $(this) - ,thisDataElem = othis.find('.'+ ELEM_DATA); + var othis = $(this); + var thisDataElem = othis.find('.'+ ELEM_DATA); thisDataElem.children('li').each(function(){ - var thisList = $(this) - ,thisElemCheckbox = thisList.find('input[type="checkbox"]') - ,isHide = thisElemCheckbox.data('hide'); + var thisList = $(this); + var thisElemCheckbox = thisList.find('input[type="checkbox"]'); + var isHide = thisElemCheckbox.data('hide'); if(thisElemCheckbox[0].checked && !isHide){ thisElemCheckbox[0].checked = false; thisBoxElem.siblings('.'+ ELEM_BOX).find('.'+ ELEM_DATA).append(thisList.clone()); thisList.remove(); - //记录当前穿梭的数据 + // 记录当前穿梭的数据 arr.push(thisElemCheckbox[0].value); } @@ -376,15 +378,15 @@ layui.define(['laytpl', 'form'], function(exports){ }); }); } else { - //双击单条记录移动 - var thisList = elem - ,thisElemCheckbox = thisList.find('input[type="checkbox"]') + // 双击单条记录移动 + var thisList = elem; + var thisElemCheckbox = thisList.find('input[type="checkbox"]'); thisElemCheckbox[0].checked = false; thisBoxElem.siblings('.'+ ELEM_BOX).find('.'+ ELEM_DATA).append(thisList.clone()); thisList.remove(); - //记录当前穿梭的数据 + // 记录当前穿梭的数据 arr.push(thisElemCheckbox[0].value); that.setValue(); @@ -392,28 +394,28 @@ layui.define(['laytpl', 'form'], function(exports){ that.renderCheckBtn(); - //穿梭时,如果另外一个框正在搜索,则触发匹配 + // 穿梭时,如果另外一个框正在搜索,则触发匹配 var siblingInput = thisBoxElem.siblings('.'+ ELEM_BOX).find('.'+ ELEM_SEARCH +' input') siblingInput.val() === '' || siblingInput.trigger('keyup'); - //穿梭时的回调 + // 穿梭时的回调 options.onchange && options.onchange(that.getData(arr), _index); } - //事件 + // 事件 Class.prototype.events = function(){ - var that = this - ,options = that.config; + var that = this; + var options = that.config; - //左右复选框 + // 左右复选框 that.elem.on('click', 'input[lay-filter="layTransferCheckbox"]+', function(){ - var thisElemCheckbox = $(this).prev() - ,checked = thisElemCheckbox[0].checked - ,thisDataElem = thisElemCheckbox.parents('.'+ ELEM_BOX).eq(0).find('.'+ ELEM_DATA); + var thisElemCheckbox = $(this).prev(); + var checked = thisElemCheckbox[0].checked; + var thisDataElem = thisElemCheckbox.parents('.'+ ELEM_BOX).eq(0).find('.'+ ELEM_DATA); if(thisElemCheckbox[0].disabled) return; - //判断是否全选 + // 判断是否全选 if(thisElemCheckbox.attr('lay-type') === 'all'){ thisDataElem.find('input[type="checkbox"]').each(function(){ if(this.disabled) return; @@ -428,26 +430,36 @@ layui.define(['laytpl', 'form'], function(exports){ // 双击穿梭 that.elem.on('dblclick', '.' + ELEM_DATA + '>li', function(event){ - var elemThis = $(this) - ,thisElemCheckbox = elemThis.children('input[type="checkbox"]') - ,thisDataElem = elemThis.parent() - ,thisBoxElem = thisDataElem.parent() + var elemThis = $(this); + var thisElemCheckbox = elemThis.children('input[type="checkbox"]'); + var thisDataElem = elemThis.parent(); + var thisBoxElem = thisDataElem.parent(); + var index = thisBoxElem.data('index'); if(thisElemCheckbox[0].disabled) return; - that.transfer(thisBoxElem.data('index'), elemThis); + // 根据 dblclick 回调函数返回值决定是否执行穿梭 --- 2.9.3+ + var ret = typeof options.dblclick === 'function' ? options.dblclick({ + elem: elemThis, + data: that.getData([thisElemCheckbox[0].value])[0], + index: index + }) : null; + + if(ret === false) return; + + that.transfer(index, elemThis); }) // 穿梭按钮事件 that.layBtn.on('click', function(){ - var othis = $(this) - ,_index = othis.data('index') - if(othis.hasClass(DISABLED)) return; + var othis = $(this); + var _index = othis.data('index'); + if(othis.hasClass(DISABLED)) return; that.transfer(_index); }); - //搜索 + // 搜索 that.laySearch.find('input').on('keyup', function(){ var value = this.value; var thisDataElem = $(this).parents('.'+ ELEM_SEARCH).eq(0).siblings('.'+ ELEM_DATA); @@ -472,17 +484,17 @@ layui.define(['laytpl', 'form'], function(exports){ that.renderCheckBtn(); - //无匹配数据视图 + // 无匹配数据视图 var isNone = thisListElem.length === thisDataElem.children('li.'+ HIDE).length; that.noneView(thisDataElem, isNone ? options.text.searchNone : ''); }); }; - //记录所有实例 - thisModule.that = {}; //记录所有实例对象 - thisModule.config = {}; //记录所有实例配置项 + // 记录所有实例 + thisModule.that = {}; // 记录所有实例对象 + thisModule.config = {}; // 记录所有实例配置项 - //重载实例 + // 重载实例 transfer.reload = function(id, options){ var that = thisModule.that[id]; that.reload(options); @@ -490,13 +502,13 @@ layui.define(['laytpl', 'form'], function(exports){ return thisModule.call(that); }; - //获得选中的数据(右侧面板) + // 获得选中的数据(右侧面板) transfer.getData = function(id){ var that = thisModule.that[id]; return that.getData(); }; - //核心入口 + // 核心入口 transfer.render = function(options){ var inst = new Class(options); return thisModule.call(inst);