vue打印print

注意, iframe打开后须close

  //判断iframe是否存在,不存在则创建iframe
  let iframe=document.getElementById("iframe");
  if(!iframe){
      iframe = document.createElement('IFRAME');
      let doc = null;
      iframe.setAttribute("id", "iframe");
      iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
      document.body.appendChild(iframe);
      doc = iframe.contentWindow.document;
      // 这里可以自定义样式
      //doc.write("<LINK rel="stylesheet" type="text/css" href="css/print.css">");
      // 获取数据,写入table
      let totalPeople = this.$refs.tableRef.echartsData;
      doc.write('<h4>人员统计</h4>')
      doc.write('<table border="1" cellpadding="0" cellspacing="0">')
      doc.write('<tr><td>人员分类</td><td>总数</td></tr>')
      for(let i in totalPeople){
          let s = '<tr><td>' + totalPeople[i].name + "</td><td>" + totalPeople[i].value + '</td></tr>'
          doc.write(s);
      }
      doc.write("</table>")
      doc.close();
      iframe.contentWindow.focus();
  }
  iframe.contentWindow.print();
  if (navigator.userAgent.indexOf("MSIE") > 0){
      document.body.removeChild(iframe);
  }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值