kdayun-compnent-develop/kdayun-compnent-pack/dist/libs/layui/lay/modules/table.js

3683 lines
173 KiB
JavaScript
Raw Normal View History

2022-02-19 20:37:32 +08:00
/**
@Namelayui.table 表格操作
@Author贤心
@LicenseMIT
@version:2.4.5
*/
layui.define(['laytpl', 'laypage', 'layer', 'form', 'util'], function (exports) {
"use strict";
var $ = layui.$
, laytpl = layui.laytpl
, laypage = layui.laypage
, layer = layui.layer
, form = layui.form
, util = layui.util
, hint = layui.hint()
, device = layui.device()
//外部接口
, table = {
config: {
checkName: 'LAY_CHECKED' //是否选中状态的字段名
, indexName: 'LAY_TABLE_INDEX' //下标索引名
} //全局配置项
, cache: {} //数据缓存
, checkedMultiList: [] // 记录勾选数据缓存
, index: layui.table ? (layui.table.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);
}
}
//操作当前实例
, thisTable = function () {
var that = this
, options = that.config
, id = options.id || options.index;
id && (thisTable.config[id] = options);
return {
reload: function (options) {
that.reload.call(that, options);
that.getSortcols();
return thisTable.config[id] = $.extend({}, that.config, options);
}
, setColsWidth: function () {
that.setColsWidth.call(that);
}
, resize: function () { //重置表格尺寸/结构
that.resize.call(that);
}
, config: options
}
}
//字符常量
, MOD_NAME = 'table', ELEM = '.layui-table', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled', NONE = 'layui-none'
, ELEM_VIEW = 'layui-table-view', ELEM_TOOL = '.layui-table-tool', ELEM_BOX = '.layui-table-box', ELEM_INIT = '.layui-table-init', ELEM_HEADER = '.layui-table-header', ELEM_BODY = '.layui-table-body', ELEM_MAIN = '.layui-table-main', ELEM_FIXED = '.layui-table-fixed', ELEM_FIXL = '.layui-table-fixed-l', ELEM_FIXR = '.layui-table-fixed-r', ELEM_TOTAL = '.layui-table-total', ELEM_PAGE = '.layui-table-page', ELEM_SORT = '.layui-table-sort', ELEM_EDIT = 'layui-table-edit', ELEM_HOVER = 'layui-table-hover'
//thead区域模板
, TPL_HEADER = function (mode, options) {
var rowCols = '{{#if(item2.colspan){}} colspan="{{item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{item2.rowspan}}"{{#}}}';
options = options || {};
// 判断否是卡片模式
if (mode == "cardListMode") {
return ['<ul class="tab-sort">'
, '{{# layui.each(d.data.sortcols, function(i1, item1){ }}'
, '{{# layui.each(item1, function(i2, item2){ }}'
, '<li data-field="{{ item2.field||i2 }}" data-key="{{# if(item2.key){ }}{{d.index}}-{{item2.key}}{{# } else { }}{{d.index}}-{{i1}}-{{i2}} {{# } }}" class="{{# if(item2.hide){ }}layui-hide{{# } }}">'
, '<div class="layui-table-cell laytable-cell-'
, '{{# if(item2.key){ }}{{d.index}}-{{item2.key}}{{# } else { }}{{d.index}}-{{i1}}-{{i2}} {{# } }}'
, '{{# if(item2.type !== "normal"){ }}'
, ' laytable-cell-{{ item2.type }}'
, '{{# } }}'
, '"{{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
, '{{# if(item2.type === "checkbox"){ }}' //复选框
, '<span style="margin-right:5px"><input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose"></span>'
, '{{# } }}'
, '<span>{{item2.title||""}}</span>'
, '{{# if(item2.type != "checkbox"){ }}' //复选框
, '<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc" title="升序"></i><i class="layui-edge layui-table-sort-desc" title="降序"></i></span>'
, '{{# } }}'
, '</div>'
, '</li>'
, '{{# }); }}'
, '{{# }); }}'
, '</ul>'].join('');
} else {
return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
, '{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
, '<thead>'
, '{{# layui.each(d.data.cols, function(i1, item1){ }}'
, '<tr>'
, '{{# layui.each(item1, function(i2, item2){ }}'
, '{{# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}'
, '{{# if(item2.fixed === "right"){ right = true; } }}'
, function () {
if (options.fixed && options.fixed !== 'right') {
return '{{# if(item2.fixed && item2.fixed !== "right"){ }}';
}
if (options.fixed === 'right') {
return '{{# if(item2.fixed === "right"){ }}';
}
return '';
}()
, '{{# var isSort = !(item2.colGroup) && item2.sort; }}'
, '<th data-field="{{ item2.field||i2 }}" data-key="{{d.index}}-{{i1}}-{{i2}}" {{# if( item2.parentKey){ }}data-parentkey="{{ item2.parentKey }}"{{# } }} {{# if(item2.minWidth){ }}data-minwidth="{{item2.minWidth}}"{{# } }} ' + rowCols + ' {{# if(item2.unresize || item2.colGroup){ }}data-unresize="true"{{# } }} class="{{# if(item2.hide){ }}layui-hide{{# } }}{{# if(isSort){ }} layui-unselect{{# } }}{{# if(!item2.field){ }} layui-table-col-special{{# } }}">'
, '<div class="layui-table-cell laytable-cell-'
, '{{# if(item2.colGroup){ }}'
, 'group'
, '{{# } else { }}'
, '{{d.index}}-{{i1}}-{{i2}}'
, '{{# if(item2.type !== "normal"){ }}'
, ' laytable-cell-{{ item2.type }}'
, '{{# } }}'
, '{{# } }}'
, '" {{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
, '{{# if(item2.type === "checkbox"){ }}' //复选框
, '<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose" {{# if(item2[d.data.checkName]){ }}checked{{# }; }}>'
, '{{# } else { }}'
, '<span>{{item2.title||""}}</span>'
, '{{# if(isSort){ }}'
, '<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc" title="升序"></i><i class="layui-edge layui-table-sort-desc" title="降序"></i></span>'
, '{{# } }}'
, '{{# } }}'
, '</div>'
, '</th>'
, (options.fixed ? '{{# }; }}' : '')
, '{{# }); }}'
, '</tr>'
, '{{# }); }}'
, '</thead>'
, '</table>'].join('');
}
}
//tbody区域模板
, TPL_BODY = function (mode) {
// 判断否是卡片模式
if (mode == "cardListMode") {
return ['<div class="layui-table card-list layui-row layui-col-space10" '
, '{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
, '</div>'].join('')
} else {
return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
, '{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
, '<tbody></tbody>'
, '</table>'].join('')
}
}
//主模板
, TPL_MAIN = function (mode) {
// 判断否是卡片模式
if (mode == "cardListMode") {
return ['<div class="layui-form layui-border-box {{d.VIEW_CLASS}}" lay-filter="LAY-table-{{d.index}}" lay-id="{{ d.data.id }}" style="{{# if(d.data.width){ }}width:{{d.data.width}}px;{{# } }} {{# if(d.data.height){ }}height:{{d.data.height}}px;{{# } }}">'
, '{{# if(d.data.toolbar){ }}'
, '<div class="layui-table-tool">'
, '<div class="layui-table-tool-temp"></div>'
, '<div class="layui-table-tool-self"></div>'
, '</div>'
, '{{# } }}'
, '<div class="layui-table-box">'
, '{{# if(d.data.loading){ }}'
, '<div class="layui-table-init" style="background-color: #fff;">'
, '<i class="layui-icon layui-icon-loading layui-icon"></i>'
, '</div>'
, '{{# } }}'
, '{{# var left, right; }}'
, '<div class="layui-table-header">'
, TPL_HEADER(mode)
, '</div>'
, '<div class="layui-table-body layui-table-main">'
, TPL_BODY(mode)
, '</div>'
, '</div>'
, '{{# if(d.data.totalRow){ }}'
, '<div class="layui-table-total">'
, '<div class="layui-table-total-box" style="overflow: hidden; padding:5px 0">'
, '<div class="layui-table-cell" style="visibility: hidden;">Total</div>'
, '</div>'
, '</div>'
, '{{# } }}'
, '{{# if(d.data.page){ }}'
, '<div class="layui-table-page">'
, '<div id="layui-table-page{{d.index}}"></div>'
, '</div>'
, '{{# } }}'
, '<style>'
, '</style>'
, '</div>'].join('')
} else {
return ['<div class="layui-form layui-border-box {{d.VIEW_CLASS}}" lay-filter="LAY-table-{{d.index}}" style="{{# if(d.data.width){ }}width:{{d.data.width}}px;{{# } }} {{# if(d.data.height){ }}height:{{d.data.height}}px;{{# } }}">'
, '{{# if(d.data.toolbar){ }}'
, '<div class="layui-table-tool">'
, '<div class="layui-table-tool-temp"></div>'
, '<div class="layui-table-tool-self"></div>'
, '</div>'
, '{{# } }}'
, '<div class="layui-table-box">'
, '{{# if(d.loading){ }}'
, '<div class="layui-table-init" style="background-color: #fff;">'
, '<i class="layui-icon layui-icon-loading layui-icon"></i>'
, '</div>'
, '{{# } }}'
, '{{# var left, right; }}'
, '<div class="layui-table-header">'
, TPL_HEADER(mode)
, '</div>'
, '<div class="layui-table-body layui-table-main">'
, TPL_BODY(mode)
, '</div>'
, '{{# if(left){ }}'
, '<div class="layui-table-fixed layui-table-fixed-l">'
, '<div class="layui-table-header">'
, TPL_HEADER(mode, { fixed: true })
, '</div>'
, '<div class="layui-table-body">'
, TPL_BODY(mode)
, '</div>'
, '</div>'
, '{{# }; }}'
, '{{# if(right){ }}'
, '<div class="layui-table-fixed layui-table-fixed-r">'
, '<div class="layui-table-header">'
, TPL_HEADER(mode, { fixed: 'right' })
, '<div class="layui-table-mend"></div>'
, '</div>'
, '<div class="layui-table-body">'
, TPL_BODY(mode)
, '</div>'
, '</div>'
, '{{# }; }}'
, '</div>'
, '{{# if(d.data.totalRow && d.data.mode!="cardListMode"){ }}'
, '<div class="layui-table-total">'
, '<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
, '{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
, '<tbody><tr><td><div class="layui-table-cell" style="visibility: hidden;">Total</div></td></tr></tbody>'
, '</table>'
, '</div>'
, '{{# } }}'
, '{{# if(d.data.page){ }}'
, '<div class="layui-table-page">'
, '<div id="layui-table-page{{d.index}}"></div>'
, '</div>'
, '{{# } }}'
, '<style>'
, '{{# layui.each(d.data.cols, function(i1, item1){'
, 'layui.each(item1, function(i2, item2){ }}'
, '.laytable-cell-{{d.index}}-{{i1}}-{{i2}}{ '
, '{{# if(item2.width){ }}'
, 'width: {{item2.width}}px;'
, '{{# } }}'
, ' }'
, '{{# });'
, '}); }}'
, '</style>'
, '</div>'].join('')
}
}
, _WIN = $(window)
, _DOC = $(document)
//构造器
, Class = function (options) {
var that = this;
that.index = ++table.index;
//响应数据的自定义格式
options.response = $.extend({
statusName: 'code'
, statusCode: 0
, msgName: 'msg'
, dataName: 'data'
, countName: 'count'
}, options.response);
if (!options.sortcols && options.mode == 'cardListMode') {
var arrs = [];
//重新整理排序结构
layui.each(options.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
if (item2.field && item2.type != "image") {
arrs.push(item2)
}
});
});
that.config.sortcols = that.config.sortcols.concat([arrs]);
}
that.config = $.extend({}, that.config, table.config, options);
//设置用户私有化参数
var upd = that.getUserPdata();
if (upd) {
that.setUserPdata(upd);
}
that.render();
};
//默认配置
Class.prototype.config = {
limit: 20 //每页显示的数量
, loading: true //请求数据时是否显示loading
, cellMinWidth: 60 //所有单元格默认最小宽度
, defaultToolbar: ['filter', 'exports', 'print'] //工具栏右侧图标
, mode: 'listMode'
, sortcols: [[ //设置卡片列表排序字段
{ type: 'checkbox', title: '全选' }
]]
, vertical: false // 卡片状态
, text: {
none: '无数据'
}
// 翻页勾选参数
, flipCheck: false // 是否开启翻页勾选
, onlyKey: 'RWID' // 数据唯一性字段
, attachFieldsName: [] // 对选中的数据保留指定的字段
};
//表格渲染
Class.prototype.render = function () {
var that = this
, options = that.config;
options.elem = $(options.elem);
options.where = options.where || {};
options.id = options.id || options.elem.attr('id') || options.index;
//点击排序是否要重新加载表格数据
options.sortload = options.sortload == undefined ? true : options.sortload;
//请求参数的自定义格式
options.request = $.extend({
pageName: 'page'
, limitName: 'limit'
}, options.request)
//如果 page 传入 laypage 对象
if (typeof options.page === 'object') {
options.limit = options.page.limit || options.limit;
options.limits = options.page.limits || options.limits;
that.page = options.page.curr = options.page.curr || 1;
delete options.page.elem;
// 如果是根据数据集来渲染着不需要吧翻页得回调事件删除掉
// if(!options.dataSource){
delete options.page.jump;
// }
}
if (!options.elem[0]) return that;
//高度铺满full-差距值
if (options.height && /^full-\d+$/.test(options.height)) {
that.fullHeightGap = options.height.split('-')[1];
options.height = _WIN.height() - that.fullHeightGap;
}
//初始化一些参数
that.setInit();
//开始插入替代元素
var othis = options.elem
, img_table = ' layui-table-ext'
, elem_view = (options.mode == 'listMode' ? ELEM_VIEW : (ELEM_VIEW + img_table))
, hasRender = othis.next('.' + ELEM_VIEW)
//主容器
, reElem = that.elem = $(laytpl(TPL_MAIN(options.mode)).render({
VIEW_CLASS: elem_view
, data: options
, index: that.index //索引
}));
options.index = that.index;
//生成替代元素
hasRender[0] && hasRender.remove(); //如果已经渲染则Rerender
othis.after(reElem);
//各级容器
that.layTool = reElem.find(ELEM_TOOL);
that.layBox = reElem.find(ELEM_BOX);
that.layHeader = reElem.find(ELEM_HEADER);
that.layMain = reElem.find(ELEM_MAIN);
that.layBody = reElem.find(ELEM_BODY);
that.layFixed = reElem.find(ELEM_FIXED);
that.layFixLeft = reElem.find(ELEM_FIXL);
that.layFixRight = reElem.find(ELEM_FIXR);
that.layTotal = reElem.find(ELEM_TOTAL);
that.layPage = reElem.find(ELEM_PAGE);
//初始化工具栏
that.renderToolbar();
//让表格平铺
that.fullSize();
//如果多级表头,则填补表头高度
if (options.cols.length > 1) {
//补全高度
var th = that.layFixed.find(ELEM_HEADER).find('th');
th.height(that.layHeader.height() - 1 - parseFloat(th.css('padding-top')) - parseFloat(th.css('padding-bottom')));
}
that.pullData(that.page); //请求数据
that.events(); //事件
};
// 卡片模式获取排序的字段
Class.prototype.getSortcols = function (arr) {
var that = this,
options = that.config;
var unique = function (array) {
var temp = [];
var index = [];
var l = array.length;
for (var i = 0; i < l; i++) {
for (var j = i + 1; j < l; j++) {
if (array[i].field === array[j].field) {
i++;
j = i;
}
}
temp.push(array[i]);
index.push(i);
}
return temp;
}
if (options.sortcols[0].length == 1 && options.mode == 'cardListMode') {
var lis = [];
that.eachCols(function (i, item) {
if (item.field && item.type == 'normal') {
var sortStr = { field: item.field, title: item.title, hide: item.hide, key: item.key };
lis.push(sortStr);
}
});
var sortcolsArry = [];
var lisArry = that.config.sortcols[0].concat(lis);
var lisArry1 = unique(lisArry);
sortcolsArry.push(lisArry1);
that.config.sortcols = sortcolsArry;
}
};
//获取用户私有数据:自定义列
Class.prototype.getUserPdata = function () {
var that = this,
options = that.config;
//是否配置了用户私有数据id
if (options && options.updId) {
var params = {
'updId': options.updId
}
// that.loadingnews(true);
var updColumns;
$.ajax({
type: 'post',
url: layui.cache['contentPath'] + '/admin/coreupd/query',
async: false,
// contentType: options.contentType,
data: params,
dataType: 'json',
success: function (res) {
//如果有数据解析的回调,则获得其返回的数据
if (res && res[options.response.statusName] == options.response.statusCode) {
updColumns = res[options.response.dataName] || [];
}
},
error: function (e, m) {
//请求失败,不做任何处理
}
});
if (updColumns) return updColumns;
}
}
//设置用户私有数据:自定义列
Class.prototype.setUserPdata = function (updColumns) {
var that = this,
options = that.config;
if (options && options.updId && updColumns) {
var datas = {};
layui.each(updColumns, function (i1, data1) {
if (data1 && data1.UPD_CONTENT) {
data1 = eval('(' + data1.UPD_CONTENT + ')');
layui.each(data1, function (i2, data2) {
if (data2 && data2.field) {
datas[data2.field] = data2;
}
});
}
});
if (!that.config.userPdatas) {
$.extend(that.config, { 'userPdatas': {} });
}
$.extend(that.config.userPdatas, datas);
//设置自定义列
layui.each(options.cols, function (c1, item1) {
layui.each(item1, function (c2, item2) {
if (item2.field && datas[item2.field]) {
// that.config.cols[c1][c2].hide = datas[item2.field].hide;
item2.hide = datas[item2.field].hide;
}
});
});
}
}
//保存用户私有数据:自定义列
Class.prototype.saveUserPdata = function (updColumns) {
var that = this,
options = that.config;
if (options && options.updId && updColumns) {
var datas = {};
layui.each(updColumns, function (i2, data2) {
if (data2 && data2.field) {
datas[data2.field] = data2;
}
});
if (!that.config.userPdatas) {
$.extend(that.config, { 'userPdatas': {} });
}
$.extend(that.config.userPdatas, datas);
var updCols = [];
$.each(that.config.userPdatas, function (i, t) {
updCols.push(t);
});
var params = {
'UPD_CONTENT': JSON.stringify(updCols),
'UPD_SHIYCJID': options.updId,
'UPD_SHIFQY': 1
}
$.ajax({
type: 'post',
url: layui.cache['contentPath'] + '/admin/coreupd/modify',
data: params,
dataType: 'json',
success: function (res) {
//请求成功,不做任何处理
},
error: function (e, m) {
//请求失败,不做任何处理
}
});
}
}
//根据列类型,定制化参数
Class.prototype.initOpts = function (item) {
var that = this
, options = that.config
, initWidth = {
checkbox: 48
, radio: 48
, space: 15
, numbers: 40
};
//让 type 参数兼容旧版本
if (item.checkbox) item.type = "checkbox";
if (item.space) item.type = "space";
if (!item.type) item.type = "normal";
if (item.type !== "normal") {
item.unresize = true;
item.width = item.width || initWidth[item.type];
}
};
//初始化一些参数
Class.prototype.setInit = function (type) {
var that = this
, options = that.config;
options.clientWidth = options.width || function () { //获取容器宽度
//如果父元素宽度为0一般为隐藏元素则继续查找上层元素直到找到真实宽度为止
var getWidth = function (parent) {
var width, isNone;
parent = parent || options.elem.parent()
width = parent.width();
try {
isNone = parent.css('display') === 'none';
} catch (e) { }
if (parent[0] && (!width || isNone)) return getWidth(parent.parent());
return width;
};
return getWidth();
}();
if (type === 'width') return options.clientWidth;
//初始化列参数
layui.each(options.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
//如果列参数为空,则移除
if (!item2) {
item1.splice(i2, 1);
return;
}
item2.key = i1 + '-' + i2;
item2.hide = item2.hide || false;
//设置列的父列索引
//如果是组合列,则捕获对应的子列
if (item2.colGroup || item2.colspan > 1) {
var childIndex = 0;
layui.each(options.cols[i1 + 1], function (i22, item22) {
//如果子列已经被标注为{HAS_PARENT},或者子列累计 colspan 数等于父列定义的 colspan则跳出当前子列循环
if (item22.HAS_PARENT || (childIndex > 1 && childIndex == item2.colspan)) return;
item22.HAS_PARENT = true;
item22.parentKey = i1 + '-' + i2;
childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
});
item2.colGroup = true; //标注是组合列
}
//根据列类型,定制化参数
that.initOpts(item2);
});
});
};
//初始工具栏
Class.prototype.renderToolbar = function () {
var that = this
, options = that.config
//添加工具栏左侧模板
var leftDefaultTemp = [
'<div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>'
, '<div class="layui-inline" lay-event="update"><i class="layui-icon layui-icon-edit"></i></div>'
, '<div class="layui-inline" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>'
].join('')
, elemToolTemp = that.layTool.find('.layui-table-tool-temp');
if (options.toolbar === 'default') {
elemToolTemp.html(leftDefaultTemp);
} else if (options.toolbar) {
var toolbarHtml = $(options.toolbar).html() || '';
toolbarHtml && elemToolTemp.html(
laytpl(toolbarHtml).render(options)
);
}
//添加工具栏右侧面板
var layout = {
filter: {
title: '筛选列'
, layEvent: 'LAYTABLE_COLS'
, icon: 'layui-icon-cols'
}
, exports: {
title: '导出'
, layEvent: 'LAYTABLE_EXPORT'
, icon: 'layui-icon-export'
}
, print: {
title: '打印'
, layEvent: 'LAYTABLE_PRINT'
, icon: 'layui-icon-print'
}
}, iconElem = [];
if (typeof options.defaultToolbar === 'object') {
layui.each(options.defaultToolbar, function (i, item) {
var thisItem = layout[item];
if (thisItem) {
iconElem.push('<div class="layui-inline" title="' + thisItem.title + '" lay-event="' + thisItem.layEvent + '">'
+ '<i class="layui-icon ' + thisItem.icon + '"></i>'
+ '</div>');
}
});
}
that.layTool.find('.layui-table-tool-self').html(iconElem.join(''));
var elemtoolSelf = that.layTool.find('.layui-table-tool-self');
elemToolTemp.css("padding-right", elemtoolSelf.width() + 17 + 'px');
}
//同步表头父列的相关值
Class.prototype.setParentCol = function (hide, parentKey) {
var that = this
, options = that.config
, parentTh = that.layHeader.find('th[data-key="' + options.index + '-' + parentKey + '"]') //获取父列元素
, parentColspan = parseInt(parentTh.attr('colspan')) || 0;
if (parentTh[0]) {
var arrParentKey = parentKey.split('-')
, getThisCol = options.cols[arrParentKey[0]][arrParentKey[1]];
hide ? parentColspan-- : parentColspan++;
parentTh.attr('colspan', parentColspan);
parentTh[parentColspan < 1 ? 'addClass' : 'removeClass'](HIDE);
getThisCol.colspan = parentColspan; //同步 colspan 参数
getThisCol.hide = parentColspan < 1; //同步 hide 参数
//递归,继续往上查询是否有父列
var nextParentKey = parentTh.data('parentkey');
nextParentKey && that.setParentCol(hide, nextParentKey);
}
};
//多级表头补丁
Class.prototype.setColsPatch = function () {
var that = this
, options = that.config
//同步表头父列的相关值
layui.each(options.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
if (item2.hide) {
that.setParentCol(item2.hide, item2.parentKey);
}
});
});
};
//动态分配列宽
Class.prototype.setColsWidth = function () {
var that = this
, options = that.config
, colNums = 0 //列个数
, autoColNums = 0 //自动列宽的列个数
, autoWidth = 0 //自动列分配的宽度
, countWidth = 0 //所有列总宽度和
, cntrWidth = that.setInit('width');
//统计列个数
that.eachCols(function (i, item) {
item.hide || colNums++;
});
//减去边框差和滚动条宽
cntrWidth = cntrWidth - function () {
return (options.skin === 'line' || options.skin === 'nob') ? 2 : colNums + 1;
}() - that.getScrollWidth(that.layMain[0]) - 1;
//计算自动分配的宽度
var getAutoWidth = function (back) {
//遍历所有列
layui.each(options.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
var width = 0
, minWidth = item2.minWidth || options.cellMinWidth; //最小宽度
if (!item2) {
item1.splice(i2, 1);
return;
}
if (item2.colGroup || item2.hide) return;
if (!back) {
width = item2.width || 0;
if (/\d+%$/.test(width)) { //列宽为百分比
width = Math.floor((parseFloat(width) / 100) * cntrWidth);
width < minWidth && (width = minWidth);
} else if (!width) { //列宽未填写
item2.width = width = 0;
autoColNums++;
}
} else if (autoWidth && autoWidth < minWidth) {
autoColNums--;
width = minWidth;
}
if (item2.hide) width = 0;
countWidth = countWidth + width;
});
});
//如果未填充满,则将剩余宽度平分
(cntrWidth > countWidth && autoColNums) && (
autoWidth = (cntrWidth - countWidth) / autoColNums
);
}
getAutoWidth();
getAutoWidth(true); //重新检测分配的宽度是否低于最小列宽
//记录自动列数
that.autoColNums = autoColNums;
//设置列宽
that.eachCols(function (i3, item3) {
var minWidth = item3.minWidth || options.cellMinWidth;
if (item3.colGroup || item3.hide) return;
//给位分配宽的列平均分配宽
if (item3.width === 0) {
that.getCssRule(options.index + '-' + item3.key, function (item) {
item.style.width = Math.floor(autoWidth >= minWidth ? autoWidth : minWidth) + 'px';
});
}
//给设定百分比的列分配列宽
else if (/\d+%$/.test(item3.width)) {
that.getCssRule(options.index + '-' + item3.key, function (item) {
item.style.width = Math.floor((parseFloat(item3.width) / 100) * cntrWidth) + 'px';
});
}
});
//填补 Math.floor 造成的数差
var patchNums = that.layMain.width() - that.getScrollWidth(that.layMain[0])
- that.layMain.children('table').outerWidth();
if (that.autoColNums && patchNums >= -colNums && patchNums <= colNums) {
var getEndTh = function (th) {
var field;
th = th || that.layHeader.eq(0).find('thead th:last-child')
field = th.data('field');
if (!field && th.prev()[0]) {
return getEndTh(th.prev())
}
return th
}
, th = getEndTh()
, key = th.data('key');
that.getCssRule(key, function (item) {
var width = item.style.width || th.outerWidth();
item.style.width = (parseFloat(width) + patchNums) + 'px';
//二次校验,如果仍然出现横向滚动条(通常是 1px 的误差导致)
if (that.layMain.height() - that.layMain.prop('clientHeight') > 0) {
item.style.width = (parseFloat(item.style.width) - 1) + 'px';
}
});
}
that.loading(!0);
};
//重置表格尺寸/结构
Class.prototype.resize = function () {
var that = this;
that.fullSize(); //让表格铺满
that.setColsWidth(); //自适应列宽
that.scrollPatch(); //滚动条补丁
};
//表格完整重载
Class.prototype.reload = function (options) {
var that = this,
opts = that.config;
if (that.config.data && that.config.data.constructor === Array) delete that.config.data;
//判断是否切换模式
var bChangeMode = false;
if (opts.updId && opts.mode && options.mode && opts.mode != options.mode) {
bChangeMode = true;
}
that.config = $.extend({}, that.config, options);
//同步获取用户私有数据
if (bChangeMode) {
//设置用户私有化参数
var updDatas = that.getUserPdata();
if (updDatas) {
that.setUserPdata(updDatas);
}
}
that.render();
};
//页码
Class.prototype.page = 1;
//获得数据
Class.prototype.pullData = function (curr) {
var that = this
, options = that.config
, request = options.request
, response = options.response
, sort = function () {
if (typeof options.initSort === 'object') {
// 由于数据源可以多个字段排序,所以排序标识需要循环出来
if (options.dataSource && options.initSort) {
for (var key in options.initSort) {
that.sort(key, options.initSort[key]);
}
} else {
that.sort(options.initSort.field, options.initSort.type);
}
}
};
that.startTime = new Date().getTime(); //渲染开始时间
if (options.url) { //Ajax请求
var params = {};
if (options.page) {
params[request.pageName] = String(curr);
params[request.limitName] = String(options.limit);
}
//参数
var data = $.extend(params, options.where);
if (options.contentType && options.contentType.indexOf("application/json") == 0) { //提交 json 格式
data = JSON.stringify(data);
}
$.ajax({
type: options.method || 'get'
, url: options.url
, contentType: options.contentType
, traditional: options.traditional
, data: data
, dataType: 'json'
, headers: options.headers || {}
, success: function (res) {
//如果有数据解析的回调,则获得其返回的数据
if (typeof options.parseData === 'function') {
res = options.parseData(res) || res;
}
//检查数据格式是否符合规范
if (res[response.statusName] != response.statusCode) {
that.renderForm();
that.layMain.html('<div class="' + NONE + '">' + (
res[response.msgName] ||
('返回的数据不符合规范,正确的成功状态码 (' + response.statusName + ') 应为:' + response.statusCode)
) + '</div>');
} else {
that.renderData(res, curr, res[response.countName]), sort();
options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
}
that.setColsWidth();
typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
}
, error: function (e, m) {
that.layMain.html('<div class="' + NONE + '">数据接口请求异常:' + m + '</div>');
that.renderForm();
that.setColsWidth();
}
});
} else if (options.dataSource) { //根据数据集渲染
var res = {}
, startLimit = curr * options.limit - options.limit;
if (typeof options.parseData === 'function') {
res = options.parseData(res) || res;
}
res[response.dataName] = options.data;
res[response.countName] = options.count;
that.renderData(res, curr, res[response.countName]), sort();
options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
that.setColsWidth();
typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
} else if (options.data && options.data.constructor === Array) { //已知数据
var res = {}
, startLimit = curr * options.limit - options.limit
res[response.dataName] = options.data.concat().splice(startLimit, options.limit);
res[response.countName] = options.data.length;
that.renderData(res, curr, options.data.length), sort();
that.setColsWidth();
typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
}
};
//遍历表头
Class.prototype.eachCols = function (callback) {
var that = this;
table.eachCols(null, callback, that.config.cols);
return that;
};
//遍历统计内容,用于卡片列表
Class.prototype.eachTotalCols = function (callback) {
var that = this;
table.eachTotalCols(null, callback, that.config.totalcols);
return that;
};
//数据渲染
Class.prototype.renderData = function (res, curr, count, sort) {
var that = this
, options = that.config
, data = res[options.response.dataName] || []
, trs = []
, trs_fixed = []
, trs_fixed_r = []
//渲染视图
, render = function () { //后续性能提升的重点
var thisCheckedRowIndex;
if (!sort && that.sortKey) {
return that.sort(that.sortKey.field, that.sortKey.sort, true);
}
layui.each(data, function (i1, item1) {
var tds = [], tds_fixed = [], tds_fixed_r = [], divs = [], infotds = []
, numbers = i1 + options.limit * (curr - 1) + 1; //序号
//if (item1.length === 0) return;
if (!sort) {
item1[table.config.indexName] = i1;
}
that.eachCols(function (i3, item3) {
var field = item3.field || i3
, key = options.index + '-' + item3.key
, content = item1[field]
, title = item3.title;
if (content === undefined || content === null) content = '';
if (item3.colGroup) return;
if (options.mode == "imgListMode") {
//td内容
if (item3.type == "checkbox" || item3.type == "radio" || item3.type == "numbers") {
var td = [];
td = ['<td data-field="' + field + '" data-key="' + key + '" ' + function () { //追加各种属性
var attr = [];
if (item3.edit) attr.push('data-edit="' + item3.edit + '"'); //是否允许单元格编辑
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.templet) attr.push('data-content="' + content + '"'); //自定义模板
if (item3.toolbar) attr.push('data-off="true"'); //行工具列关闭单元格事件
if (item3.event) attr.push('lay-event="' + item3.event + '"'); //自定义事件
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
if (item3.minWidth) attr.push('data-minwidth="' + item3.minWidth + '"'); //单元格最小宽度
return attr.join(' ');
}() + ' class="' + function () { //追加样式
var classNames = [];
if (item3.hide) classNames.push(HIDE); //插入隐藏列样式
if (!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
return classNames.join(' ');
}() + '">'
, '<div class="layui-table-cell laytable-cell-' + function () { //返回对应的CSS类标识
return item3.type === 'normal' ? key
: (key + ' laytable-cell-' + item3.type);
}() + '">' + function () {
var tplData = $.extend(true, {
LAY_INDEX: numbers
}, item1)
, checkName = table.config.checkName;
//渲染不同风格的列
switch (item3.type) {
case 'checkbox':
// 分页勾选逻辑 start
var itemValue = item1[that.config.onlyKey]; // 获取唯一值用于判断默认rwid
var ischecked = that.checkedDefault(itemValue, i1);
// 分页勾选逻辑 end
return '<input type="checkbox" ' + ischecked + ' name="layTableCheckbox" lay-skin="primary" ' + function () {
//如果是全选
if (item3[checkName]) {
item1[checkName] = item3[checkName];
return item3[checkName] ? 'checked' : '';
}
return tplData[checkName] ? 'checked' : '';
}() + '>';
break;
case 'radio':
if (tplData[checkName]) {
thisCheckedRowIndex = i1;
}
return '<input type="radio" name="layTableRadio_' + options.index + '" '
+ (tplData[checkName] ? 'checked' : '') + ' lay-type="layTableRadio">';
break;
case 'numbers':
return numbers;
break;
};
}()
, '</div></td>'].join('');
} else {
var info = [];
info = ['<li data-field="' + field + '" data-key="' + key + '" ' + function () { //追加各种属性
var attr = [];
if (item3.edit) attr.push('data-edit="' + item3.edit + '"'); //是否允许单元格编辑
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.templet) attr.push('data-content="' + content + '"'); //自定义模板
if (item3.toolbar) attr.push('data-off="true"'); //行工具列关闭单元格事件
if (item3.event) attr.push('lay-event="' + item3.event + '"'); //自定义事件
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
if (item3.minWidth) attr.push('data-minwidth="' + item3.minWidth + '"'); //单元格最小宽度
return attr.join(' ');
}() + ' class="' + function () { //追加样式
var classNames = [];
if (item3.hide) classNames.push(HIDE); //插入隐藏列样式
if (!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
return classNames.join(' ');
}() + '">'
, '<div class="layui-table-cell laytable-cell-' + function () { //返回对应的CSS类标识
return item3.type === 'normal' ? key
: (key + ' laytable-cell-' + item3.type);
}() + '">' + function () {
var tplData = $.extend(true, {
LAY_INDEX: numbers
}, item1)
//解析工具列模板
if (item3.toolbar) {
return laytpl($(item3.toolbar).html() || '').render(tplData);
}
return item3.templet ? function () {
return typeof item3.templet === 'function'
? item3.templet(tplData)
: laytpl($(item3.templet).html() || String(content)).render(tplData)
}() : content;
}()
, '</div></li>'].join('');
}
divs.push(info);
} else if (options.mode == "cardListMode") {
//渲染不同风格的列
var tplData = $.extend(true, {
LAY_INDEX: numbers
}, item1)
, checkName = table.config.checkName;
var td = [];
var infotd = []
switch (item3.type) {
case 'checkbox':
// 分页勾选逻辑 start
var itemValue = item1[that.config.onlyKey]; // 获取唯一值用于判断默认rwid
var ischecked = that.checkedDefault(itemValue, i1);
// 分页勾选逻辑 end
td = ['<div class="item-sub item-checkbox" data-field="' + field + '"><input type="checkbox" ' + ischecked + ' name="layTableCheckbox" lay-skin="primary" ' + function () {
//如果是全选
if (item3[checkName]) {
item1[checkName] = item3[checkName];
return item3[checkName] ? 'checked' : '';
}
return tplData[checkName] ? 'checked' : '';
}() + '></div>'].join('');
break;
case 'radio':
if (tplData[checkName]) {
thisCheckedRowIndex = i1;
}
td = ['<div class="item-sub item-checkbox" data-field="' + field + '"><input type="radio" name="layTableRadio_' + options.index + '" '
+ (tplData[checkName] ? 'checked' : '') + ' lay-type="layTableRadio"></div>'].join('');
break;
case 'image':
td = ['<div data-field="' + field + '" data-key="' + key + '" ' + function () { //追加各种属性
var attr = [];
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.templet) attr.push('data-content="' + content + '"'); //自定义模板
if (item3.event) attr.push('lay-event="' + item3.event + '"'); //自定义事件
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
return attr.join(' ');
}() + ' class="imgbox item-sub"><a href="javascript:;">' + function () {
var tplData = $.extend(true, {
LAY_INDEX: numbers
}, item1);
return item3.templet ? function () {
return typeof item3.templet === 'function'
? item3.templet(tplData)
: laytpl($(item3.templet).html() || String(content)).render(tplData)
}() : content;
}()
, '</a></div>'].join('');
break;
default:
infotd = ['<div data-field="' + field + '" data-key="' + key + '" ' + function () { //追加各种属性
var attr = [];
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.templet) attr.push('data-content="' + content + '"'); //自定义模板
if (item3.event) attr.push('lay-event="' + item3.event + '"'); //自定义事件
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
return attr.join(' ');
}() + ' class="layui-table-cell ' + function () { //追加样式
var classNames = [];
if (item3.hide) classNames.push(HIDE); //插入隐藏列样式
if (!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
return classNames.join(' ');
}() + '">' + function () {
var tplData = $.extend(true, {
LAY_INDEX: numbers
}, item1);
//解析工具列模板
if (item3.toolbar) {
return laytpl($(item3.toolbar).html() || '').render(tplData);
}
return item3.templet ? function () {
return typeof item3.templet === 'function'
? item3.templet(tplData)
: laytpl($(item3.templet).html() || String(content)).render(tplData)
}() : (title + ':' + content);
}()
, '</div>'].join('');
};
if (infotd.length != 0) {
infotds.push(infotd)
}
} else {
//td内容
var td = ['<td data-field="' + field + '" data-key="' + key + '" ' + function () { //追加各种属性
var attr = [];
if (item3.edit) attr.push('data-edit="' + item3.edit + '"'); //是否允许单元格编辑
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.templet) attr.push('data-content="' + content + '"'); //自定义模板
if (item3.toolbar) attr.push('data-off="true"'); //行工具列关闭单元格事件
if (item3.event) attr.push('lay-event="' + item3.event + '"'); //自定义事件
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
if (item3.minWidth) attr.push('data-minwidth="' + item3.minWidth + '"'); //单元格最小宽度
if (item3.currency) attr.push('data-currency="' + item3.currency + '"'); //add 是否对数据做千分号格式化处理
if (item3.edit == 'select') attr.push('data-selectvalue="' + content + '"');
return attr.join(' ');
}() + ' class="' + function () { //追加样式
var classNames = [];
if (item3.hide) classNames.push(HIDE); //插入隐藏列样式
if (!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
return classNames.join(' ');
}() + '">'
, '<div class="layui-table-cell laytable-cell-' + function () { //返回对应的CSS类标识
return item3.type === 'normal' ? key
: (key + ' laytable-cell-' + item3.type);
}() + '">' + function () {
var tplData = $.extend(true, {}, {
LAY_INDEX: numbers
}, item1)
, checkName = table.config.checkName;
//渲染不同风格的列
switch (item3.type) {
case 'checkbox':
// 分页勾选逻辑 start
var itemValue = item1[that.config.onlyKey]; // 获取唯一值用于判断默认rwid
var ischecked = that.checkedDefault(itemValue, i1);
// 分页勾选逻辑 end
return '<input type="checkbox" ' + ischecked + ' name="layTableCheckbox" lay-skin="primary" ' + function () {
//如果是全选
if (item3[checkName]) {
item1[checkName] = item3[checkName];
return item3[checkName] ? 'checked' : '';
}
return tplData[checkName] ? 'checked' : '';
}() + '>';
break;
case 'radio':
if (tplData[checkName]) {
thisCheckedRowIndex = i1;
}
return '<input type="radio" name="layTableRadio_' + options.index + '" '
+ (tplData[checkName] ? 'checked' : '') + ' lay-type="layTableRadio">';
break;
case 'numbers':
return numbers;
break;
case 'currency':
if (!item3.templet) {
if (item3.tofix) {
return table.formatNumberByPermil(parseFloat(item1[item3.field]).toFixed(parseInt(item3.tofix)));
}
else {
return table.formatNumberByPermil(item1[item3.field]);
}
}
break;
};
//解析工具列模板
if (item3.toolbar) {
return laytpl($(item3.toolbar).html() || '').render(tplData);
}
if (item3.edit == 'select' && item3.list) {
var opts = item3.list();
for (let i = 0; i < opts.length; i++) {
const opt = opts[i];
if (opt.key == content) {
return opt.name
}
}
}
return item3.templet ? function () {
return typeof item3.templet === 'function'
? item3.templet(tplData)
: laytpl($(item3.templet).html() || String(content)).render(tplData)
}() : (item3.tofix ? parseFloat(content).toFixed(parseInt(item3.tofix)) : content);
}()
, '</div></td>'].join('');
}
tds.push(td);
if (options.mode != "cardListMode") {
if (item3.fixed && item3.fixed !== 'right') tds_fixed.push(td);
if (item3.fixed === 'right') tds_fixed_r.push(td);
}
});
// 三种列表模式拼接
if (options.mode == "imgListMode") {
// 创建一个标题头部的数据,遍历一下赋值,只循环一次
var setsing_obj = options.setsing;
var table_nfo_cell = '';
if (setsing_obj) {
var ul_list = [];
for (var j = 0, len = setsing_obj.length; j < len; j++) {
if (item1) {
ul_list.push('<li><span>' + setsing_obj[j].title + ':</span>' + item1[setsing_obj[j].field] + '</li>');
}
}
table_nfo_cell = '<div class="layui-table-cell table-info-cell"><ul class="ul-list">' + ul_list.join('') + '</ul></div>';
}
trs.push('<tr class="layu-trhead" data-index="' + i1 + '">' + tds.join('') + '<td colspan="' + (options.cols[0].length - tds.length) + '">' + table_nfo_cell + '<div vlass="layui-table-cell table-images-cell"><ul class="ul-info-list">' + divs.join('') + '</ul></div></td></tr>');
// trs_fixed.push('<tr class="layu-trhead-fixed" data-index="' + i1 + '">' + tds_fixed.join('') + '</tr>');
// trs_fixed_r.push('<tr class="layu-trhead-fixed" data-index="' + i1 + '">' + tds_fixed_r.join('') + '</tr>');
} else if (options.mode == "cardListMode") {
if (options.vertical) {
trs.push('<div class=" card-item vertical-item" data-index="' + i1 + '"><div class="tp">' + tds.join('') + '</div><div class="text">' + infotds.join('') + '</div></div>');
} else {
trs.push('<div class=" card-item" data-index="' + i1 + '"><div class=" layui-row grid-demo item">' + tds.join('') + '<div class="text">' + infotds.join('') + '</div></div></div>');
}
} else {
trs.push('<tr data-index="' + i1 + '">' + tds.join('') + '</tr>');
trs_fixed.push('<tr data-index="' + i1 + '">' + tds_fixed.join('') + '</tr>');
trs_fixed_r.push('<tr data-index="' + i1 + '">' + tds_fixed_r.join('') + '</tr>');
}
});
that.layBody.scrollTop(0);
that.layMain.find('.' + NONE).remove();
if (options.mode == "cardListMode") {
var layConent = '.layui-table';
} else {
var layConent = 'tbody';
}
that.layMain.find(layConent).html(trs.join(''));
if (options.mode == "listMode") {
that.layFixLeft.find('tbody').html(trs_fixed.join(''));
that.layFixRight.find('tbody').html(trs_fixed_r.join(''));
}
if ($(".imgbox a").length > 0) {
var imgboxH = $(".imgbox a").height();
if (imgboxH) {
$(".imgbox a").css("line-height", imgboxH + 'px');
}
}
// 表格里面有下拉框的添加样式
if ($(".layui-table-cell").find('select').length > 0) {
$(".layui-table-cell").find('select').parent(".layui-table-cell").css('overflow', 'visible');
$(".layui-table-cell").find('select').parent(".layui-table-cell").addClass('layui-table-cell-active')
}
that.renderForm();
typeof thisCheckedRowIndex === 'number' && that.setThisRowChecked(thisCheckedRowIndex);
that.syncCheckAll();
that.scrollPatch();
/*
that.haveInit ? that.scrollPatch() : setTimeout(function(){
that.scrollPatch();
}, 50);
that.haveInit = true;
*/
layer.close(that.tipsIndex);
that.loadingnews(true);
//同步表头父列的相关值
options.HAS_SET_COLS_PATCH || that.setColsPatch();
options.HAS_SET_COLS_PATCH = true;
};
that.key = options.id || options.index;
table.cache[that.key] = data; //记录数据
//设置当前选中数据参数开启翻页勾选状态 flipCheck
if (that.config.flipCheck && table.checkedMultiList[that.key] == null) {
table.checkedMultiList[that.key] = [];
}
//显示隐藏分页栏
that.layPage[(count == 0 || (data.length === 0 && curr == 1)) ? 'addClass' : 'removeClass'](HIDE);
//排序
if (sort) {
return render();
}
if (data.length === 0) {
that.renderForm();
that.layFixed.remove();
that.layMain.find('tbody').html('');
that.layMain.find('.' + NONE).remove();
// return that.layMain.append('<div class="'+ NONE +'"><div class="empty-data">'+ options.text.none +'</div></div>');
var imgtop = 0, imgsize = 0, fontszie = 0, fonttop = 0;
if (that.layMain.height() > 150) {
imgtop = '40px'; imgsize = '80px'; fontszie = '22px'; fonttop = '10px'
} else {
imgtop = '10px'; imgsize = '40px'; fontszie = '14px'; fonttop = '5px'
}
return that.layMain.append('<div class="' + NONE + '" style="padding-bottom: 0;">' +
' <div class="empty-data" style="text-align:center;padding-top: ' + imgtop + ';font-size: 25px;color: #ccc;font-family: tahoma ,微软雅黑;">' +
' <i class="layui-icon layui-icon-search" style="font-size: ' + imgsize + ';"></i> ' +
' <div style="padding-top: ' + fonttop + ';font-size: ' + fontszie + ';">' + options.text.none + '</div>' +
' </div>' +
'</div>');
}
// <div style="text-align:center;padding-top: 10%;font-size: 25px;color: #aab4bd;font-family: tahoma ,微软雅黑;">
// <img src="/JE/resource/css/img/nodata.png" width="130">
// <div>没有数据</div>
// </div>
render(); //渲染数据
that.renderTotal(data); //数据合计
//同步分页状态
if (options.page) {
options.page = $.extend({
elem: 'layui-table-page' + options.index
, count: count
, limit: options.limit
, limits: options.limits || [20, 50, 100, 200, 500, 1000]
, groups: 3
, layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
, prev: '<i class="layui-icon">&#xe603;</i>'
, next: '<i class="layui-icon">&#xe602;</i>'
, jump: function (obj, first) {
if (!first) {
//分页本身并非需要做以下更新,下面参数的同步,主要是因为其它处理统一用到了它们
//而并非用的是 options.page 中的参数(以确保分页未开启的情况仍能正常使用)
//that.page = obj.curr; //更新页码
options.limit = obj.limit; //更新每页条数
that.loadingnews();
if (options.dataSource) {
var ds = options.dataSource;
ds.reload({ params: { page: obj.curr, limit: obj.limit } });
} else {
that.pullData(obj.curr);
}
}
}
}, options.page);
options.page.count = count; //更新总条数
laypage.render(options.page);
}
};
//数据合计行
Class.prototype.renderTotal = function (data) {
var that = this
, options = that.config
, totalNums = {};
if (!options.totalRow) return;
layui.each(data, function (i1, item1) {
if (item1.length === 0) return;
if (options.mode == "cardListMode") {
that.eachTotalCols(function (i3, item3) {
var field = item3.field || i3
, content = item1[field];
if (item3.totalRow) {
totalNums[field] = that.add((totalNums[field] || 0), (parseFloat(content) || 0));
}
});
} else {
that.eachCols(function (i3, item3) {
var field = item3.field || i3
, content = item1[field];
if (item3.totalRow) {
totalNums[field] = that.add((totalNums[field] || 0), (parseFloat(content) || 0));
}
});
}
});
var tds = [];
if (options.mode == "cardListMode") {
that.eachTotalCols(function (i3, item3) {
var field = item3.field || i3
if (item3.hide) return;
//td内容
var td = ['<div data-field="' + field + '" ' + function () {
var attr = [];
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
return attr.join(' ');
}() + ' class="layui-table-cell total-box">' + function () {
var text = item3.totalRowText || ''
, unit = item3.unit || ''
, total = parseFloat(item3.total) || 0
, classname = item3.classname || 'total' + i3
, decimal = item3.decimal;
if (item3.totalRow && !item3.checkstate) {
return item3.totalRow ? (text + '<span class="' + classname + '">' + (decimal ? totalNums[field].toFixed(2) : totalNums[field]) + '</span>' + unit) : text;
} else {
return text + '<span class="' + classname + '">' + (decimal ? total.toFixed(2) : total) + '</span>' + unit;
}
}()
, '</div>'].join('');
tds.push(td);
});
that.layTotal.find('.layui-table-total-box').html(tds.join(''));
} else {
that.eachCols(function (i3, item3) {
var field = item3.field || i3;
if (item3.hide) return;
if (item3.tofix) {
totalNums[field] = parseFloat(totalNums[field]).toFixed(parseInt(item3.tofix));
}
//td内容
var td = ['<td data-field="' + field + '" data-key="' + options.index + '-' + item3.key + '" ' + function () {
var attr = [];
if (item3.align) attr.push('align="' + item3.align + '"'); //对齐方式
if (item3.style) attr.push('style="' + item3.style + '"'); //自定义样式
if (item3.minWidth) attr.push('data-minwidth="' + item3.minWidth + '"'); //单元格最小宽度
return attr.join(' ');
}() + '>'
, '<div class="layui-table-cell laytable-cell-' + function () { //返回对应的CSS类标识
var str = (options.index + '-' + item3.key);
return item3.type === 'normal' ? str
: (str + ' laytable-cell-' + item3.type);
}() + '">' + function () {
var text = item3.totalRowText || '';
return item3.totalRow ? (item3.type === 'currency' ? table.formatNumberByPermil((totalNums[field] || text)) : (totalNums[field] || text)) : text;
}()
, '</div></td>'].join('');
tds.push(td);
});
that.layTotal.find('tbody').html('<tr>' + tds.join('') + '</tr>');
}
};
//选中数据合计行
Class.prototype.renderCheckTotal = function (data) {
var that = this
, options = that.config
, totalNums = {};
if (!options.totalRow) return;
layui.each(data, function (i1, item1) {
if (item1.length === 0) return;
that.eachTotalCols(function (i3, item3) {
var field = item3.field || i3
, content = item1[field];
if (item3.totalRow) {
totalNums[field] = that.add((totalNums[field] || 0), (parseFloat(content) || 0));
}
});
});
that.eachTotalCols(function (i3, item3) {
var field = item3.field || i3
if (item3.hide) return;
var text = item3.totalRowText || ''
, unit = item3.unit || ''
, classname = item3.classname || 'total' + i3
, decimal = item3.decimal
, checkNums = totalNums[field] ? totalNums[field] : 0.00;
if (item3.checkstate) {
if (item3.field) {
that.layTotal.find('.layui-table-total-box .' + classname).text(decimal ? checkNums.toFixed(2) : checkNums);
} else {
that.layTotal.find('.layui-table-total-box .' + classname).text(data.length);
}
}
});
};
Class.prototype.add = function (arg1, arg2) {
var r1, r2, m, c;
try {
r1 = arg1.toString().split(".")[1].length;
}
catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
}
catch (e) {
r2 = 0;
}
c = Math.abs(r1 - r2);
m = Math.pow(10, Math.max(r1, r2));
if (c > 0) {
var cm = Math.pow(10, c);
if (r1 > r2) {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", "")) * cm;
} else {
arg1 = Number(arg1.toString().replace(".", "")) * cm;
arg2 = Number(arg2.toString().replace(".", ""));
}
} else {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", ""));
}
return (arg1 + arg2) / m;
};
//找到对应的列元素
Class.prototype.getColElem = function (parent, key) {
var that = this
, options = that.config;
return parent.eq(0).find('.laytable-cell-' + (options.index + '-' + key) + ':eq(0)');
};
//渲染表单
Class.prototype.renderForm = function (type) {
form.render(type, 'LAY-table-' + this.index);
};
//标记当前行选中状态
Class.prototype.setThisRowChecked = function (index) {
var that = this
, options = that.config
, ELEM_CLICK = 'layui-table-click'
, ELEM_OBJ = (options.mode == 'cardListMode' ? '.card-item' : 'tr')
, tr = that.layBody.find(ELEM_OBJ + '[data-index="' + index + '"]');
tr.addClass(ELEM_CLICK).siblings('tr').removeClass(ELEM_CLICK);
};
//数据排序
Class.prototype.sort = function (th, type, pull, formEvent) {
var that = this
, field
, res = {}
, options = that.config
, elemobj = (options.mode == 'cardListMode' ? 'li' : 'th')
, filter = options.elem.attr('lay-filter')
, data = table.cache[that.key], thisData;
//字段匹配
if (typeof th === 'string') {
that.layHeader.find(elemobj).each(function (i, item) {
var othis = $(this)
, _field = othis.data('field');
if (_field === th) {
th = othis;
field = _field;
return false;
}
});
}
try {
var field = field || th.data('field')
, key = th.data('key');
//如果欲执行的排序已在状态中,则不执行渲染
if (that.sortKey && !pull) {
if (field === that.sortKey.field && type === that.sortKey.sort) {
return;
}
}
var elemSort = that.layHeader.find(elemobj + ' .laytable-cell-' + key).find(ELEM_SORT);
if (!options.dataSource) {
that.layHeader.find(elemobj).find(ELEM_SORT).removeAttr('lay-sort'); //清除其它标题排序状态
}
elemSort.attr('lay-sort', type || null);
if (options.mode != "cardListMode") {
that.layFixed.find('th')
}
} catch (e) {
return hint.error('Table modules: Did not match to field');
}
//记录排序索引和类型
that.sortKey = {
field: field
, sort: type
};
if (!options.sortload) {
if (type === 'asc') { //升序
thisData = layui.sort(data, field);
} else if (type === 'desc') { //降序
thisData = layui.sort(data, field, true);
} else { //清除排序
thisData = layui.sort(data, table.config.indexName);
delete that.sortKey;
}
}
res[options.response.dataName] = thisData || data;
that.renderData(res, that.page, that.count, true);
// that.pullData();
if (formEvent) {
layui.event.call(th, MOD_NAME, 'sort(' + filter + ')', {
field: field
, type: type
});
}
};
//请求loading
Class.prototype.loading = function (hide) {
var that = this
, options = that.config;
if (options.loading) {
if (hide) {
that.layInit && that.layInit.remove();
delete that.layInit;
that.layBox.find(ELEM_INIT).remove()
} else {
that.layInit = $(['<div class="layui-table-init">'
, '<i class="layui-icon layui-icon-loading layui-icon"></i>'
, '</div>'].join(''));
that.layBox.append(that.layInit);
}
}
};
Class.prototype.loadingnews = function (hide) {
var that = this,
index = layer.msg('数据查询中,请稍候...', { icon: 16, time: false, shade: 0.1 });
if (hide) {
layer.close(index);
}
}
//同步选中值状态
Class.prototype.setCheckData = function (index, checked) {
var that = this
, options = that.config
, thisData = table.cache[that.key];
if (!thisData[index]) return;
if (thisData[index].constructor === Array) return;
thisData[index][options.checkName] = checked;
};
//同步全选按钮状态
Class.prototype.syncCheckAll = function () {
var that = this
, options = that.config
, checkAllElem = that.layHeader.find('input[name="layTableCheckbox"]')
, syncColsCheck = function (checked) {
that.eachCols(function (i, item) {
if (item.type === 'checkbox') {
item[options.checkName] = checked;
}
});
return checked;
};
if (!checkAllElem[0]) return;
if (table.checkStatus(that.key).isAll) {
if (!checkAllElem[0].checked) {
checkAllElem.prop('checked', true);
that.renderForm('checkbox');
}
syncColsCheck(true);
} else {
if (checkAllElem[0].checked) {
checkAllElem.prop('checked', false);
that.renderForm('checkbox');
}
syncColsCheck(false);
}
};
//获取cssRule
Class.prototype.getCssRule = function (key, callback) {
var that = this
, style = that.elem.find('style')[0]
, sheet = style.sheet || style.styleSheet || {}
, rules = sheet.cssRules || sheet.rules;
layui.each(rules, function (i, item) {
if (item.selectorText === ('.laytable-cell-' + key)) {
return callback(item), true;
}
});
};
//让表格铺满
Class.prototype.fullSize = function () {
var that = this
, options = that.config
, height = options.height, bodyHeight;
if (that.fullHeightGap) {
height = _WIN.height() - that.fullHeightGap;
if (height < 135) height = 135;
that.elem.css('height', height);
}
if (!height) return;
//减去列头区域的高度
bodyHeight = parseFloat(height) - (that.layHeader.outerHeight() || 38); //此处的数字常量是为了防止容器处在隐藏区域无法获得高度的问题,暂时只对默认尺寸的表格做支持。
//减去工具栏的高度
if (options.toolbar) {
bodyHeight = bodyHeight - (that.layTool.outerHeight() || 50);
}
//减去统计朗的高度
if (options.totalRow) {
bodyHeight = bodyHeight - (that.layTotal.outerHeight() || 40);
}
//减去分页栏的高度
if (options.page) {
bodyHeight = bodyHeight - (that.layPage.outerHeight() || 41) - 2;
}
that.layMain.css('height', bodyHeight);
};
//获取滚动条宽度
Class.prototype.getScrollWidth = function (elem) {
var width = 0;
if (elem) {
width = elem.offsetWidth - elem.clientWidth;
} else {
elem = document.createElement('div');
elem.style.width = '100px';
elem.style.height = '100px';
elem.style.overflowY = 'scroll';
document.body.appendChild(elem);
width = elem.offsetWidth - elem.clientWidth;
document.body.removeChild(elem);
}
return width;
};
//滚动条补丁
Class.prototype.scrollPatch = function () {
var that = this
, options = that.config
, elemobj = (options.mode == 'cardListMode' ? '.layui-table' : 'table')
, layMainTable = that.layMain.children(elemobj)
, scollWidth = that.layMain.width() - that.layMain.prop('clientWidth') //纵向滚动条宽度
, scollHeight = that.layMain.height() - that.layMain.prop('clientHeight') //横向滚动条高度
, getScrollWidth = that.getScrollWidth(that.layMain[0]) //获取主容器滚动条宽度,如果有的话
, outWidth = layMainTable.outerWidth() - that.layMain.width() //表格内容器的超出宽度
//添加补丁
, addPatch = function (elem) {
if (scollWidth && scollHeight) {
elem = elem.eq(0);
if (!elem.find('.layui-table-patch')[0]) {
var patchElem = $('<th class="layui-table-patch"><div class="layui-table-cell"></div></th>'); //补丁元素
patchElem.find('div').css({
width: scollWidth
});
elem.find('tr').append(patchElem);
}
} else {
elem.find('.layui-table-patch').remove();
}
}
addPatch(that.layHeader);
addPatch(that.layTotal);
//固定列区域高度
var mainHeight = that.layMain.height()
, fixHeight = mainHeight - scollHeight;
that.layFixed.find(ELEM_BODY).css('height', layMainTable.height() > fixHeight ? fixHeight : 'auto');
//表格宽度小于容器宽度时,隐藏固定列
that.layFixRight[outWidth > 0 ? 'removeClass' : 'addClass'](HIDE);
//操作栏
that.layFixRight.css('right', scollWidth - 1);
};
//设置页面默认选中在表格渲染input的时候调用
Class.prototype.checkedDefault = function (itemValue, index) {
var that = this
, options = that.config
, ischecked = "";
if (options.flipCheck) {
var getOnlyKey = that.config.onlyKey;
if (table.checkedMultiList[that.key] != null && table.checkedMultiList[that.key].length > 0) {
var objFilter = table.checkedMultiList[that.key].filter(function (filterItem, filterIndex) {
return filterItem[getOnlyKey] == itemValue;
});
if (objFilter != null && objFilter.length > 0) {
//设置选中状态
ischecked = "checked";
that.setCheckData(index, true)
}
}
that.syncCheckAll();
}
return ischecked
}
//事件处理
Class.prototype.events = function () {
var that = this
, options = that.config
, _BODY = $('body')
, elemobj = (options.mode == 'cardListMode' ? 'li' : 'th')
, ELEM_OBJ = (options.mode == 'cardListMode' ? '.card-item' : 'tr')
, dict = {}
, th = that.layHeader.find(elemobj)
, resizing
, ELEM_CELL = '.layui-table-cell'
, filter = options.elem.attr('lay-filter');
//工具栏操作事件
that.layTool.on('click', '*[lay-event]', function (e) {
var othis = $(this)
, events = othis.attr('lay-event')
, openPanel = function (sets) {
var list = $(sets.list)
, panel = $('<ul class="layui-table-tool-panel"></ul>');
panel.html(list);
othis.find('.layui-table-tool-panel')[0] || othis.append(panel);
that.renderForm();
panel.on('click', function (e) {
layui.stope(e);
});
sets.done && sets.done(panel, list)
};
layui.stope(e);
_DOC.trigger('table.tool.panel.remove');
layer.close(that.tipsIndex);
switch (events) {
case 'LAYTABLE_COLS': //筛选列
openPanel({
list: function () {
var lis = [];
that.eachCols(function (i, item) {
if (item.field && item.type == 'normal') {
lis.push('<li><input type="checkbox" name="' + item.field + '" data-key="' + item.key + '" data-parentkey="' + (item.parentKey || '') + '" lay-skin="primary" ' + (item.hide ? '' : 'checked') + ' title="' + (item.title || item.field) + '" lay-filter="LAY_TABLE_TOOL_COLS"></li>');
}
});
return lis.join('');
}()
, done: function () {
form.on('checkbox(LAY_TABLE_TOOL_COLS)', function (obj) {
var othis = $(obj.elem)
, checked = this.checked
, key = othis.data('key')
, parentKey = othis.data('parentkey');
//保存用户私有数据
var upd = [];
var sortStr = { field: othis.attr('name'), title: othis.attr('title'), hide: !checked, key: key };
layui.each(options.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
if (i1 + '-' + i2 === key) {
var hide = item2.hide;
//同步勾选列的 hide 值和隐藏样式
item2.hide = !checked;
that.elem.find('*[data-key="' + options.index + '-' + key + '"]')
[checked ? 'removeClass' : 'addClass'](HIDE);
//根据列的显示隐藏,同步多级表头的父级相关属性值
if (hide != item2.hide) {
that.setParentCol(!checked, parentKey);
}
//适配
that.fullSize();
that.scrollPatch();
that.setColsWidth();
}
if (item2.field && item2.type == 'normal') {
upd.push({ 'field': item2.field, 'hide': item2.hide });
}
});
});
that.saveUserPdata(upd);
// 保存显示隐藏的状态
if (options.sortcols[0].length == 1 && options.mode == 'cardListMode') {
var sortcolsArr = that.config.sortcols[0];
for (var i = 0; i < sortcolsArr.length; i++) {
if (sortcolsArr[i].field == sortStr.field) {
sortcolsArr[i] = sortStr;
}
}
var sortcolsArry = [];
sortcolsArry.push(sortcolsArr);
that.config.sortcols = sortcolsArry;
}
});
}
});
break;
case 'LAYTABLE_EXPORT': //导出
if (device.ie) {
layer.tips('导出功能不支持 IE请用 Chrome 等高级浏览器导出', this, {
tips: 3
})
} else {
openPanel({
list: function () {
return [
'<li data-type="csv">导出到 Csv 文件</li>'
, '<li data-type="xls">导出到 Excel 文件</li>'
].join('')
}()
, done: function (panel, list) {
list.on('click', function () {
var type = $(this).data('type')
table.exportFile(options.id, null, type);
});
}
});
}
break;
case 'LAYTABLE_PRINT': //打印
var printWin = window.open('打印窗口', '_blank')
, style = ['<style>'
, 'body{font-size: 12px; color: #666;}'
, 'table{width: 100%; border-collapse: collapse; border-spacing: 0;}'
, 'th,td{line-height: 20px; padding: 9px 15px; border: 1px solid #ccc; text-align: left; font-size: 12px; color: #666;}'
, 'a{color: #666; text-decoration:none;}'
, '*.layui-hide{display: none}'
, '</style>'].join('')
, html = $(that.layHeader.html());
html.append(that.layMain.find('table').html());
html.find('th.layui-table-patch').remove();
html.find('.layui-table-col-special').remove();
printWin.document.write(style + html.prop('outerHTML'));
printWin.document.close();
printWin.print();
printWin.close();
break;
}
layui.event.call(this, MOD_NAME, 'toolbar(' + filter + ')', $.extend({
event: events
, config: options
}, {}));
});
//拖拽调整宽度
th.on('mousemove', function (e) {
var othis = $(this)
, oLeft = othis.offset().left
, pLeft = e.clientX - oLeft;
if (othis.data('unresize') || dict.resizeStart) {
return;
}
dict.allowResize = othis.width() - pLeft <= 10; //是否处于拖拽允许区域
_BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
}).on('mouseleave', function () {
var othis = $(this);
if (dict.resizeStart) return;
_BODY.css('cursor', '');
}).on('mousedown', function (e) {
var othis = $(this);
if (dict.allowResize) {
var key = othis.data('key');
e.preventDefault();
dict.resizeStart = true; //开始拖拽
dict.offset = [e.clientX, e.clientY]; //记录初始坐标
that.getCssRule(key, function (item) {
var width = item.style.width || othis.outerWidth();
dict.rule = item;
dict.ruleWidth = parseFloat(width);
dict.minWidth = othis.data('minwidth') || options.cellMinWidth;
});
}
}).on('mouseup', function (e) {
var othis = $(this);
var field = othis.data('field');
var key = othis.data('key');
e.preventDefault();
that.getCssRule(key, function (item) {
var width = item.style.width || othis.outerWidth();
layui.each(that.config.cols, function (key, value) {
layui.each(value, function (index, item) {
if (item.field && item.field == field) {
item.width = parseFloat(width)
}
})
})
});
});
//拖拽中
_DOC.on('mousemove', function (e) {
if (dict.resizeStart) {
e.preventDefault();
if (dict.rule) {
var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
if (setWidth < dict.minWidth) setWidth = dict.minWidth;
dict.rule.style.width = setWidth + 'px';
layer.close(that.tipsIndex);
}
resizing = 1
}
}).on('mouseup', function (e) {
if (dict.resizeStart) {
dict = {};
_BODY.css('cursor', '');
that.scrollPatch();
}
if (resizing === 2) {
resizing = null;
}
});
//排序
th.on('click', function (e) {
var othis = $(this)
, elemSort = othis.find(ELEM_SORT)
, nowType = elemSort.attr('lay-sort')
, _field = othis.data('field')
, type;
if (!elemSort[0] || resizing === 1) return resizing = 2;
if (nowType === 'asc') {
type = 'desc';
} else if (nowType === 'desc') {
type = null;
} else {
type = 'asc';
}
// 不请求数据
if (!that.config.sortload) {
that.sort(othis, type, null, true);
} else {
that.loadingnews();
if (options.dataSource) {
// 数据源排序
var ds = options.dataSource;
// sort数据源请求的参数
var sort = {};
// 设置默认的排序字段,组装已有的数据
that.config.initSort = that.config.initSort || {};
var sortField = that.config.initSort;
for (var key in sortField) {
sort[key] = sortField[key];
}
sort[_field] = (type != null ? type : '');
// 记录当前排序的字段
that.config.initSort[_field] = type
ds.reload({ sort: sort });
} else {
table.reload(that.config.id, {
initSort: {
field: _field
, type: type
},
where: { FIELD: _field, ORDER: type }
});
}
}
}).find(ELEM_SORT + ' .layui-edge ').on('click', function (e) {
var othis = $(this)
, index = othis.index()
, field = othis.parents('th').eq(0).data('field')
, type;
layui.stope(e);
if (index === 0) {
type = 'asc'
} else {
type = 'desc'
}
if (!that.config.sortload) {
that.sort(field, type, null, true);
} else {
that.loadingnews();
if (options.dataSource) {
// 数据源排序
var ds = options.dataSource;
// sort数据源请求的参数
var sort = {};
// 设置默认的排序字段
that.config.initSort = that.config.initSort || {};
var sortField = that.config.initSort;
for (var key in sortField) {
sort[key] = sortField[key];
}
sort[field] = type;
// 记录当前排序的字段
that.config.initSort[field] = type
ds.reload({ sort: sort });
} else {
table.reload(that.config.id, {
initSort: {
field: field
, type: type
},
where: { FIELD: field, ORDER: type }
});
}
}
});
//数据行中的事件监听返回的公共对象成员
var commonMember = function (sets) {
var othis = $(this)
, ELEM_CHILD = (options.mode == 'cardListMode' ? 'dic' : 'td')
, index = othis.parents(ELEM_OBJ).eq(0).data('index')
, tr = that.layBody.find(ELEM_OBJ + '[data-index="' + index + '"]')
, data = table.cache[that.key][index];
return $.extend({
tr: tr //行元素
, data: table.clearCacheKey(data) //当前行数据
, del: function () { //删除行数据
table.cache[that.key][index] = [];
tr.remove();
that.scrollPatch();
}
, update: function (fields) { //修改行数据
fields = fields || {};
layui.each(fields, function (key, value) {
if (key in data) {
var templet, td = tr.children(ELEM_CHILD + '[data-field="' + key + '"]');
data[key] = value;
that.eachCols(function (i, item2) {
if (item2.field == key && item2.templet) {
templet = item2.templet;
}
});
td.children(ELEM_CELL).html(function () {
return templet ? function () {
return typeof templet === 'function'
? templet(data)
: laytpl($(templet).html() || value).render(data)
}() : value;
}());
td.data('content', value);
}
});
}
}, sets);
};
//复选框选择
that.elem.on('click', 'input[name="layTableCheckbox"]+', function () { //替代元素的 click 事件
var checkbox = $(this).prev()
, childs = that.layBody.find('input[name="layTableCheckbox"]')
, index = checkbox.parents(ELEM_OBJ).eq(0).data('index')
, checked = checkbox[0].checked
, isAll = checkbox.attr('lay-filter') === 'layTableAllChoose';
//全选
if (isAll) {
childs.each(function (i, item) {
item.checked = checked;
that.setCheckData(i, checked);
});
that.syncCheckAll();
that.renderForm('checkbox');
} else {
that.setCheckData(index, checked);
that.syncCheckAll();
}
if (that.config.flipCheck) {
// 分页记录选择逻辑 start
//当前选中数据
var checkRowData = [];
//当前取消选中的数据
var cacelCheckedRowData = [];
var getOnlyKey = that.config.onlyKey
//选中
if (checked) {
checkRowData = table.checkStatus(that.key).data;
}
//取消选中
else {
if (isAll) {
//当前页数据
var currentPageData = table.cache[that.key];
cacelCheckedRowData = currentPageData;
}
else {
var thisData = table.cache[that.key];
if (!thisData[index]) return;
if (thisData[index].constructor === Array) return;
cacelCheckedRowData.push(thisData[index]);
}
}
//debugger;
//清除数据
$.each(cacelCheckedRowData, function (index, item) {
var itemValue = item[that.config.onlyKey];
//debugger;
table.checkedMultiList[that.key] = table.checkedMultiList[that.key].filter(function (fItem, fIndex) {
return fItem[getOnlyKey] != itemValue;
})
});
//添加选中数据
$.each(checkRowData, function (index, item) {
//debugger;
var itemValue = item[that.config.onlyKey];
var objFilter = table.checkedMultiList[that.key].filter(function (fItem, fIndex) {
return fItem[getOnlyKey] == itemValue;
});
if (objFilter == null || objFilter.length == 0) {
var rowObj = {};
rowObj[getOnlyKey] = itemValue;
if (that.config.attachFieldsName.length > 0) {
$.each(that.config.attachFieldsName, function (objIndex, objItem) {
rowObj[objItem] = item[objItem];
});
} else {
rowObj = item;
}
table.checkedMultiList[that.key].push(rowObj);
}
});
}
// 分页记录选择逻辑 end
layui.event.call(checkbox[0], MOD_NAME, 'checkbox(' + filter + ')', commonMember.call(checkbox[0], {
checked: checked
, type: isAll ? 'all' : 'one'
}));
that.renderCheckTotal(table.checkStatus(that.key).data);
});
//单选框选择
that.elem.on('click', 'input[lay-type="layTableRadio"]+', function () {
var radio = $(this).prev()
, checked = radio[0].checked
, thisData = table.cache[that.key]
, index = radio.parents('tr').eq(0).data('index')
//重置数据单选属性
layui.each(thisData, function (i, item) {
if (index === i) {
item.LAY_CHECKED = true;
} else {
delete item.LAY_CHECKED;
}
});
that.setThisRowChecked(index);
layui.event.call(this, MOD_NAME, 'radio(' + filter + ')', commonMember.call(this, {
checked: checked
}));
});
//行事件
that.layBody.on('mouseenter', ELEM_OBJ, function () { //鼠标移入行
var othis = $(this)
, index = othis.index();
that.layBody.find(ELEM_OBJ + ':eq(' + index + ')').addClass(ELEM_HOVER)
}).on('mouseleave', ELEM_OBJ, function () { //鼠标移出行
var othis = $(this)
, index = othis.index();
that.layBody.find(ELEM_OBJ + ':eq(' + index + ')').removeClass(ELEM_HOVER)
}).on('click', ELEM_OBJ, function () { //单击行
setRowEvent.call(this, 'row');
}).on('dblclick', ELEM_OBJ, function () { //双击行
setRowEvent.call(this, 'rowDouble');
});
//创建行单击、双击事件监听
var setRowEvent = function (eventType) {
var othis = $(this);
var ELEM_CHILD = (options.mode == 'cardListMode' ? 'dic' : 'td')
layui.event.call(this,
MOD_NAME, eventType + '(' + filter + ')'
, commonMember.call(othis.children(ELEM_CHILD)[0])
);
};
//单元格编辑
that.layBody.on('change', '.' + ELEM_EDIT, function () {
var othis = $(this)
, value = this.value
, field = othis.parent().data('field')
, index = othis.parents('tr').eq(0).data('index')
, data = table.cache[that.key][index];
data[field] = value; //更新缓存中的值
layui.event.call(this, MOD_NAME, 'edit(' + filter + ')', commonMember.call(this, {
value: value
, field: field
}));
}).on('blur', '.' + ELEM_EDIT, function () {
var templet
, othis = $(this)
, field = othis.parent().data('field')
, index = othis.parents('tr').eq(0).data('index')
, data = table.cache[that.key][index];
that.eachCols(function (i, item) {
if (item.field == field && item.templet) {
templet = item.templet;
}
});
if (othis.parent().data('edit') == 'select') {
othis.siblings(ELEM_CELL).html(othis.find("option:selected").text());
} else {
othis.siblings(ELEM_CELL).html(function (value) {
return templet ? function () {
return typeof templet === 'function'
? templet(data)
: laytpl($(templet).html() || this.value).render(data)
}() : value;
}(this.value));
}
othis.parent().data('content', this.value);
othis.remove();
});
//单元格双击事件
that.layBody.on('dblclick', 'td', function () {
var othis = $(this)
, field = othis.data('field')
, editType = othis.data('edit')
, elemCell = othis.children(ELEM_CELL)
, index = othis.parents('tr').eq(0).data('index')
, data = table.cache[that.key][index];
if (options.event && options.event.onRowdbClick) {
$('tr[data-index="' + index + '"]').addClass('layui-table-click').siblings().removeClass('layui-table-click');
options.event.onRowdbClick(field, data);
}
});
//单元格单击事件
that.layBody.on('click', 'td', function () {
var othis = $(this)
, field = othis.data('field')
, editType = othis.data('edit')
, elemCell = othis.children(ELEM_CELL)
, index = othis.parents('tr').eq(0).data('index')
, data = table.cache[that.key][index];
if (options.event && options.event.onRowClick) {
$('tr[data-index="' + index + '"]').addClass('layui-table-click').siblings().removeClass('layui-table-click');
options.event.onRowClick(field, data);
}
layer.close(that.tipsIndex);
if (othis.data('off')) return; //不触发事件
//显示编辑表单
if (editType == 'text') {
var input = $('<input class="layui-input ' + ELEM_EDIT + '">');
input[0].value = othis.data('content') || elemCell.text();
othis.find('.' + ELEM_EDIT)[0] || othis.append(input);
input.focus();
return;
} else if (editType == 'select') {
var select = $('<select class="' + ELEM_EDIT + '" style="z-index:9999999; display: inline;">');
othis.find('.' + ELEM_EDIT)[0] || othis.append(select);
select.focus();
var findcol = function (fieldName) {
for (let i = 0; i < options.cols.length; i++) {
const cs = options.cols[i];
for (let j = 0; j < cs.length; j++) {
const col = cs[j];
if (col.field == fieldName) {
return col;
}
}
}
}
var col = findcol(field)
if (col.list) {
var opts = col.list();
for (let i = 0; i < opts.length; i++) {
const opt = opts[i];
var option = $("<option>").val(opt.key).text(opt.name);
select.append(option);
}
}
select.val(othis.data('selectvalue'));
return;
}
}).on('mouseenter', 'td', function () {
gridExpand.call(this)
}).on('mouseleave', 'td', function () {
gridExpand.call(this, 'hide');
});
//单元格展开图标
var ELEM_GRID = 'layui-table-grid', ELEM_GRID_DOWN = 'layui-table-grid-down', ELEM_GRID_PANEL = 'layui-table-grid-panel'
, gridExpand = function (hide) {
var othis = $(this)
, elemCell = othis.children(ELEM_CELL);
if (hide) {
othis.find('.layui-table-grid-down').remove();
} else if (elemCell.prop('scrollWidth') > elemCell.outerWidth()) {
if (elemCell.find('.' + ELEM_GRID_DOWN)[0]) return;
othis.append('<div class="' + ELEM_GRID_DOWN + '"><i class="layui-icon layui-icon-down"></i></div>');
}
};
//单元格展开事件
that.layBody.on('click', '.' + ELEM_GRID_DOWN, function (e) {
var othis = $(this)
, td = othis.parent()
, elemCell = td.children(ELEM_CELL);
that.tipsIndex = layer.tips([
'<div class="layui-table-tips-main" style="margin-top: -' + (elemCell.height() + 16) + 'px;' + function () {
if (options.size === 'sm') {
return 'padding: 4px 15px; font-size: 12px;';
}
if (options.size === 'lg') {
return 'padding: 14px 15px;';
}
return '';
}() + '">'
, elemCell.html()
, '</div>'
, '<i class="layui-icon layui-table-tips-c layui-icon-close"></i>'
].join(''), elemCell[0], {
tips: [3, '']
, time: -1
, anim: -1
, maxWidth: (device.ios || device.android) ? 300 : that.elem.width() / 2
, isOutAnim: false
, skin: 'layui-table-tips'
, success: function (layero, index) {
layero.find('.layui-table-tips-c').on('click', function () {
layer.close(index);
});
}
});
layui.stope(e);
});
//行工具条操作事件
that.layBody.on('click', '*[lay-event]', function () {
var othis = $(this)
, index = othis.parents(ELEM_OBJ).eq(0).data('index');
layui.event.call(this, MOD_NAME, 'tool(' + filter + ')', commonMember.call(this, {
event: othis.attr('lay-event')
}));
that.setThisRowChecked(index);
});
//同步滚动条
that.layMain.on('scroll', function () {
var othis = $(this)
, scrollLeft = othis.scrollLeft()
, scrollTop = othis.scrollTop();
that.layHeader.scrollLeft(scrollLeft);
that.layTotal.scrollLeft(scrollLeft);
that.layFixed.find(ELEM_BODY).scrollTop(scrollTop);
that.scrollPatch();
console.info("that.layHeader.scrollLeft:" + that.layHeader.scrollLeft())
layer.close(that.tipsIndex);
});
//全局点击
_DOC.on('click', function () {
_DOC.trigger('table.remove.tool.panel');
});
//工具面板移除事件
_DOC.on('table.remove.tool.panel', function () {
$('.layui-table-tool-panel').remove();
});
//自适应
_WIN.on('resize', function () {
that.fullSize();
that.scrollPatch();
that.setColsWidth();
});
// 左右拖拽调整列顺序、向上拖隐藏列
if (typeof options.drag === 'undefined' || options.drag) {
if (options.cols.length > 1) {
// 如果是复杂表头,则自动禁用拖动效果
return;
}
var $table = $(options.elem),
$tableBox = $table.next().children('.layui-table-box'),
$tableHead = $.merge($tableBox.children('.layui-table-header').children('table'),
$tableBox.children('.layui-table-fixed').children('.layui-table-header').children('table')),
$fixedBody = $tableBox.children('.layui-table-fixed').children('.layui-table-body').children('table'),
$noFixedBody = $tableBox.children('.layui-table-body').children('table'),
$tableBody = $.merge($tableBox.children('.layui-table-body').children('table'), $fixedBody),
$totalTable = $table.next().children('.layui-table-total').children('table'),
$fixedTotalTable = $table.next().children('.layui-table-total').children('table.layui-table-total-fixed'),
$noFixedTotalTable = $table.next().children('.layui-table-total').children('table:eq(0)'),
tableId = options.id,
isSimple = options.drag === 'simple' || (options.drag && options.drag.type === 'simple'), // 是否为简易拖拽
toolbar = options.drag && options.drag.toolbar, // 是否开启工具栏
isDraging = false, isStart = false;
if (!$tableHead.attr('drag')) {
$tableHead.attr('drag', true);
if (toolbar) {
$tableBox.append('<div class="soul-drag-bar"><div data-type="left">左固定</div><div data-type="none">不固定</div><div data-type="right">右固定</div></div>')
var $dragBar = $tableBox.children('.soul-drag-bar');
$dragBar.children('div').on('mouseenter', function () {
$(this).addClass('active')
}).on('mouseleave', function () {
$(this).removeClass('active')
})
}
$tableHead.find('th').each(function () {
var $this = $(this),
field = $this.data('field'),
key = $this.data('key');
if (!key) { return; }
var keyArray = key.split('-'),
curColumn = options.cols[keyArray[1]][keyArray[2]],
curKey = keyArray[1] + '-' + keyArray[2],
isInFixed = $this.parents('.layui-table-fixed').length > 0;
// 绑定鼠标按下事件
$(this).find('span:first,.laytable-cell-checkbox')
.css('cursor', 'move')
.on('mousedown', function (e) {
if (e.button !== 0) {
return;
}
e.preventDefault();
var $cloneHead = $this.clone().css('visibility', 'hidden'),
originLeft = $this.position().left,
originTop = $this.offset().top,
disX = e.clientX - originLeft, // 鼠标距离被移动元素左侧的距离
color = $this.parents('tr:eq(0)').css("background-color"),
width = $this.width(), moveDistince = 0,
$that = $(this),
isFixed = curColumn.fixed;
isStart = true;
//区分click、drag事件
// 阻止文本选中
$(document).bind("selectstart", function () {
return false;
});
// 移动事件
$('body').on('mousemove', function (e) {
if (isStart && $cloneHead) {
$tableBox.removeClass('no-left-border');
if (!isDraging) {
if (toolbar) {
$dragBar.attr('data-type', isFixed || 'none')
$dragBar.addClass('active')
}
$this.after($cloneHead);
$this.addClass('isDrag').css({
'position': 'absolute',
'z-index': 1,
'border-left': '1px solid #e6e6e6',
'background-color': color,
'width': width + 1
});
if (isSimple) {
//设置蒙板
} else {
(isInFixed ? $fixedBody : $tableBody).find('td[data-key="' + key + '"]').each(function () {
$(this).after($(this).clone().css('visibility', 'hidden').attr('data-clone', ''));
$(this).addClass('isDrag').css({
'position': 'absolute',
'z-index': 1,
'border-left': '1px solid #e6e6e6',
'background-color': $(this).css('background-color'),
'width': width + 1
});
})
if ($totalTable.length > 0) {
(isInFixed ? $fixedTotalTable : $totalTable).find('td[data-key="' + key + '"]').each(function () {
$(this).after($(this).clone().css('visibility', 'hidden').attr('data-clone', ''));
$(this).addClass('isDrag').css({
'position': 'absolute',
'z-index': 1,
'background-color': $(this).parents('tr:eq(0)').css('background-color'),
'width': width + 1
});
})
}
}
}
isDraging = true;
var x, y, i, j, tempCols,
left = e.clientX - disX, // 计算当前被移动列左侧位置应该哪里
$leftTh = $cloneHead.prev().prev(),
hasLeftTh = $leftTh.length > 0,
leftKey = hasLeftTh ? $leftTh.data('key').split('-') : [],
$rightTh = $cloneHead.next().hasClass('layui-table-patch') ? [] : $cloneHead.next(),
hasRightTh = $rightTh.length > 0,
rightKey = hasRightTh ? $rightTh.data('key').split('-') : [],
leftMove = hasLeftTh && ($cloneHead.position().left - left > $leftTh.width() / 2.0),
rightMove = hasRightTh && (left - $cloneHead.position().left > $rightTh.width() / 2.0);
moveDistince = Math.abs($cloneHead.position().left - left); //记录移动距离
// 移动到左右两端、checbox/radio 固定列等停止移动
if ($cloneHead.position().left - left > 0
? !hasLeftTh || !!isFixed !== !!options.cols[leftKey[1]][leftKey[2]].fixed
: !hasRightTh || !!isFixed !== !!options.cols[rightKey[1]][rightKey[2]].fixed) {
$this.css('left', $cloneHead.position().left);
$tableBody.find('td[data-key="' + key + '"][data-clone]').each(function (e) {
$(this).prev().css('left', $cloneHead.position().left);
})
if ($totalTable.length > 0) {
$totalTable.find('td[data-key="' + key + '"][data-clone]').each(function (e) {
$(this).prev().css('left', $cloneHead.position().left);
})
}
$tableBox.addClass('no-left-border');
return;
}
$this.css('left', left);
if (leftMove) {
$cloneHead.after($leftTh);
// 更新隐藏列顺序
$('#soul-columns' + tableId + '>li[data-value=' + field + ']').after($('#soul-columns' + tableId + '>li[data-value=' + field + ']').prev())
// 更新配置信息
for (i = 0; i < options.cols.length; i++) {
for (j = 0; j < options.cols[i].length; j++) {
if (options.cols[i][j].key === curKey) {
x = i;
y = j;
break;
}
}
if (typeof x !== 'undefined' && typeof y !== 'undefined') {
break;
}
}
tempCols = options.cols[x][y - 1];
options.cols[x][y - 1] = options.cols[x][y];
options.cols[x][y] = tempCols;
if (options.filter && options.filter.cache) {
localStorage.setItem(location.pathname + location.hash + options.id, _this.deepStringify(options.cols))
}
} else if (rightMove) {
$cloneHead.prev().before($rightTh);
// 更新隐藏列顺序
$('#soul-columns' + tableId + '>li[data-value=' + field + ']').before($('#soul-columns' + tableId + '>li[data-value=' + field + ']').next())
// 更新配置信息
for (i = 0; i < options.cols.length; i++) {
for (j = 0; j < options.cols[i].length; j++) {
if (options.cols[i][j].key === curKey) {
x = i;
y = j;
break;
}
}
if (typeof x !== 'undefined' && typeof y !== 'undefined') {
break;
}
}
tempCols = options.cols[x][y + 1];
options.cols[x][y + 1] = options.cols[x][y];
options.cols[x][y] = tempCols;
if (options.filter && options.filter.cache) {
localStorage.setItem(location.pathname + location.hash + options.id, _this.deepStringify(options.cols))
}
}
$tableBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().css('left', left);
if (leftMove) {
if ($(this).prev().prev().length !== 0) {
$(this).after($(this).prev().prev());
}
} else if (rightMove) {
if ($(this).next().length !== 0) {
$(this).prev().before($(this).next());
}
}
})
if ($totalTable.length > 0) {
$totalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().css('left', left);
if (leftMove) {
if ($(this).prev().prev().length !== 0) {
$(this).after($(this).prev().prev());
}
} else if (rightMove) {
if ($(this).next().length !== 0) {
$(this).prev().before($(this).next());
}
}
})
}
/* 拖动隐藏列 */
if (e.clientY - originTop < -15) {
if ($('#column-remove').length === 0) {
$('body').append('<i id="column-remove" class="layui-red layui-icon layui-icon-delete"></i>')
}
$('#column-remove').css({
top: e.clientY - $('#column-remove').height() / 2,
left: e.clientX - $('#column-remove').width() / 2,
'font-size': (originTop - e.clientY) + 'px'
})
$('#column-remove').show();
} else {
$('#column-remove').hide();
}
}
}).on('mouseup', function () {
$(document).unbind("selectstart");
$('body').off('mousemove').off('mouseup')
if (isStart && $cloneHead) {
isStart = false;
if (isDraging) {
if (curColumn.type !== 'checkbox') {
$that.on('click', function (e) {
e.stopPropagation();
});
}
isDraging = false;
$tableBox.removeClass('no-left-border')
$this.removeClass('isDrag').css({
'position': 'relative',
'z-index': 'inherit',
'left': 'inherit',
'border-left': 'inherit',
'width': 'inherit',
'background-color': 'inherit'
});
$this.next().remove();
var prefKey = $this.prev().data('key');
if (isFixed) {
var $noFixedTh = $tableBox.children('.layui-table-header').children('table').find('th[data-key="' + key + '"]');
if (prefKey) {
$noFixedTh.parent().children('th[data-key="' + prefKey + '"]').after($noFixedTh)
} else {
if (isFixed === 'right') {
if ($this.siblings().length > 0) {
$tableBox.children('.layui-table-header').children('table').find('th[data-key="' + $this.next().data('key') + '"]').prev().after($noFixedTh);
}
} else {
$noFixedTh.parent().prepend('<th class="layui-hide"></th>');
$noFixedTh.parent().children('th:first').after($noFixedTh);
$noFixedTh.parent().children('th:first').remove();
}
}
}
if (isSimple) {
$tableBody.find('td[data-key="' + key + '"]').each(function () {
if (prefKey) {
$(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
} else {
if (isFixed === 'right') {
if ($this.siblings().length > 0) {
var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
if ($preTd.length > 0) {
$preTd.after($(this));
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
});
if ($totalTable.length > 0) {
$totalTable.find('td[data-key="' + key + '"]').each(function () {
if (prefKey) {
$(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
} else {
if (isFixed === 'right') {
var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
if ($preTd.length > 0) {
$preTd.after($(this));
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
});
}
} else if (isInFixed) {
$noFixedBody.find('td[data-key="' + key + '"]').each(function () {
if (prefKey) {
$(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
} else {
if (isFixed === 'right') {
if ($this.siblings().length > 0) {
var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
if ($preTd.length > 0) {
$preTd.after($(this));
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
});
$fixedBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().removeClass('isDrag').css({
'position': 'relative',
'z-index': 'inherit',
'left': 'inherit',
'border-left': 'inherit',
'width': 'inherit',
'background-color': 'inherit'
});
$(this).remove();
});
if ($totalTable.length > 0) {
$noFixedTotalTable.find('td[data-key="' + key + '"]').each(function () {
if (prefKey) {
$(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
} else {
if (isFixed === 'right') {
var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
if ($preTd.length > 0) {
$preTd.after($(this));
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
} else {
$(this).parent().prepend('<td class="layui-hide"></td>');
$(this).parent().children('td:first').after($(this));
$(this).parent().children('td:first').remove();
}
}
});
$fixedTotalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().removeClass('isDrag').css({
'position': 'relative',
'z-index': 'inherit',
'left': 'inherit',
'width': 'inherit',
'background-color': 'inherit'
});
$(this).remove();
});
}
} else {
$tableBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().removeClass('isDrag').css({
'position': 'relative',
'z-index': 'inherit',
'left': 'inherit',
'width': 'inherit',
'background-color': 'inherit'
});
$(this).remove();
});
if ($totalTable.length > 0) {
$totalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
$(this).prev().removeClass('isDrag').css({
'position': 'relative',
'z-index': 'inherit',
'left': 'inherit',
'width': 'inherit',
'background-color': 'inherit'
});
$(this).remove();
});
}
}
$cloneHead = null;
// 处理 toolbar 事件
if (toolbar) {
if ($dragBar.children('.active').length > 0 && $dragBar.children('.active').attr('data-type') !== $dragBar.attr('data-type')) {
var targetFix = $dragBar.children('.active').attr('data-type'),
i, j, curPos, targetPos;
for (i = 0; i < options.cols.length; i++) {
for (j = 0; j < options.cols[i].length; j++) {
if (targetFix === 'right' || (targetFix === 'none' && $dragBar.attr('data-type') === 'right')) {
if (typeof targetPos === 'undefined') {
if (options.cols[i][j].fixed === 'right') {
targetPos = { x: i, y: j };
} else if (j === options.cols[i].length - 1) {
targetPos = { x: i, y: j + 1 };
}
}
} else {
if (typeof targetPos === 'undefined' && (!options.cols[i][j].fixed || options.cols[i][j].fixed === 'right')) {
targetPos = { x: i, y: j };
}
}
if (options.cols[i][j].key === curKey) {
curPos = { x: i, y: j };
}
}
}
curColumn['fixed'] = targetFix === 'none' ? false : targetFix;
if (curPos.y !== targetPos.y) {
options.cols[curPos.x].splice(curPos.y, 1);
if (curPos.y < targetPos.y) {
targetPos.y -= 1
}
options.cols[targetPos.x].splice(targetPos.y, 0, curColumn)
if (options.filter && options.filter.cache) {
localStorage.setItem(location.pathname + location.hash + options.id, _this.deepStringify(options.cols))
}
}
table.reload(tableId)
}
$dragBar.removeClass('active')
}
} else {
$that.unbind('click');
}
if ($('#column-remove').is(':visible')) {
$tableHead.find('thead>tr>th[data-key=' + key + ']').addClass(HIDE);
$tableBody.find('tbody>tr>td[data-key="' + key + '"]').addClass(HIDE);
$totalTable.find('tbody>tr>td[data-key="' + key + '"]').addClass(HIDE);
// 同步配置
curColumn['hide'] = true
if (options.filter && options.filter.cache) {
localStorage.setItem(location.pathname + location.hash + options.id, _this.deepStringify(options.cols))
}
// 更新下拉隐藏
$('#soul-columns' + tableId).find('li[data-value="' + field + '"]>input').prop('checked', false);
tableFilter.resize(options)
}
$('#column-remove').hide();
}
})
});
})
}
}
// 行拖拽事件
if (options.rowDrag) {
var $table = $(options.elem),
$tableBox = $table.next().children('.layui-table-box'),
$fixedBody = $tableBox.children('.layui-table-fixed').children('.layui-table-body').children('table'),
$noFixedBody = $tableBox.children('.layui-table-body').children('table'),
$tableBody = $.merge($tableBox.children('.layui-table-body').children('table'), $fixedBody),
tableId = options.id,
isDraging = false,
trigger = options.rowDrag.trigger || 'row',
$trs = trigger === 'row' ? $tableBody.children('tbody').children('tr') : $tableBody.find(trigger);
if (trigger !== 'row') {
$tableBody.find(trigger).css('cursor', 'move')
}
$trs.on('mousedown', function (e) {
if (e.button !== 0) {
return;
}
var $this = trigger === 'row' ? $(this) : $(this).parents('tr:eq(0)'),
index = parseInt($this.data('index')),
$bodyTr = $noFixedBody.children('tbody').children('tr[data-index=' + index + ']'),
$cloneTr = $bodyTr.clone().css('visibility', 'hidden'),
$FixBodyTr = $fixedBody.children('tbody').children('tr[data-index=' + index + ']'),
bodyScrollTop = $tableBox.children('.layui-table-body').scrollTop(), // 记录当前滚动条位置
originTop = $this.position().top,
disY = e.clientY - originTop; // 鼠标距离被移动元素上侧的距离
$('body').on('mousemove', function (e) {
if (!isDraging) {
isDraging = true;
// 设置鼠标样式
// $table.next().find('style').append('.layui-table-view .layui-table td{cursor: move;}.layui-table tr{transition: none}')
var style = $table.next().find('style')[0],
sheet = style.sheet || style.styleSheet || {};
that.addCSSRule(sheet, '.layui-table-view .layui-table td', 'cursor: move')
that.addCSSRule(sheet, '.layui-table tr', 'transition: none')
$tableBox.addClass('noselect'); // 禁止选中文本
$bodyTr.after($cloneTr);
$bodyTr.css({
'position': 'absolute',
'z-index': 1
})
$FixBodyTr.each(function () {
$(this).after($(this).clone().css('visibility', 'hidden'))
$(this).css({
'position': 'absolute',
'z-index': 102
})
})
}
var top = e.clientY - disY + ($tableBox.children('.layui-table-body').scrollTop() - bodyScrollTop), // 计算当前被移动行top位置应该哪里
trTop = $cloneTr.position().top, //当前行所在位置
$UpTr = $bodyTr.prev(),
hasUpTr = $UpTr.length > 0,
$downTr = $cloneTr.next(),
hasDownTr = $downTr.length > 0,
upMove = hasUpTr && (trTop - top > $UpTr.height() / 2.0),
downMove = hasDownTr && (top - trTop > $downTr.height() / 2.0);
if (trTop - top > 0 ? !hasUpTr : !hasDownTr) {
$bodyTr.css('top', trTop);
$FixBodyTr.each(function () {
$(this).css('top', trTop);
})
return;
}
$bodyTr.css('top', top);
$FixBodyTr.each(function () {
$(this).css('top', top);
})
if (upMove) {
updateDataIndex($bodyTr, -1)
$cloneTr.after(updateDataIndex($UpTr, 1));
$FixBodyTr.each(function () {
updateDataIndex($(this), -1)
$(this).next().after(updateDataIndex($(this).prev(), 1));
})
} else if (downMove) {
updateDataIndex($bodyTr, 1).before(updateDataIndex($downTr, -1))
$FixBodyTr.each(function () {
updateDataIndex($(this), 1);
$(this).before(updateDataIndex($(this).next().next(), -1));
})
}
// 同步 data-index
function updateDataIndex($el, diff) {
var tempIndex = parseInt($el.data('index')) + diff;
$el.data('index', tempIndex);
$el.attr('data-index', tempIndex);
return $el
}
}).on('mouseup', function (e) {
$('body').off('mousemove').off('mouseup');
if (isDraging) {
isDraging = false;
$tableBox.removeClass('noselect'); // 取消禁止选中文本
$bodyTr.css({ 'position': 'inherit', 'z-index': 'inherit' });
$bodyTr.next().remove();
$FixBodyTr.each(function () {
$(this).css({ 'position': 'inherit', 'z-index': 'inherit' });
$(this).next().remove()
})
// 恢复样式
var style = $table.next().find('style')[0],
sheet = style.sheet || style.styleSheet || {},
rules = sheet.cssRules || sheet.rules;
layui.each(rules, function (i, item) {
if (item.selectorText === ('.layui-table-view .layui-table td')) {
item.style.cursor = 'default';
}
});
var newIndex = $this.index();
if (newIndex !== index) { // 有位置变动
if (typeof options.rowDrag.done === 'function') {
var cache = layui.table.cache[tableId],
row = cache.splice(index, 1)[0];
cache.splice(newIndex, 0, row);
options.rowDrag.done.call(options, {
row: row,
newIndex: newIndex,
oldIndex: index,
cache: cache
})
}
}
}
})
})
}
};
// 扩展添加属性
// 记录拖拽样式调整
Class.prototype.addCSSRule = function (sheet, selector, rules, index) {
if ('insertRule' in sheet) {
sheet.insertRule(selector + '{' + rules + '}', index)
} else if ('addRule' in sheet) {
sheet.addRule(selector, rules, index)
}
};
//初始化
table.init = function (filter, settings) {
settings = settings || {};
var that = this
, elemTable = filter ? $('table[lay-filter="' + filter + '"]') : $(ELEM + '[lay-data]')
, errorTips = 'Table element property lay-data configuration item has a syntax error: ';
//遍历数据表格
elemTable.each(function () {
var othis = $(this), tableData = othis.attr('lay-data');
try {
tableData = new Function('return ' + tableData)();
} catch (e) {
hint.error(errorTips + tableData)
}
var cols = [], options = $.extend({
elem: this
, cols: []
, data: []
, skin: othis.attr('lay-skin') //风格
, size: othis.attr('lay-size') //尺寸
, even: typeof othis.attr('lay-even') === 'string' //偶数行背景
}, table.config, settings, tableData);
filter && othis.hide();
//获取表头数据
othis.find('thead>tr').each(function (i) {
options.cols[i] = [];
$(this).children().each(function (ii) {
var th = $(this), itemData = th.attr('lay-data');
try {
itemData = new Function('return ' + itemData)();
} catch (e) {
return hint.error(errorTips + itemData)
}
var row = $.extend({
title: th.text()
, colspan: th.attr('colspan') || 0 //列单元格
, rowspan: th.attr('rowspan') || 0 //行单元格
}, itemData);
if (row.colspan < 2) cols.push(row);
options.cols[i].push(row);
});
});
//获取表体数据
othis.find('tbody>tr').each(function (i1) {
var tr = $(this), row = {};
//如果定义了字段名
tr.children('td').each(function (i2, item2) {
var td = $(this)
, field = td.data('field');
if (field) {
return row[field] = td.html();
}
});
//如果未定义字段名
layui.each(cols, function (i3, item3) {
var td = tr.children('td').eq(i3);
row[item3.field] = td.html();
});
options.data[i1] = row;
});
table.render(options);
});
return that;
};
//记录所有实例配置信息
thisTable.config = {};
//遍历表头
table.eachCols = function (id, callback, cols) {
var config = thisTable.config[id] || {}
, arrs = [], index = 0;
cols = $.extend(true, [], cols || config.cols);
//重新整理表头结构
layui.each(cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
//如果是组合列,则捕获对应的子列
if (item2.colGroup) {
var childIndex = 0;
index++
item2.CHILD_COLS = [];
layui.each(cols[i1 + 1], function (i22, item22) {
//如果子列已经被标注为{PARENT_COL_INDEX},或者子列累计 colspan 数等于父列定义的 colspan则跳出当前子列循环
if (item22.PARENT_COL_INDEX || (childIndex > 1 && childIndex == item2.colspan)) return;
item22.PARENT_COL_INDEX = index;
item2.CHILD_COLS.push(item22);
childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
});
}
if (item2.PARENT_COL_INDEX) return; //如果是子列,则不进行追加,因为已经存储在父列中
arrs.push(item2)
});
});
//重新遍历列,如果有子列,则进入递归
var eachArrs = function (obj) {
layui.each(obj || arrs, function (i, item) {
if (item.CHILD_COLS) return eachArrs(item.CHILD_COLS);
typeof callback === 'function' && callback(i, item);
});
};
eachArrs();
};
//遍历统计列的内容,用于统计卡片列表
table.eachTotalCols = function (id, callback, totalcols) {
var config = thisTable.config[id] || {}
, arrs = [], index = 0;
totalcols = $.extend(true, [], totalcols || config.totalcols);
//重新整理表头结构
layui.each(totalcols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
//如果是组合列,则捕获对应的子列
if (item2.colGroup) {
var childIndex = 0;
index++
item2.CHILD_COLS = [];
layui.each(totalcols[i1 + 1], function (i22, item22) {
//如果子列已经被标注为{PARENT_COL_INDEX},或者子列累计 colspan 数等于父列定义的 colspan则跳出当前子列循环
if (item22.PARENT_COL_INDEX || (childIndex > 1 && childIndex == item2.colspan)) return;
item22.PARENT_COL_INDEX = index;
item2.CHILD_COLS.push(item22);
childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
});
}
if (item2.PARENT_COL_INDEX) return; //如果是子列,则不进行追加,因为已经存储在父列中
arrs.push(item2)
});
});
//重新遍历列,如果有子列,则进入递归
var eachArrs = function (obj) {
layui.each(obj || arrs, function (i, item) {
if (item.CHILD_COLS) return eachArrs(item.CHILD_COLS);
typeof callback === 'function' && callback(i, item);
});
};
eachArrs();
};
//表格选中状态
table.checkStatus = function (id) {
var nums = 0
, invalidNum = 0
, arr = []
, data = table.cache[id] || [];
//计算全选个数
layui.each(data, function (i, item) {
if (item.constructor === Array) {
invalidNum++; //无效数据,或已删除的
return;
}
if (item[table.config.checkName]) {
nums++;
arr.push(table.clearCacheKey(item));
}
});
return {
data: arr //选中的数据
, isAll: data.length ? (nums === (data.length - invalidNum)) : false //是否全选
};
};
//表格带有数据源的导出
table.exportFileZlDataSource = function (id, excelmodel) {
var config = thisTable.config[id] || {},
url = layui.cache['contentPath'] + '/manager/coredatasource/export';
var params = {};
if (excelmodel.datalist == '0') {
params[config.request.pageName] = config.page.curr;
params[config.request.limitName] = config.limit;
} else if (excelmodel.datalist == '2') {
params[config.request.pageName] = 1;
params[config.request.limitName] = 1000000;
}
params['extraMap'] = {
modelId: excelmodel.modelId,
dsId: config.dataSource.id,
// [config.request.pageName]: params[config.request.pageName],
// [config.request.limitName]: params[config.request.limitName]
}
params['extraMap'][config.request.pageName] = params[config.request.pageName];
params['extraMap'][config.request.limitName] = params[config.request.limitName];
//参数
var model = $.extend(params, excelmodel);
//参数
var data = $.extend(params, config.where);
var html = ('<form id="downloadexcelform" action="' + url + '" method="post" style="display:none">');
if ($("#downloadexcelform").length > 0) {
$("#downloadexcelform").remove();
}
if (model) {
model = JSON.stringify(model).replace(/"/g, "&quot;").replace(/'/g, "&apos;");
html += '<input name="model" value=\'' + model + '\'/>';
}
if (data) {
$.each(data, function (k, v) {
if (v) {
html += '<input name="' + k + '" value=\'' + v + '\'/>';
}
})
}
html += '</form>';
$("body").append(html);
$("#downloadexcelform").prop('action', url);
$("#downloadexcelform").submit();
};
//表格导出
table.exportFileZl = function (id, excelmodel) {
var config = thisTable.config[id] || {}
if (config.url) { //Ajax请求
var url = config.url;
var params = {};
params[config.request.pageName] = 1;
params[config.request.limitName] = 1000000;
if (url.indexOf("?") > -1) {
url = url.substring(0, url.indexOf("?")) + "export" + url.substring(url.indexOf("?"), url.length());
} else {
url = url + "export";
}
//参数
var data = $.extend(params, config.where);
var html = ('<form id="downloadexcelform" action="' + url + '" method="post" style="display:none">');
if ($("#downloadexcelform").length > 0) {
$("#downloadexcelform").remove();
}
if (excelmodel) {
excelmodel = JSON.stringify(excelmodel);
html += '<input name="model" value=\'' + excelmodel + '\'/>';
}
if (data) {
$.each(data, function (k, v) {
if (v) {
html += '<input name="' + k + '" value=\'' + v + '\'/>';
}
})
}
html += '</form>';
$("body").append(html);
$("#downloadexcelform").prop('action', url);
$("#downloadexcelform").submit();
};
}
//表格导出
table.exportFile = function (id, data, type) {
data = data || table.clearCacheKey(table.cache[id]);
type = type || 'csv';
var config = thisTable.config[id] || {}
, textType = ({
csv: 'text/csv'
, xls: 'application/vnd.ms-excel'
})[type]
, alink = document.createElement("a");
if (device.ie) return hint.error('IE_NOT_SUPPORT_EXPORTS');
alink.href = 'data:' + textType + ';charset=utf-8,\ufeff' + encodeURIComponent(function () {
var dataTitle = [], dataMain = [];
layui.each(data, function (i1, item1) {
var vals = [];
if (typeof id === 'object') { //ID直接为表头数据
layui.each(id, function (i, item) {
i1 == 0 && dataTitle.push(item || '');
});
layui.each(table.clearCacheKey(item1), function (i2, item2) {
vals.push(item2);
});
} else {
table.eachCols(id, function (i3, item3) {
if (item3.field && item3.type == 'normal' && !item3.hide) {
i1 == 0 && dataTitle.push(item3.title || '');
vals.push(item1[item3.field]);
}
});
}
dataMain.push(vals.join(','))
});
return dataTitle.join(',') + '\r\n' + dataMain.join('\r\n');
}());
alink.download = (config.title || 'table_' + (config.index || '')) + '.' + type;
document.body.appendChild(alink);
alink.click();
document.body.removeChild(alink);
};
//重置表格尺寸结构
table.resize = function (id) {
//如果指定表格唯一 id则只执行该 id 对应的表格实例
if (id) {
var config = getThisTableConfig(id); //获取当前实例配置项
if (!config) return;
thisTable.that[id].resize();
} else { //否则重置所有表格实例尺寸
layui.each(thisTable.that, function () {
this.resize();
});
}
};
//表格重载
table.reload = function (id, options) {
var config = thisTable.config[id];
options = options || {};
if (!config) return hint.error('The ID option was not found in the table instance');
if (options.data && options.data.constructor === Array) delete config.data;
return table.render($.extend(true, {}, config, options));
};
//核心入口
table.render = function (options) {
var inst = new Class(options);
return thisTable.call(inst);
};
//清除临时Key
table.clearCacheKey = function (data) {
data = $.extend({}, data);
delete data[table.config.checkName];
delete data[table.config.indexName];
return data;
};
//删除打勾的行modify by gk
table.deleteCheckRows = function (id) {
var data = table.cache[id] || [];
$.each(data, function (i, item) {
if (item.constructor !== Array && item[table.config.checkName]) {
var elmobj = (table.config.mode == 'cardListMode' ? '.card-item' : 'table tbody tr');
var check = item[table.config.checkName];
var index = item[table.config.indexName];
var tr = $('#' + id).next().find(elmobj + '[data-index=' + index + ']');
tr.remove();
table.cache[id][index] = [];
}
});
};
table.formatNumberByPermil = function (num) {
var decimalPart = '';
num = num.toString();
if (num.indexOf('.') != -1) {
decimalPart = '.' + num.split('.')[1];
num = parseInt(num.split('.')[0]);
}
var array = num.toString().split('');
var index = -3;
while (array.length + index > 0) {
array.splice(index, 0, ',');
index -= 4;
}
return array.join('') + decimalPart;
};
//获取当前获取的所有集合值
table.getValue = function (id) {
return table.checkedMultiList[id];
}
// 设置选中的id(一般在编辑时候调用初始化选中值)
table.setIds = function (settings) {
var param = {
gridId: ''
//数据集合
, ids: []
};
$.extend(param, settings);
table.checkedMultiList[param.gridId] = [];
$.each(param.ids, function (index, item) {
table.checkedMultiList[param.gridId].push(item);
});
}
//自动完成渲染
table.init();
exports(MOD_NAME, table);
});