vxe-table 打印出货单、自定义打印单据

在开发 ERP 或内部系统时,经常会遇到需求打印出货单,接下来分享如何在 vxe-table 灵活的使用打印功能,非常简单就能实现能自定义的出货单打印。

页面效果

页面上的打印区用蓝色边框标注了,方便看出效果

点击打印后,完整的将指定的区域打印出来,也可以添加自定义内容,非常灵活。

不仅支持去掉页头、页尾,自定义标题、分页等等,灵活自定义程度非常高

html 复制代码
<template>
  <div>
    <vxe-button @click="printEvent">点击打印</vxe-button>

    <div style="border: 1px solid #409eff; padding: 16px">
      <div ref="topElRef">
        <div style="margin-bottom: 8px;">
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">商品名称:Vxe 企业授权</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">发货单号:X2665847132654</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">发货日期:2024-12-01</div>
          </div>
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">收货姓名:小徐</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">收货地址:火星第七区18号001</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">联系电话:10086</div>
          </div>
        </div>
      </div>

      <vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>

      <div ref="bottomElRef">
        <div style="margin-top: 30px;text-align: right;">
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;"></div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">创建人:小徐</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">创建日期:2024-12-01</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { VxeUI } from 'vxe-table'

const gridRef = ref()
const topElRef = ref()
const bottomElRef = ref()

const gridOptions = reactive({
  border: true,
  columns: [
    { type: 'seq', width: 60 },
    { field: 'name', title: '名称' },
    { field: 'code', title: '编码' },
    { field: 'num', title: '数量' },
    { field: 'price', title: '价格' },
    { field: 'taxRate', title: '税率' }
  ],
  data: [
    { id: 10001, name: '筛选插件', code: 'T1', price: 1200, num: 1, taxRate: '3%' },
    { id: 10002, name: '区域选取插件', code: 'T2', price: 16840, num: 3, taxRate: '3%' },
    { id: 10003, name: '单元格选取插件', code: 'T3', price: 8460, num: 3, taxRate: '3%' },
    { id: 10004, name: 'Excel 筛选插件', code: 'T4', price: 980, num: 2, taxRate: '3%' },
    { id: 10005, name: '零代码平台模板', code: 'T5', price: 23199, num: 5, taxRate: '3%' },
    { id: 10006, name: '后台管理系统模板', code: 'T6', price: 899, num: 7, taxRate: '3%' },
    { id: 10007, name: '低代码设计器插件', code: 'T7', price: 1688, num: 4, taxRate: '3%' },
    { id: 10008, name: '可视化拖拽插件', code: 'T8', price: 1299, num: 4, taxRate: '3%' }
  ]
})

const printEvent = async () => {
  const $grid = gridRef.value
  if ($grid) {
    const printRest = await $grid.getPrintHtml()
    const topEl = topElRef.value
    const bottomEl = bottomElRef.value
    const topHtml = topEl ? topEl.innerHTML : ''
    const bottomHtml = bottomEl ? bottomEl.innerHTML : ''
    VxeUI.print({
      title: '出货单据',
      pageBreaks: [
        // 第一页
        {
          bodyHtml: topHtml + printRest.html + bottomHtml
        }
      ]
    })
  }
}
</script>

https://gitee.com/xuliangzhan/vxe-table

相关推荐
仰望.2 天前
vue 甘特图 vxe-gantt table 拖拽任务调整开始日期和结束日期的使用,拖拽任务调整日期
vue.js·甘特图·vxe-ui
大猩猩X11 天前
vxe-gantt vue table 甘特图子任务多层级自定义模板用法
vue.js·甘特图·vxe-table·vxe-ui
大猩猩X12 天前
vue vxe-gantt table 甘特图实现多个维度视图展示,支持切换年视图、月视图、周视图等
前端·javascript·甘特图·vxe-table·vxe-ui
大猩猩X12 天前
vue vxe-gantt 甘特图实现产品进度列表,自定义任务条样式和提示信息
前端·javascript·甘特图·vxe-ui·vxe-gantt
chenhdowue13 天前
如何使用 vxe-table 导出为带图片的单元格到 excel 格式文件
vue.js·excel·vxe-table·vxe-ui
大猩猩X16 天前
vxe-gantt 甘特图使用右键菜单
vue.js·vxe-table·vxe-ui·vxe-gantt
仰望.16 天前
如何实现 vxe-gantt 甘特图编辑左侧表格数据自动更新右侧进度视图
前端·vue.js·甘特图·vxe-ui·vxe-gantt
chenhdowue17 天前
vxe-table 数据校验的2种提示方式
vue.js·vxe-table·vxe-ui
仰望.2 个月前
vue 下拉框 vxe-select 实现人员选择下拉列表
前端·javascript·vue.js·vxe-ui
仰望.5 个月前
vxe-table 通过配置 ajax 方式自动请求数据,适用于简单场景的列表
vue.js·ajax·vxe-table·vxe-ui