From aaa2a6e5f9ad222383fe5b694ee925e4213fdb73 Mon Sep 17 00:00:00 2001 From: huayj Date: Mon, 26 Aug 2019 10:57:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A7=E5=B8=81=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TableCell.js | 24 +++++++++++++++++++++++- src/lib/utils.js | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/TableCell.js b/src/TableCell.js index 3d43a66..d9bc5a1 100644 --- a/src/TableCell.js +++ b/src/TableCell.js @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import objectPath from 'object-path'; import i18n from './lib/i18n'; import { getComponentLocale } from 'bee-locale/build/tool'; +import { formatMoney } from './lib/utils'; const propTypes = { record: PropTypes.object, clsPrefix: PropTypes.string, @@ -70,7 +71,8 @@ class TableCell extends Component{ return data; } - renderBoolType = ( data, record, index, config ) => { + // 渲染布尔类型 + renderBoolType = ( data, record, index, config={} ) => { let locale = getComponentLocale(this.props, this.context, 'Table', () => i18n); let boolConfig = {...{ trueText: locale['bool_true'], falseText: locale['bool_false']},...config}; if(typeof data === 'string'){ @@ -84,6 +86,22 @@ class TableCell extends Component{ return boolConfig.trueText; } + // 渲染货币类型 + renderCurrency = (data, record, index, config={}) => { + let number = formatMoney(data, config.precision || 2, config.thousand || true); + if(config.makeUp === false && number !== '0') { + number = number.replace(/0*$/,'').replace(/\.$/,''); + } + let res = {number}; + let pre = config.preSymbol ? {config.preSymbol} : null; + let next = config.nextSymbol ? {config.nextSymbol} : null; + return + {pre} + {res} + {next} + ; + } + render() { const { record, indentSize, clsPrefix, indent, index, expandIcon, column ,fixed,showSum, bodyDisplayInRow,lazyStartIndex,lazyEndIndex} = this.props; @@ -116,6 +134,10 @@ class TableCell extends Component{ text = this.renderBoolType(text, record, index, column.boolConfig); break; } + case 'currency':{ + text = this.renderCurrency(text, record, indent, column.currencyConfig); + break; + } default : { break; } diff --git a/src/lib/utils.js b/src/lib/utils.js index 74b7428..1ad0ba1 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -293,6 +293,23 @@ export function checkDicimalInvalid(value, precision) { return result; }; + +/** + * 将数值转化为货币类型 + * @param {*} number 数值 + * @param {*} places 精度 + * @param {*} thousand 是否展示千分位 + */ +export function formatMoney(number, places, thousand) { + number = number || 0; + places = !isNaN(places = Math.abs(places)) ? places : 2; + let thousandSymbol = thousand ? "," : ''; + let negative = number < 0 ? "-" : ""; + let i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + ""; + let j = (j = i.length) > 3 ? j % 3 : 0; + return negative + (j ? i.substr(0, j) + thousandSymbol : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousandSymbol) + (places ? '.' + Math.abs(number - i).toFixed(places).slice(2) : ""); +} + export const Event = { addHandler, removeHandler,