table.js 3.49 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: []
  },
  observers: {
    'table, tableObj': function (table, tableObj) {
      this.formatTable(table, tableObj)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    navToRegionalLeaders(e) {
      let { headerConfig } = this.data
      //  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 == 'regionName') {
        let { item } = e.currentTarget.dataset
        wx.navigateTo({
          url: `/packageIndex/pages/regionalLeaders/regionalLeaders?query=${encodeURIComponent(
            JSON.stringify(item)
          )}`
        })
      }
    },
    formatTable(table, tableObj) {
      // 通用格式化:为空返回"0.00",数值保留两位小数,并加千分位逗号
      // 特例:当 headerConfig.key 为 'staffName' 时不处理数值格式化
      // if (
      //   this.data.headerConfig &&
      //   (this.data.headerConfig.key === 'staffName' ||
      //     this.data.headerConfig.key === 'personnelName')
      // ) {
      //   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 || [])
        .filter(i => i && (i.unit || i.beforeUnit))
        .map(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 })
    },
    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()
    }
  }
})