/** @ Name:表格冗余列可展开显示 @ Author:hbm @ License:MIT */ layui.define(['form', 'table'], function (exports) { var $ = layui.$ , table = layui.table , form = layui.form , VERSION = 1.0, MOD_NAME = 'opTable' // 展开 , 关闭 , ON = 'on', OFF = 'off', KEY_STATUS = "status" //openType 0、默认效果同时只展开一项 1、点击展开多项 2、 展开全部 3、关闭全部 , OPEN_DEF = 0, OPEN_NO_CLOSE = 1, OPEN_ALL = 2, CLOSE_ALL = 3 //外部接口 , opTable = { index: layui.opTable ? (layui.opTable.index + 10000) : 0 //设置全局项 , set: function (options) { var that = this; that.config = $.extend({}, that.config, options); return that; } //事件监听 , on: function (events, callback) { return layui.onevent.call(this, MOD_NAME, events, callback); } } // 展开列需要需要显示的数据 数据格式为 每个页面唯一的(LAY_IINDEX)下标绑定数据 对应的数据 , openItemData = {} , getOpenClickClass = function (elem, isAddClickClass) { return elem.replace("#", '').replace(".", '') + (isAddClickClass ? 'opTable-i-table-open' : '') } , getOpenAllClickClass = function (elem) { return getOpenClickClass(elem, true) + "-all" } //操作当前实例 , thisIns = function () { var that = this , options = that.config , id = options.id || options.index; return { reload: function (options) { that.config = $.extend(that.config, options); that.config.cols[0].splice(0, 1); that.render(); return this; } , config: options // 展开全部 , openAll: function () { // 表格 同时只支持展开一项 if (that.config.openTable || this.isOpenAll()) { return; } var def = that.config.openType; that.config.openType = OPEN_ALL; $("." + getOpenClickClass(that.config.elem, true)).parent().click(); that.config.openType = def; $("." + getOpenAllClickClass(that.config.elem)) .addClass("opTable-open-dow") .removeClass("opTable-open-up") .attr(KEY_STATUS, ON); } // 关闭全部 , closeAll: function () { if (!this.isOpenAll()) { return; } var def = that.config.openType; that.config.openType = CLOSE_ALL; $("." + getOpenClickClass(that.config.elem, true)).parent().click(); that.config.openType = def; $("." + getOpenAllClickClass(that.config.elem)) .addClass("opTable-open-up") .removeClass("opTable-open-dow") .attr(KEY_STATUS, ON); } // 通过下标展开一项 , openIndex: function (index) { var dom = $("." + getOpenClickClass(that.config.elem, true)).eq(index); if (dom.length <= 0) { return false; } var status = dom.attr(KEY_STATUS); if (status === ON) { return true; } dom.click(); return true; } // 通过下标展开一项 , closeIndex: function (index) { var dom = $("." + getOpenClickClass(that.config.elem, true)).eq(index); if (dom.length <= 0) { return false; } var status = dom.attr(KEY_STATUS); if (status === OFF) { return true; } dom.click(); return true; } // 当前展开状态取反 , toggleOpenIndex: function (index) { var dom = $("." + getOpenClickClass(that.config.elem, true)).eq(index); if (dom.length <= 0) { return false; } dom.parent().click(); return true; } // 当前是否全部展开 , isOpenAll: function () { var localTag = $("." + getOpenClickClass(that.config.elem, true)); var allTag = localTag.length; var openTag = localTag.parent().parent().parent().parent().find(".opTable-open-dow").length; // 当所有的选项都被展开 则 true return allTag === openTag; } } } //构造器 , Class = function (options) { var that = this; that.index = ++opTable.index; that.config = $.extend({}, that.config, opTable.config, options); that.render(); return this; }; //默认配置 Class.prototype.config = { openType: OPEN_DEF // 展开的item (垂直v|水平h) 排序 , opOrientation: 'v' // layui table 对象 , table: null }; //渲染视图 Class.prototype.render = function () { var that = this , options = that.config , colArr = options.cols[0] , openCols = options.openCols || [] , openNetwork = options.openNetwork || null , openTable = options.openTable || null , done = options.done; // 展开显示表格 同时只支持展开一个 options.openType = openTable ? OPEN_DEF : options.openType; delete options["done"]; // 1、在第一列 插入可展开操作 colArr.splice(0, 0, { align: 'left', width: 50, title: openTable ? '' : '', templet: function (item) { // 解决页面多个表格问题 var cla = getOpenClickClass(options.elem, false); openItemData[cla] = openItemData[cla] || {}; openItemData[cla][item.LAY_INDEX] = item; return ""; } }); // 2、表格Render options.table = table.render( $.extend({ done: function (res, curr, count) { initExpandedListener(); if (done) { done(res, curr, count) } } }, options)); // 3、展开事件 function initExpandedListener() { $("." + getOpenClickClass(options.elem, true)) .parent() .unbind("click") .click(function () { var that = $(this).children() , _this = this , itemIndex = parseInt(that.attr("data")) , bindOpenData = openItemData[that.attr("elem")][itemIndex] , status = that.attr(KEY_STATUS) === 'on' // 操作倒三角 , dowDom = that.parent().parent().parent().parent().find(".opTable-open-dow") // 展开的tr , addTD = that.parent().parent().parent().parent().find(".opTable-open-td"); // 关闭全部 if (options.openType === CLOSE_ALL) { dowDom .addClass("opTable-open-up") .removeClass("opTable-open-dow") .attr(KEY_STATUS, OFF); addTD.slideUp(100, function () { addTD.remove(); }); return; } // 展开全部 if (options.openType === OPEN_ALL) { dowDom .addClass("opTable-open-dow") .removeClass("opTable-open-up") .attr(KEY_STATUS, ON); if (status) { _this.addTR.remove(); } } if (options.openType === OPEN_DEF) { // 关闭类型 var sta = dowDom.attr(KEY_STATUS), isThis = (that.attr("data") === dowDom.attr("data")); //1、关闭展开的 dowDom .addClass("opTable-open-up") .removeClass("opTable-open-dow") .attr(KEY_STATUS, OFF); //2、如果当前 = 展开 && 不等于当前的 关闭 if (sta === ON && isThis) { addTD.slideUp(100, function () { addTD.remove(); }); return; } else { that.attr(KEY_STATUS, OFF); addTD.remove(); } } else if (options.openType === OPEN_NO_CLOSE) { // 1、如果当前为打开,再次点击则关闭 if (status) { that.removeClass("opTable-open-dow"); that.attr(KEY_STATUS, 'off'); this.addTR.find("div").slideUp(100, function () { _this.addTR.remove(); }); return; } } // 把添加的 tr 绑定到当前 移除时使用 this.addTR = $([ "