当前位置:首页 > VUE

vue实现表格打印

2026-01-19 14:22:27VUE

Vue 实现表格打印的方法

使用 window.print() 方法

在 Vue 中可以通过调用浏览器的 window.print() 方法实现打印功能。创建一个打印按钮,点击时触发打印事件。

<template>
  <div>
    <button @click="printTable">打印表格</button>
    <table ref="printTableRef">
      <!-- 表格内容 -->
    </table>
  </div>
</template>

<script>
export default {
  methods: {
    printTable() {
      window.print();
    }
  }
};
</script>

<style>
@media print {
  body * {
    visibility: hidden;
  }
  table, table * {
    visibility: visible;
  }
  table {
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>

使用第三方库 vue-print-nb

vue-print-nb 是一个专门为 Vue 设计的打印插件,使用起来更加方便。

安装插件:

npm install vue-print-nb --save

在 Vue 项目中使用:

import Print from 'vue-print-nb'
Vue.use(Print);

模板中使用:

<template>
  <div>
    <button v-print="printObj">打印表格</button>
    <table id="printTable">
      <!-- 表格内容 -->
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      printObj: {
        id: 'printTable',
        popTitle: '表格打印'
      }
    }
  }
};
</script>

自定义打印样式

通过 CSS 的 @media print 可以自定义打印时的样式,隐藏不需要打印的元素。

@media print {
  .no-print {
    display: none;
  }
  .print-table {
    width: 100%;
    border-collapse: collapse;
  }
  .print-table th, .print-table td {
    border: 1px solid #000;
    padding: 8px;
  }
}

使用 iframe 打印

创建一个隐藏的 iframe,将表格内容复制到 iframe 中,然后打印 iframe 的内容。

methods: {
  printUsingIframe() {
    const content = this.$refs.printTable.innerHTML;
    const iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    document.body.appendChild(iframe);
    const frameDoc = iframe.contentDocument || iframe.contentWindow.document;
    frameDoc.open();
    frameDoc.write(`
      <!DOCTYPE html>
      <html>
        <head>
          <title>打印表格</title>
          <style>
            table {
              width: 100%;
              border-collapse: collapse;
            }
            th, td {
              border: 1px solid #000;
              padding: 8px;
            }
          </style>
        </head>
        <body>
          ${content}
        </body>
      </html>
    `);
    frameDoc.close();
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
    document.body.removeChild(iframe);
  }
}

打印特定区域

通过 CSS 控制只打印特定的区域,隐藏其他内容。

vue实现表格打印

@media print {
  body * {
    visibility: hidden;
  }
  .print-area, .print-area * {
    visibility: visible;
  }
  .print-area {
    position: absolute;
    left: 0;
    top: 0;
  }
}
<div class="print-area">
  <!-- 需要打印的表格内容 -->
</div>

标签: 表格vue
分享给朋友:

相关文章

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vue…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…