vxe-table 列印出貨單、自定義列印單據

abc26296發表於2024-11-21

在開發 ERP 或內部系統時,經常會遇到需求列印出貨單,接下來分享如何在 vxe-table 靈活的使用列印功能,非常簡單就能實現能自定義的出貨單列印。

安裝

npm install vxe-pc-ui@4.3.2 vxe-table@4.9.3
// ...
import VxeUI from 'vxe-pc-ui'
import 'vxe-pc-ui/lib/style.css'
import VxeUITable from 'vxe-table'
import 'vxe-table/lib/style.css'
// ...

createApp(App).use(VxeUI).use(VxeUITable).mount('#app')
// ...

頁面效果

頁面上的列印區用藍色邊框標註了,方便看出效果

{AF41EDFB-33D2-4158-A6A5-CA23DDE6A54B}.png

點選列印後,完整的將指定的區域列印出來,也可以新增自定義內容,非常靈活。
不僅支援去掉頁頭、頁尾,自定義標題、分頁等等,靈活自定義程度非常高

<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>

相關文章