table.js 3.88 KB
// pages/components/table/table.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    table: {
      type: Array,
      value: []
    },
    tableObj: {
      type: Array,
      value: []
    },
    tableFlag: {
      type: Boolean,
      value: true
    },
    tableWidth: {
      type: String,
      value: '100%'
    },
    headerConfig: {
      type: Object,
      value: {}
    },
    itemTableHeight: {
      type: String,
      value: '0rpx'
    },
    itemHeight: {
      type: String,
      value: '72rpx'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // tableWidth: 1088,
    // tableFlag: true,
    table: [],
    formattedTable: []
    // tableObj: [
    //   {
    //     name: '贴膜',
    //     unit: '/台',
    //     key: 'filmTimes',
    //     width: 'var(--fontSize110)'
    //   },
    //   {
    //     name: '核销',
    //     unit: '/张',
    //     key: 'usedTotal',
    //     width: 'var(--fontSize110)'
    //   },
    //   {
    //     name: '体验',
    //     unit: '/位',
    //     key: 'experienceTotal',
    //     width: 'var(--fontSize110)'
    //   },
    //   {
    //     name: '转化',
    //     unit: '/单',
    //     key: 'upgradeTotal',
    //     width: 'var(--fontSize110)'
    //   },
    // ]
  },
  observers: {
    'table, tableObj': function (table, tableObj) {
      this.formatTable(table, tableObj)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatTable(table, tableObj) {
      // 通用格式化:为空返回"0.00",数值保留两位小数,并加千分位逗号
      // 特例:当 headerConfig.key 为 'staffName' 时不处理数值格式化
      if (this.data.headerConfig.key == 'staffName') {
        this.setData({ formattedTable: table })
        return
      }

      const formatNumber = val => {
        if (val === null || val === undefined || val === '') return '0.00'
        const raw = typeof val === 'string' ? val.replace(/,/g, '').trim() : val
        const num = Number(raw)
        if (isNaN(num)) return '0.00'
        const fixed = (Math.abs(num) === 0 ? 0 : num).toFixed(2)
        const parts = fixed.split('.')
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
        return parts.join('.')
      }

      const keys = (tableObj || []).map(i => i && i.key).filter(Boolean)
      const src = Array.isArray(table) ? table : []
      const formatted = src.map(row => {
        const copy = { ...row }
        keys.forEach(k => {
          copy[k] = formatNumber(copy[k])
        })
        return copy
      })
      this.setData({ formattedTable: formatted })
    },
    onTableClick(e) {
      // let { item } = e.currentTarget.dataset
      let { headerConfig } = this.data
      console.log(headerConfig, 'headerConfig')
      if (headerConfig.key == 'merchantName') {
        let { item } = e.currentTarget.dataset
        wx.navigateTo({
          url: `/packageCustomer/pages/merchantDetail/merchantDetail?query=${encodeURIComponent(
            JSON.stringify(item)
          )}`
        })
      }
      if (headerConfig.key == 'staffName') {
        let { item } = e.currentTarget.dataset
        wx.navigateTo({
          url: `/packageIndex/pages/staff/staff?query=${encodeURIComponent(
            JSON.stringify(item)
          )}`
        })
      }
    },
    scrollonReachBottom() {
      // console.log('scrollonReachBottom')
      // let { pages, pageNum } = this.data
      // // 判断当前页是否是末页
      // if (pages == pageNum) return wx.$util.showToast('列表已到底')
      // this.setData({
      //   pageNum: pageNum + 1
      // })
      // this.getMerchantCouponList()
    },
    restPageNum() {
      // this.setData({
      //   pageNum: 1
      // })
      // this.getMerchantCouponList()
    }
  }
})