打印功能的实现往往是新建一个页面,然后在新建页面中画出你想要的效果,然后调用浏览器的打印功能进行打印
项目依赖:
记得换layui和jquery引入路径
完整项目代码:
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>layui打印title>
<link rel="stylesheet" href="layui/css/layui.css" media="all" />
head>
<body class="layui-bg-gray">
<div style="margin: 20px;">
<div class="layui-card">
<div class="layui-card-body">
<button type="button" class="layui-btn layui-btn-normal"
onclick="print('test')">layui自定义打印表格button>
<button id="printSelect" type="button" class="layui-btn layui-btn-normal">自定义打印button>
div>
div>
<div class="layui-card">
<div class="layui-card-body">
<table class="layui-hide" id="test" lay-filter="test">table>
div>
div>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
</div>
script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
script>
div>
body>
<script src="js/jquery-1.9.1.min.js">script>
<script src="layui/layui.js">script>
<script>
layui.use('table', function() {
var table = layui.table;
table.render({
elem: '#test',
data: [{
"id": "10001",
"username": "张三",
"email": "[email protected]",
"sex": "男",
"city": "广西南宁",
"sign": "海内存知己,天涯若比邻",
"experience": "116",
"ip": "192.168.1.26",
"logins": "666",
"joinTime": "2022-02-22"
}, {
"id": "10002",
"username": "李四",
"email": "[email protected]",
"sex": "男",
"city": "广西贵港",
"sign": "海内存知己,天涯若比邻",
"experience": "12",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22",
}, {
"id": "10003",
"username": "王五",
"email": "[email protected]",
"sex": "男",
"city": "广西玉林",
"sign": "海内存知己,天涯若比邻",
"experience": "65",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}, {
"id": "10004",
"username": "玛卡巴卡",
"email": "[email protected]",
"sex": "女",
"city": "广西百色",
"sign": "海内存知己,天涯若比邻",
"experience": "666",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}, {
"id": "10005",
"username": "老五",
"email": "[email protected]",
"sex": "男",
"city": "广西",
"sign": "海内存知己,天涯若比邻",
"experience": "86",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}, {
"id": "10006",
"username": "老六",
"email": "[email protected]",
"sex": "男",
"city": "广西",
"sign": "海内存知己,天涯若比邻",
"experience": "12",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}, {
"id": "10007",
"username": "老七",
"email": "[email protected]",
"sex": "男",
"city": "广西",
"sign": "海内存知己,天涯若比邻",
"experience": "16",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}, {
"id": "10008",
"username": "老八",
"email": "[email protected]",
"sex": "男",
"city": "广西",
"sign": "海内存知己,天涯若比邻",
"experience": "106",
"ip": "192.168.1.26",
"logins": "106",
"joinTime": "2022-02-22"
}],
toolbar: '#toolbarDemo',
title: '用户数据表',
totalRow: true,
cols: [
[{
type: 'checkbox',
fixed: 'left'
}, {
field: 'id',
title: 'ID',
width: 80,
fixed: 'left',
unresize: true,
sort: true,
totalRowText: '合计'
}, {
field: 'username',
title: '用户名',
width: 120,
edit: 'text'
}, {
field: 'email',
title: '邮箱',
width: 150,
edit: 'text',
templet: function(res) {
return '' + res.email + ''
}
}, {
field: 'experience',
title: '积分',
width: 80,
sort: true,
totalRow: true
}, {
field: 'sex',
title: '性别',
width: 80,
edit: 'text',
sort: true
}, {
field: 'logins',
title: '登入次数',
width: 110,
sort: true,
totalRow: true
}, {
field: 'sign',
title: '签名'
}, {
field: 'city',
title: '城市',
width: 100
}, {
field: 'ip',
title: 'IP',
width: 120
}, {
field: 'joinTime',
title: '加入时间',
width: 120
}, {
fixed: 'right',
title: '操作',
toolbar: '#barDemo',
width: 150
}]
],
page: true
});
table.on('toolbar(test)', function(obj) {
var checkStatus = table.checkStatus(obj.config.id);
switch (obj.event) {
case 'getCheckData':
var data = checkStatus.data;
layer.alert(JSON.stringify(data));
break;
case 'getCheckLength':
var data = checkStatus.data;
layer.msg('选中了:' + data.length + ' 个');
break;
case 'isAll':
layer.msg(checkStatus.isAll ? '全选' : '未全选')
break;
};
});
});
function print(tablelayid) {
var v = document.createElement("div");
var f = ["", "", ""
].join("");
$(v).append($(".layui-table-box").find(".layui-table-header").html());
$(v).find("tr").after($("[lay-id=\"" + tablelayid + "\"] .layui-table-body.layui-table-main table").html());
$(v).find("th.layui-table-patch").remove();
$(v).find(".layui-table-col-special").remove();
var h = window.open("打印窗口", "_blank");
h.document.write(f + $(v).prop("outerHTML"));
h.document.close();
h.print();
h.close();
}
$('#printSelect').click(function() {
layui.use(['table', 'layer'], function() {
var table = layui.table,
layer = layui.layer;
var checkStatus = table.checkStatus('test')
console.log(checkStatus)
if (checkStatus.data.length === 0) {
layer.msg('请选择数据', {
icon: 2
})
} else {
printSelect(checkStatus.data)
}
})
})
function printSelect(allData) {
var v = document.createElement("div");
var f = ["", "", ""
].join("");
var contentHtml = "";
for (let i = 0; i < allData.length; i++) {
var data = allData[i]
var template =
""
+
"ID:" + data.id + "
" +
"用户名:" + data.username + "
" +
"邮箱:" + data.email + "
" +
"积分:" + data.experience + "
" +
"性别:" + data.sex + "
" +
"登录次数:" + data.logins + "
" +
"签名:" + data.sign + "
" +
"城市:" + data.city + "
" +
"IP:" + data.ip + "
" +
"加入时间:" + data.joinTime + "
" +
" "
contentHtml += template;
}
$(v).append(contentHtml)
var h = window.open("打印窗口", "_blank");
h.document.write(f + $(v).prop("outerHTML"));
h.print();
h.close();
}
script>
html>