/** @ Name:表格冗余列可展开显示 @ Author:hbm @ License:MIT */ layui.define(['form', 'table'], function (exports) { var $ = layui.$ , table = layui.table , form = layui.form , VERSION = 1.0, MOD_NAME = 'openTable', ELEM = '.layui-openTable', ON = 'on', OFF = 'off' //外部接口 , openTable = { index: layui.openTable ? (layui.openTable.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); } } //操作当前实例 , thisIns = function () { var that = this , options = that.config , id = options.id || options.index; return { reload: function (options) { that.reload.call(that, options); } , config: options } } //构造器 , Class = function (options) { var that = this; that.index = ++openTable.index; that.config = $.extend({}, that.config, openTable.config, options); that.render(); }; //默认配置 Class.prototype.config = { openType: 0 }; //渲染视图 Class.prototype.render = function () { var that = this , options = that.config , colArr = options.cols[0] , openCols = options.openCols || [] , done = options.done; delete options["done"]; // 1、在第一列 插入可展开操作 colArr.splice(0, 0, { align: 'left', width: 50, templet: function (item) { return ""; } }); // 2、表格Render table.render( $.extend({ done: function (res, curr, count) { initExpandedListener(); if (done) { done(res, curr, count) } } }, options)); // 3、展开事件 function initExpandedListener() { //扩大点击事件范围 为父级div $(".openTable-i-table-open") .parent() .unbind() .click(function () { var that = $(this).children(); // 关闭类型 if (options.openType === 0) { var sta = $(".openTable-open-dow").attr("status"), isThis = (that.attr("data") === $(".openTable-open-dow").attr("data")); //1、关闭展开的 $(".openTable-open-dow") .addClass("openTable-open-up") .removeClass("openTable-open-dow") .attr("status", OFF); //2、如果当前 = 展开 && 不等于当前的 关闭 if (sta === ON && isThis) { $(".openTable-open-td").slideUp(100, function () { $(".openTable-open-td").remove(); }); return; } else { that.attr("status", OFF) $(".openTable-open-td").remove(); } } var _this = this , item = JSON.parse(that.attr("data")) , status = that.attr("status") === 'on'; // 1、如果当前为打开,再次点击则关闭 if (status) { that.removeClass("openTable-open-dow"); that.attr("status", 'off'); this.addTR.find("div").slideUp(100, function () { _this.addTR.remove(); }); return; } var html = ["