货币类型

This commit is contained in:
huayj 2019-08-26 10:57:15 +08:00
parent 90e108c735
commit aaa2a6e5f9
2 changed files with 40 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import objectPath from 'object-path'; import objectPath from 'object-path';
import i18n from './lib/i18n'; import i18n from './lib/i18n';
import { getComponentLocale } from 'bee-locale/build/tool'; import { getComponentLocale } from 'bee-locale/build/tool';
import { formatMoney } from './lib/utils';
const propTypes = { const propTypes = {
record: PropTypes.object, record: PropTypes.object,
clsPrefix: PropTypes.string, clsPrefix: PropTypes.string,
@ -70,7 +71,8 @@ class TableCell extends Component{
return data; return data;
} }
renderBoolType = ( data, record, index, config ) => { // 渲染布尔类型
renderBoolType = ( data, record, index, config={} ) => {
let locale = getComponentLocale(this.props, this.context, 'Table', () => i18n); let locale = getComponentLocale(this.props, this.context, 'Table', () => i18n);
let boolConfig = {...{ trueText: locale['bool_true'], falseText: locale['bool_false']},...config}; let boolConfig = {...{ trueText: locale['bool_true'], falseText: locale['bool_false']},...config};
if(typeof data === 'string'){ if(typeof data === 'string'){
@ -84,6 +86,22 @@ class TableCell extends Component{
return boolConfig.trueText; 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 = <span className='u-table-currency-number'>{number}</span>;
let pre = config.preSymbol ? <span className='u-table-currency-pre'>{config.preSymbol}</span> : null;
let next = config.nextSymbol ? <span className='u-table-currency-next'>{config.nextSymbol}</span> : null;
return <span className='u-table-currency'>
{pre}
{res}
{next}
</span>;
}
render() { render() {
const { record, indentSize, clsPrefix, indent, const { record, indentSize, clsPrefix, indent,
index, expandIcon, column ,fixed,showSum, bodyDisplayInRow,lazyStartIndex,lazyEndIndex} = this.props; 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); text = this.renderBoolType(text, record, index, column.boolConfig);
break; break;
} }
case 'currency':{
text = this.renderCurrency(text, record, indent, column.currencyConfig);
break;
}
default : { default : {
break; break;
} }

View File

@ -293,6 +293,23 @@ export function checkDicimalInvalid(value, precision) {
return result; 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 = { export const Event = {
addHandler, addHandler,
removeHandler, removeHandler,