vue vxe-table 实现财务记账凭证和打印功能

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

posted @ 2024-12-08 16:29  可不简单  阅读(29)  评论(0编辑  收藏  举报