在前端写一个导出按钮
<button type="button" class="layui-btn" style="margin-left: 30%" id="exportBtn">导出数据</button>
在js里加上事件,其中exportList是查出所有的数据
<script>
var BASE_PATH = "[[${mainDomain}]]";
layui.config({
base: '/zcmdmooc/commons/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'table'], function(){
var table = layui.table;
table.on('tool(ffrstable)', function(obj){ //注:tool 是工具条事件名,test 是 table 原始容器的属性 lay-filter="对应的值"
var data = obj.data; //获得当前行数据
var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
var tr = obj.tr; //获得当前行 tr 的 DOM 对象(如果有的话)
if(layEvent === 'edit'){ //编辑
location.href="[[${mainDomain}]]/enConEduAdmin/toProblemDetail?id="+data.id;
} else if(layEvent === 'LAYTABLE_TIPS'){
layer.alert('Hi,头部工具栏扩展的右侧图标。');
}
});
//第一个实例
table.render({
elem: '#test-table-simple',
id:'exportTable',
title:'导出数据',
height: 'auto',
even: true, //开启隔行背景
skin: 'nob',
cellMinWidth: 80,
url: BASE_PATH + '/enConEduAdmin/problemList', //数据接口
page: true, //开启分页
response: {
statusName: 'result' //规定数据状态的字段名称,默认:code
,statusCode: 0 //规定成功的状态码,默认:0
,msgName: 'msg' //规定状态信息的字段名称,默认:msg
,countName: 'count' //规定数据总数的字段名称,默认:count
,dataName: 'data' //规定数据列表的字段名称,默认:data
},
done: function(res, curr, count){
//如果是异步请求数据方式,res即为你接口返回的信息。
//如果是直接赋值的方式,res即为:{data: [], count: 99} data为当前页数据、count为数据总长度
console.log(res);
//$("#total").html("在职"+count+"人");
//得到当前页码
//console.log(curr);
//得到数据总量
//console.log(count);
}
,request: {
pageName: 'pageNumber' //页码的参数名称,默认:page
,limitName: 'pageSize' //每页数据量的参数名,默认:limit
},
cols: [
[ //表头
{field: 'id', title: '序号'},
{field: 'phone', title: '提问者手机号'},
{field: 'createTime', title: '提问时间',width:160,templet:function (param) {
return new Date(param.createTime).Format('yyyy-MM-dd HH:mm:ss');
}},
{field: 'type', title: '问题类型'},
{field: 'problem', title: '问题选项'},
{field : 'status',title : '是否精选', align:'center', halign: 'center',
templet:function(value){
if(value.status == 1){
return "已精选";
} else if(value.status == 0){
return "未精选";
}else{
return "-";
}
}},
{
fixed: 'right',
title: '操作',
align: 'center',
width: 170,
toolbar: "#barrstable"
}
]
]
});
//导出
$('#exportBtn').on('click',function () {
//使用ajax请求获取所有数据
$.ajax({
url: BASE_PATH + '/enConEduAdmin/exportList',
type: 'post',
data: {
type: 1
},
async: false,
dataType: 'json',
success: function (res) {
//使用table.exportFile()导出数据
table.exportFile('exportTable', res.data, 'xls');
}
});
});
});
</script>