vue vxe-table 實現財務記賬憑證和列印功能

可不简单發表於2024-12-08

使用 vxe-table 實現財務記賬憑證和列印功能非常簡單,實現線上實時編輯的記賬憑證、自動合計金額等

官網:https://vxetable.cn/

<template>
  <div>
    <vxe-grid ref="gridRef" v-bind="gridOptions" @edit-closed="editClosedEvent">
      <template #toolbarButtons>
        <vxe-button status="primary" @click="addEvent">新增</vxe-button>
        <vxe-button status="success" @click="saveEvent">儲存</vxe-button>
      </template>
    </vxe-grid>

    <div ref="bottomElemRef" style="display: flex;padding: 8px;">
      <div style="width: 25%;">財務主管:小徐</div>
      <div style="width: 25%;">記賬:張三</div>
      <div style="width: 25%;">出納:李四</div>
      <div style="width: 25%;">稽核:老六</div>
    </div>
  </div>
</template>

<script>
import { VxeUI } from 'vxe-table'
import XEUtils from 'xe-utils'

export default {
  data () {
    const footerRow = {
      seq: '合計',
      debtorObj: {},
      creditObj: {}
    }

    const gridOptions = {
      border: true,
      showOverflow: true,
      showFooter: true,
      keepSource: true,
      height: 600,
      printConfig: {
        beforePrintMethod: ({ html }) => {
          const topEl = this.$refs.topElemRef
          const topHtml = topEl ? topEl.outerHTML : ''
          const bottomEl = this.$refs.bottomElemRef
          const bottomHtml = bottomEl ? bottomEl.outerHTML : ''
          return `${topHtml}${html}${bottomHtml}`
        }
      },
      exportConfig: {},
      columnConfig: {
        resizable: true
      },
      toolbarConfig: {
        export: true,
        print: true,
        slots: {
          buttons: 'toolbarButtons'
        }
      },
      mouseConfig: {
        area: true,
        extension: false
      },
      editConfig: {
        mode: 'cell',
        trigger: 'dblclick',
        showStatus: true
      },
      keyboardConfig: {
        isClip: true,
        arrowCursorLock: true,
        isArrow: true,
        isShift: true,
        isTab: true,
        isEnter: true,
        isEdit: true,
        isBack: true,
        isDel: true,
        isEsc: true,
        isFNR: true // 是否開啟查詢與替換
      },
      data: [],
      footerData: [
        footerRow
      ],
      columns: [
        { field: 'seq', type: 'seq', width: 60 },
        { field: 'summary', title: '摘要', minWidth: 120, editRender: { name: 'VxeInput' } },
        { field: 'subject', title: '會計科目', minWidth: 180, editRender: { name: 'VxeInput' } },
        {
          field: 'debtorAmount',
          title: '借方金額',
          children: [
            { field: 'debtorObj.p9', title: '億', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p8', title: '千', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p7', title: '百', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p6', title: '十', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p5', title: '萬', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p4', title: '千', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p3', title: '百', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p2', title: '十', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.p1', title: '元', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.m1', title: '角', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'debtorObj.m2', title: '分', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } }
          ]
        },
        { field: 'x1', title: '√', width: 40 },
        {
          field: 'creditAmount',
          title: '貸方金額',
          children: [
            { field: 'creditObj.p9', title: '億', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p8', title: '千', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p7', title: '百', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p6', title: '十', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p5', title: '萬', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p4', title: '千', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p3', title: '百', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p2', title: '十', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.p1', title: '元', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.m1', title: '角', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } },
            { field: 'creditObj.m2', title: '分', width: 60, align: 'center', editRender: { name: 'VxeNumberInput', props: { type: 'integer', max: 9, controls: false, maxLength: 1, align: 'center' } } }
          ]
        },
        { field: 'x2', title: '√', width: 40 }
      ]
    }

    return {
      gridOptions,
      footerRow
    }
  },
  methods: {
    handleSpitAmount (amount, isUnit) {
      const str = XEUtils.toValueString(XEUtils.toNumber(amount) || '')
      const [pStr, mStr] = `${isUnit ? '¥' : ''}${str}`.split('.')
      const restObj = {
        p9: '',
        p8: '',
        p7: '',
        p6: '',
        p5: '',
        p4: '',
        p3: '',
        p2: '',
        p1: '',
        m1: '',
        m2: ''
      }
      if (pStr) {
        pStr.split('').reverse().forEach((val, i) => {
          restObj[`p${i + 1}`] = `${val || 0}`
        })
      }
      if (mStr) {
        mStr.split('').forEach((val, i) => {
          restObj[`m${i + 1}`] = `${val || 0}`
        })
      }
      return restObj
    },
    handleJoinAmount (obj) {
      const strArr = [obj.p9 || 0, obj.p8 || 0, obj.p7 || 0, obj.p6 || 0, obj.p5 || 0, obj.p4 || 0, obj.p3 || 0, obj.p2 || 0, obj.p1 || 0, '.', obj.m2 || 0, obj.m1 || 0]
      return XEUtils.toNumber(strArr.join(''))
    },
    handleData (list) {
      return list.map(item => {
        return {
          ...item,
          debtorObj: this.handleSpitAmount(item.debtorAmount, false),
          creditObj: this.handleSpitAmount(item.creditAmount, false)
        }
      })
    },
    loadList (list) {
      this.gridOptions.data = this.handleData(list)
      this.$nextTick(() => {
        this.updateFooter()
      })
    },
    updateFooter () {
      this.$nextTick(() => {
        const $grid = this.$refs.gridRef
        if ($grid) {
          const { visibleData } = $grid.getTableData()
          let countDebtorAmount = 0
          let countCreditAmount = 0
          visibleData.forEach(row => {
            row.debtorAmount = this.handleJoinAmount(row.debtorObj)
            row.creditAmount = this.handleJoinAmount(row.creditObj)
            countDebtorAmount += row.debtorAmount
            countCreditAmount += row.creditAmount
          })
          this.footerRow.debtorObj = this.handleSpitAmount(countDebtorAmount, true)
          this.footerRow.creditObj = this.handleSpitAmount(countCreditAmount, true)
        }
      })
    },
    async addEvent () {
      const $grid = this.$refs.gridRef
      if ($grid) {
        const record = {}
        await $grid.insertAt(record, -1)
        this.updateFooter()
      }
    },
    saveEvent () {
      const $grid = this.$refs.gridRef
      if ($grid) {
        const { visibleData } = $grid.getTableData()
        const rest = visibleData.map(item => {
          return {
            id: item.id,
            summary: item.summary,
            subject: item.subject,
            debtorAmount: this.handleJoinAmount(item.debtorObj),
            creditAmount: this.handleJoinAmount(item.creditObj)
          }
        })
        VxeUI.modal.message({
          content: '儲存成功',
          status: 'success'
        })
        this.loadList(rest)
        console.log(rest)
      }
    },
    editClosedEvent () {
      this.updateFooter()
    }
  },
  created () {
    const list = [
      { id: 10001, summary: '購買辦公用品', subject: '辦公費', debtorAmount: 120000, creditAmount: 0 },
      { id: 10002, summary: '購買辦公用品', subject: '庫存現金', debtorAmount: 0, creditAmount: 120000 }
    ]
    this.loadList(list)
  }
}
</script>

https://gitee.com/x-extends/vxe-table

相關文章