导出按钮 :
<a class="layui-btn layui-btn-normal" id="export"><i class="layui-icon"></i>导出</a>
导出事件:
//导出数据
$("#export").on("click",function () {
xadmin.export(tableIns,'${ctx}/house/outbound/list',where(),'出库');
});
PS : 参数1 tableIns 是 你的table 加载完成的函数
var table_url = '${ctx}/xx/xx/list';
if(${status != null}){
table_url = '${ctx}/xx/xx/list?status=${status}&ck_status=${ck_Status}';
}
//分页列表 参数 1
var tableIns = table.render({
elem: '#table',
url: table_url,
page: true,
toolbar: "#toolbar",
height: "full-120",
loading: true,
limits: [15, 30, 50, 80,100],
limit:15,
totalRow:true,
request: {
pageName: 'pageNumber' //页码的参数名称,默认:page
,limitName: 'pageSize' //每页数据量的参数名,默认:limit
},
response: {
statusName: 'code' //规定数据状态的字段名称,默认:code
,statusCode: 10200 //规定成功的状态码,默认:0
,msgName: 'message' //规定状态信息的字段名称,默认:msg
,countName: 'total' //规定数据总数的字段名称,默认:count
,dataName: 'data' //规定数据列表的字段名称,默认:data
},
cols: [[
]],
done: function(res, curr, count){
//这段代码是统计符合查询条件的所有统计 而不是当前页的统计。 PS: 需要查库统计
xadmin.totalOpen(res,curr,count,'${ctx}/xx/xx/total',where());
}
});
参数 2 是 url 参数, 参数 3 是 查询条件参数
//参数 3
function where(){
var where = new Object();
where.userName = $(".userName").val();
where.clientName = $(".clientName").val();
where.transportName = $(".transportName").val();
where.outboundNo = $(".outboundNo").val();
where.outboundIsCode = $(".outboundIsCode").val();
where.houseId = $(".houseId").val();
where.startTime = $(".startTime").val();
where.endTime = $(".endTime").val();
where.checkStatus = $(".checkStatus").val();
where.ckStatus = $(".ckStatus").val();
where.transportId = $(".transportId").val();
where.invoiceCount = $(".invoiceCount").val();
return where;
}
参数 4 是 导出 Excel的名称
导出Excel 主要JS 如下
//导出数据
Xadmin.prototype.export = function(table,url,where,title,params) { /*下载模板提示*/
//console.log(where);
layui.layer.confirm('将导出所有符合条件的数据,确定吗?', {
icon: 3,
title: "请确保先点搜索得到数据",
closeBtn: 0,
anim: Math.ceil(Math.random() * 6),
btn: ['确定', '取消']
}, function (index) {
layer.close(index);
params = null != params ? "&"+params : "";
var countHtml = $(".layui-laypage-count").html();
if(undefined == countHtml){
layui.layer.open({
icon: 0,
title : '系统提示',
content: '当前搜索条件无数据可以导出!',
anim: Math.ceil(Math.random() * 6),
yes: function(index, layero){
layer.close(index); //如果设定了yes回调,需进行手工关闭
}
});
return;
}
var count = countHtml.replace(/[^0-9]/ig,"");
if(count>2000){
layui.layer.open({
icon: 0,
title : '系统提示',
content: '最大只可导出<span style="color: red;font-size: 15px;font-weight: 800;padding: 0 8px;">2000</span>条数据!',
anim: Math.ceil(Math.random() * 6),
yes: function(index, layero){
layer.close(index); //如果设定了yes回调,需进行手工关闭
}
});
return false;
}
var start = new Date(); //开始时间
var load = window.top.layer.load(3,{
time: 300*1000,
shade: [0.4,'gray'],
content: '正在导出...',
success: function (layerContentStyle) {
layerContentStyle.find('.layui-layer-content').css({
'padding-top': '35px',
/* 'text-align': 'left',*/
'color':'red',
'font-size':'14px',
'font-weight':'600',
'width': '160px'
});
}
});
$.ajax({
url: url+'?pageNumber=1&pageSize='+count+params,
data: where,
dataType: 'json',
success: function (res) {
layui.table.exportFile(table.config.id, res.data, 'xls',title+"_数据导出"+new Date().getTime());
window.top.layer.close(load);
var end = new Date(); //结束时间
var time = parseInt((end.getTime() - start.getTime())/1000);
layui.layer.open({
icon: 6,
title : '系统提示',
content: title+'<span style="color: green;font-size: 15px;font-weight: 600;padding: 0 5px;">'+count+'</span>'+'条导出成功,用时<span style="padding: 0 3px;color: orange;font-size: 15px;font-weight: 600;">'+time+'</span>秒',
anim: Math.ceil(Math.random() * 6),
yes: function(index, layero){
layer.close(index); //如果设定了yes回调,需进行手工关闭
}
});
},
error: function () {
window.top.layer.close(load);
layui.layer.open({
icon: 5,
title : '系统提示',
content: '系统繁忙,请稍后再试!',
anim: Math.ceil(Math.random() * 6),
yes: function(index, layero){
layer.close(index); //如果设定了yes回调,需进行手工关闭
}
});
}
});
}, function (index) {
layer.close(index);
});
}