实现el-table打印功能,样式对齐,去除滚动条

实现el-table打印功能,样式对齐,去除滚动条

复制代码
// 整个页面打印
function printTable(id) {
  // let domId = '#js_index'
  // if (id) {
  //   domId = `#${ id }`;
  // }
  // let wpt = document.querySelector(domId);
  // let newContent = wpt.innerHTML;
  // let oldContent = document.body.innerHTML;
  // document.body.innerHTML = newContent;
  // window.print(); //打印方法
  // history.go(0);
  // document.body.innerHTML = oldContent;

  if (id) {
    // 获取打印DOM
    let el = document.getElementById(id);
    // 当前页面样式
    let headDom = document.getElementsByTagName("head")[0];
    // 创建iframe
    let iframe = document.createElement("iframe");
    // 设置iframe样式
    iframe.setAttribute("id", id);
    iframe.setAttribute(
      "style",
      "position:absolute;width:0px;height:0px;left:-500px;top:-500px;"
    );
    // 在页面插入iframe
    document.body.appendChild(iframe);
    // 获取iframe内的html
    let doc = iframe.contentWindow.document;

    // 经需要打印的DOM插入iframe
    let printMain = document.createElement("div");
    printMain.setAttribute("id", id);
    printMain.innerHTML = el.innerHTML;
    doc.body.appendChild(printMain);
    // 设置iframe内的header,添加样式文件
    setTimeout(() => {
      doc.getElementsByTagName("head")[0].innerHTML = headDom.innerHTML;

      const table = doc.querySelectorAll('.el-table__header,.el-table__body');
      const table1 = doc.querySelectorAll('.el-table');
      const table2 = doc.querySelectorAll('.el-table__body-wrapper');
      for (let i = 0; i < table2.length; i++) {
        const tableItem = table2[i];
        console.log(tableItem);
        tableItem.style.maxHeight = 'unset';
      }
      for (let i = 0; i < table1.length; i++) {
        const tableItem = table1[i];
        console.log(tableItem);
        tableItem.style.maxHeight = 'unset';
      }
      setTimeout(() => {
        //el-table 打印不全的问题
        for (let i = 0; i < table.length; i++) {
          const tableItem = table[i];
          console.log(table1);
          tableItem.style.width = '100%';
          const child = tableItem.childNodes;
          for (let j = 0; j < child.length; j++) {
            const element = child[j];
            if (element.localName == 'colgroup') {
              element.innerHTML = '';
            }
          }
        }
        setTimeout(function () {
          iframe.contentWindow.focus();
          iframe.contentWindow.print();
          // 关闭iframe
          doc.close();
          document.body.removeChild(iframe);
        }, 1000);
      }, 1000)
    }, 0)
  } else {
    window.print(); //打印方法
  }
}
相关推荐
开开心心就好4 天前
近200个工具的电脑故障修复合集
安全·智能手机·pdf·电脑·consul·memcache·1024程序员节
数据皮皮侠AI6 天前
中国城市可再生能源数据集(2005-2021)|顶刊 Sci Data 11 种能源面板
大数据·人工智能·笔记·能源·1024程序员节
计算机毕业论文辅导8 天前
物联网实战:基于MQTT协议的智能家居数据传输系统设计与实现
1024程序员节
开开心心就好9 天前
支持批量处理的视频分割工具推荐
安全·智能手机·rust·pdf·电脑·1024程序员节·lavarel
liuyao_xianhui11 天前
Linux开发工具结尾 _make
linux·运维·服务器·数据结构·哈希算法·宽度优先·1024程序员节
学传打活13 天前
【边打字.边学昆仑正义文化】_21_爱的结晶(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
数据皮皮侠AI20 天前
顶刊同款!中国地级市风灾风险与损失数据集(2000-2022)|灾害 / 环境 / 经济研究必备
大数据·人工智能·笔记·能源·1024程序员节
Fab1an21 天前
Busqueda——Hack The Box 靶机
linux·服务器·学习·1024程序员节
技术专家21 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
学传打活24 天前
古代汉语是源,现代汉语是流,源与流一脉相承。
微信公众平台·1024程序员节·汉字·中华文化