当前位置:首页 > 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 控制只打印特定的区域,隐藏其他内容。

@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实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…