layui table拼接打印行内容时,如何直接打印,不显示打印预览?
如下我拼接后调用打印,会先弹出打印预览窗口,然后点击打印才执行打印,如何取消预览呢?
```php
<script>
layui.use('table', function(){
var table = layui.table;
var $ = layui.jquery;
var tableFilter = layui.tableFilter;
var tableIns = table.render({
elem: '#test'
,url: './score_data.php' //数据接口,json格式
,toolbar:'#toolbarDemo'
,defaultToolbar: ['filter', 'print']
,loading:true
,height:'full-20'
,skin:''
,size:''
,page: true //开启分页
,cols: [[ //表头
{field: 'id', title: 'ID', width:'5%', sort: true, fixed: 'left'}
,{field: 'name', title: '姓名', width:'8%'}
,{field: 'datetime', title: '日期', width:'10%'}
,{field: 'score', title: '分数', width:'10%'}
,{field: 'print', title: '操作', width:'10%'}
]]
,where:{
}
, page: true
, limits: [50,100,150,200,250,300,350,400,450,500]
, limit: 50 //每页默认显示的数量
});
table.on('tool(test)', function (obj) {
var data = obj.data;
//console.log(data)
if (obj.event === 'print' ) {
//用于包含内容
var v = document.createElement("divarea");
//页面头部,导入css ,media="print"表示打印时使用该样式
var f = ["<head>", "<style>", "div{font-size:12px;}", ".main{width:50%;height:20%}",
".main div{width:300px;margin-right: 50px;display:inline-block;}", "</style>", "</head>"
].join("");
var contentHtml = "";
var template =
"<div class='main'>" +
"<div>PN:" + data.name +"</div><br>" +
"<div>ANA:" + data.datetime+ "</div><br>" +
"<div>WH:" + "<?php echo $_SESSION['name']; ?>" + "</div><br>" +
"<div>Date:"+ data.score+ "</div><br>" +
"</div>"
contentHtml += template;
//contentHtml为已拼凑好的内容
$(v).append(contentHtml)
//新建窗口
var h = window.open("打印窗口", "_blank",'width=600,height=400,top=260px,left=380px');
//写入拼凑内容
h.document.write(f + $(v).prop("outerHTML"));
//调用打印
h.print();
//关闭页面
h.close();
}
```