layui.define(['jquery','layer','table'], function(exports) {
var $ = layui.$,
layer = layui.layer,
table = layui.table;
var MOD_NAME = 'xlsx';
var modFile = layui.cache.modules['xlsx'];
var modPath = modFile.substr(0, modFile.lastIndexOf('.'));
var tableXlsx=$.extend({},table);
tableXlsx._render = tableXlsx.render;
tableXlsx.excel = function(data,page_size,obj,filter,merge){
//表头工具栏导出按钮
$('[lay-id="'+obj.id+'"]').find('[lay-event="LAYTABLE_EXCEL"]').off().on('click',function(){
if(data.count==0){
layer.msg('暂无数据');
return false;
}
else{
let _page = parseInt(data.count/page_size);
let page = data.count%page_size>0?(_page+1):_page;
let pageHtml='
';
for (i = 1; i <= page; i++) {
pageHtml += '
'+i+'';
}
pageHtml+='
';
layer.open({
type: 1,
title: '导出数据',
area:['580px','240px'],
content: pageHtml,
success:function(res){
var tableWhere = JSON.parse(JSON.stringify(obj.where));
tableWhere.limit=page_size;
$('#exportPage').on('click','a',function(){
tableWhere.page=$(this).data('page');
let msg = layer.msg('正在导出数据...', {time:5000});
$.ajax({
url: obj.url,
data: tableWhere,
success:function(res){
//table.exportFile(obj.id, res.data,'xls');
//console.log(res.data);
tableXlsx.xsls(res.data,obj.cols[0],obj.title,filter,merge);
layer.close(msg);
}
});
})
}
});
return false;
}
});
}
tableXlsx.xsls = function(jsonData,field,name,filter,merge){
let new_jsonData;
//数据过滤
if(filter.length>0){
new_jsonData = jsonData.map((item, index) => {
const newProps = {}; // 创建一个空对象,用于存放新增的属性和对应的计算值
filter.forEach((obj) => {
for (const [key, func] of Object.entries(obj)) {
newProps[key] = func(item); // 调用函数计算属性值,并赋给新的属性
}
});
return { ...item, ...newProps }; // 创建新对象,将原有的属性展开并新增 newProps 中的属性
});
}
else{
new_jsonData = jsonData;
}
console.log("===========打印初始化后的json数据============");
console.log(new_jsonData);
//需要显示的表格字段,及表头文字描述
let field_array =[],customHeaders = [];
for (var f=0; f