Commit 65094dab by 张立顺

fix bug

1 parent a326e388
// packageIndex/pages/directorLeaders/directorLeaders.js
import IndexApi from '~/api/index/index'
// 通用数字格式化:两位小数 + 千分位;无效值返回 "0.00"
function formatNumber(value) {
const num = Number(value)
if (!isFinite(num)) return '0.00'
const fixed = num.toFixed(2)
const parts = fixed.split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
Component({
/**
* 组件的属性列表
......@@ -12,7 +22,7 @@ Component({
data: {
switchName: {
left: '当月',
right: '全部'
right: '全年'
},
rankingSelectIndex: 1,
overviewSwitchValue: 0,
......@@ -398,10 +408,9 @@ Component({
},
// 总览合作模式
cooperationMode(values, items, selectType) {
let { cooperationMode } = this.data
cooperationMode = values[0]
console.log(cooperationMode, 'cooperationMode');
console.log(cooperationMode, 'cooperationMode')
this.setData({
[selectType]: cooperationMode,
[selectType + 'label']: items[0].name
......@@ -468,8 +477,10 @@ Component({
}).then(res => {
// dataOverview
console.log(res, 'outputRanking')
const overview = Object.assign({}, res.data || {})
overview.outputValueFormatted = formatNumber(overview.outputValue)
this.setData({
dataOverview: res.data
dataOverview: overview
})
})
},
......
......@@ -42,7 +42,7 @@
<view class="statistics">
<view class="item total">
<view class="title">总产值</view>
<view class="num">¥ {{dataOverview.outputValue}}</view>
<view class="num">¥ {{dataOverview.outputValueFormatted}}</view>
</view>
<view class="item">
<view class="title">出单门店</view>
......
......@@ -40,12 +40,14 @@ Component({
{
name: '已完成',
key: 'completed',
width: 'var(--fontSize134)'
width: 'var(--fontSize134)',
beforeUnit: '¥'
},
{
name: '当月目标',
key: 'target',
width: 'var(--fontSize134)'
width: 'var(--fontSize134)',
beforeUnit: '¥'
},
{
name: '完成率',
......
......@@ -80,25 +80,30 @@ Component({
*/
methods: {
formatTable(table, tableObj) {
// 区分一下,headerConfig.key == 'staffName' 不处理
// 通用格式化:为空返回"0.00",数值保留两位小数,并加千分位逗号
// 特例:当 headerConfig.key 为 'staffName' 时不处理数值格式化
if (this.data.headerConfig.key == 'staffName') {
this.setData({
formattedTable: table
})
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 => {
const v = copy[k]
if (v === null || v === undefined || v === '') {
copy[k] = '0.00'
return
}
const num = Number(v)
copy[k] = isNaN(num) ? '0.00' : num.toFixed(2)
copy[k] = formatNumber(copy[k])
})
return copy
})
......
......@@ -61,7 +61,7 @@
>{{item[i.key] || 0}}</view>
</block>
<block wx:elif="{{ i.key == 'upgradeRate' }}">{{item[i.key] || 0}}%</block> -->
<block>{{item[i.key] || 0}}{{i.unit}}</block>
<block>{{i.beforeUnit}}{{item[i.key] || 0}}{{i.unit}}</block>
</view>
</block>
</view>
......
......@@ -176,18 +176,30 @@ Component({
}
},
formatTable(table, tableObj) {
// 通用格式化:为空返回"0.00",数值保留两位小数,并加千分位逗号
// 特例:当 headerConfig.key 为 'staffName' 时不处理数值格式化
if (this.data.headerConfig && 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 => {
const v = copy[k]
if (v === null || v === undefined || v === '') {
copy[k] = '0.00'
return
}
const num = Number(v)
copy[k] = isNaN(num) ? '0.00' : num.toFixed(2)
copy[k] = formatNumber(copy[k])
})
return copy
})
......
......@@ -51,7 +51,7 @@
>{{item[i.key] || 0}}</view>
</block>
<block wx:elif="{{ i.key == 'upgradeRate' }}">{{item[i.key] || 0}}%</block> -->
<block>{{item[i.key] || 0}}{{i.unit}}</block>
<block>{{i.unit?'':'¥'}}{{item[i.key] || 0}}{{i.unit}}</block>
</view>
</block>
</view>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!