首页 > 其他分享 >用print-js实现打印功能时element table组件打印不全的问题

用print-js实现打印功能时element table组件打印不全的问题

时间:2023-05-09 22:00:43浏览次数:30  
标签:function inputs dom 打印 element var print document

插件下载:

npm install --save print-js

解决方案,修改print.js里面的 getHtml

// 打印类属性、方法定义 /* eslint-disable */ const Print = function (dom, options) {     if (!(this instanceof Print)) return new Print(dom, options);
    this.options = this.extend({         'noPrint': '.no-print'     }, options);
    if ((typeof dom) === "string") {         this.dom = document.querySelector(dom);     } else {         this.isDOM(dom)         this.dom = this.isDOM(dom) ? dom : dom.$el;     }
    this.init(); }; Print.prototype = {     init: function () {         var content = this.getStyle() + this.getHtml();         this.writeIframe(content);     },     extend: function (obj, obj2) {         for (var k in obj2) {             obj[k] = obj2[k];         }         return obj;     },
    getStyle: function () {         var str = "",             styles = document.querySelectorAll('style,link');         for (var i = 0; i < styles.length; i++) {             str += styles[i].outerHTML;         }         str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";         str += "<style>html,body,div{height: auto!important;font-size:14px}</style>";         return str;     },
    getHtml: function () {         var inputs = document.querySelectorAll('input');         var textareas = document.querySelectorAll('textarea');         var selects = document.querySelectorAll('select');         let cells = document.querySelectorAll('.cell');         for (var k = 0; k < inputs.length; k++) {             if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {                 if (inputs[k].checked == true) {                     inputs[k].setAttribute('checked', "checked")                 } else {                     inputs[k].removeAttribute('checked')                 }             } else if (inputs[k].type == "text") {                 inputs[k].setAttribute('value', inputs[k].value)             } else {                 inputs[k].setAttribute('value', inputs[k].value)             }         }
        for (var k2 = 0; k2 < textareas.length; k2++) {             if (textareas[k2].type == 'textarea') {                 textareas[k2].innerHTML = textareas[k2].value             }         }
        for (var k3 = 0; k3 < selects.length; k3++) {             if (selects[k3].type == 'select-one') {                 var child = selects[k3].children;                 for (var i in child) {                     if (child[i].tagName == 'OPTION') {                         if (child[i].selected == true) {                             child[i].setAttribute('selected', "selected")                         } else {                             child[i].removeAttribute('selected')                         }                     }                 }             }         }   // 解决table组件打印不全         const tableNode = document.querySelectorAll('.el-table__header, .el-table__body')         for (let k6 = 0; k6 < tableNode.length; k6++) {             const tableItem = tableNode[k6]             tableItem.style.width = '100%'             const child = tableItem.childNodes;             for (let i = 0; i < child.length; i++) {                 const element = child[i];                 if (element.localName === 'colgroup') {                     element.innerHTML = ''                 }             }         }   // table格子超出问题         for (let k7 = 0; k7 < cells.length; k7++) {             const cell = cells[k7]             cell.style.width = '100%'             cell.removeAttribute('style')         }         return this.dom.outerHTML;     },
    writeIframe: function (content) {         var w, doc, iframe = document.createElement('iframe'),             f = document.body.appendChild(iframe);         iframe.id = "myIframe";         //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";         iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');         w = f.contentWindow || f.contentDocument;         doc = f.contentDocument || f.contentWindow.document;         doc.open();         doc.write(content);         doc.close();         var _this = this         iframe.onload = function () {             _this.toPrint(w);             setTimeout(function () {                 document.body.removeChild(iframe)             }, 100)         }     },
    toPrint: function (frameWindow) {         try {             setTimeout(function () {                 frameWindow.focus();                 try {                     if (!frameWindow.document.execCommand('print', false, null)) {                         frameWindow.print();                     }                 } catch (e) {                     frameWindow.print();                 }                 frameWindow.close();             }, 10);         } catch (err) {             console.log('err', err);         }     },     isDOM: (typeof HTMLElement === 'object') ?         function (obj) {             return obj instanceof HTMLElement;         } :         function (obj) {             return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';         } }; const MyPlugin = {} MyPlugin.install = function (Vue, options) {     // 4. 添加实例方法     Vue.prototype.$print = Print } export default MyPlugin

标签:function,inputs,dom,打印,element,var,print,document
From: https://www.cnblogs.com/tiantiantiantian/p/17386428.html

相关文章

  • Web: parentElement parentNode
         ......
  • element-ui上传组件,before-upload发送异步请求 + Promise
    element-ui上传组件,before-upload发送异步请求+Promisebefore-upload为false的时候会阻止图片的上传但是和chenge事情一起不行可以:http-request="fnUploadRequest"<el-upload--snip--:before-upload="beforeAvatarUpload"--snip--......
  • bartender导入EXCEL批量打印
    第一次使用BarTender软件,感觉很NICE,记录一下;1、安装最好选择全部安装,我第一次是最小安装,结果连EXCEL文件时提示要下载“AccessDatabaseEngine"安装后才行,估计与最小安装有关系。2、自定义要打印的纸张大小;3、在EXCEL文件中加入要打印的数据,我只测试了一列,(要有列标题)4、启......
  • Java NullPointerException 没有打印堆栈
    ref  https://www.bbsmax.com/A/obzbgx41JE/ 周五在公司搭好的ELK上查看日志,组长让看看其中NullPointerException出现很多的原因。通过NullPointerException搜索,点看其中一个查看,发现异常的信息就一行java.lang.NullPointerException,并没有堆栈信息。看了几个都没有,然后翻......
  • elementUI 多层结构动态生成el-form及校验
    如题,当整个el-form都是通过多层数据结构循环渲染出来的表单,那么每个el-form-item的prop和el-input、el-select等的v-model也是动态变量填充,怎么样才能内嵌rules校验呢?如下数据结构://form通过initData数据结构进行渲染constinitData=[{channel:'sms',title......
  • el-scrollbar elementui 滚动条
    参考:https://www.jb51.net/article/245065.htmel-scrollbar滚动条,文档中没有<el-scrollbarstyle="height:100%"><!--滚动条要包裹的内容--><divclass="container">占位</div></el-scrollbar> ......
  • 微信小程序实现类似elementUI的Timeline时间线效果,自适应页面与文本
    <viewclass="box"><viewwx:for="{{list}}"wx:key="index"class="one"><viewclass="onedot"></view><viewwx:if="{{index!=list.length-1}}"class="oneline......
  • vue+element输入框校验输入汉字再输入数字看似正常,实则有大问题,保存时数据不对
    在vue+elementUI项目中经常会使用到输入框限制为整数或者小数的需求,一般采用如下oninput="value=value.replace(/[^0-9.]/g,'')"解决,<el-input    :placeholder="请输入整数或者小数"    v-model="inputValue"   oninput="value=value.replace(/[^0-9......
  • scandir,major和minor,state,无锁机制----比较交换CAS Compare And Swap,dirent,sprintf,fop
    文章目录1.Linuxc目录操作函数scandir2.Linux系统设备(device)的major和minornumber3.state4.无锁机制----比较交换CASCompareAndSwap5.dirent6.sprintf7.fopen8.atoi函数9.strtok10.strtol1.Linuxc目录操作函数scandir(1)头文件:#include<dirent.h>定义函数:intscandir(......
  • element中表格的另一种显示方法
    正常(默认展示该数据的相关属性值):      <el-table-column       prop="name"       label="姓名"       width="180"      ></el-table-column>转换法(去掉prop属性,改为用值判断显示内容):     ......