table.js
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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()
}
}
})