From e0e550f360e263652f528bd6d810ebd88db28fd2 Mon Sep 17 00:00:00 2001 From: huyueb <1062887235@qq.com> Date: Mon, 16 Oct 2017 17:01:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAInputRender=E5=A2=9E=E5=8A=A0format?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E8=B4=A7=E5=B8=81=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/rendertype.md | 1 + src/render/InputRender.js | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/rendertype.md b/docs/rendertype.md index 0e770ab..edc6673 100644 --- a/docs/rendertype.md +++ b/docs/rendertype.md @@ -10,4 +10,5 @@ | value | render的值 | string | 无 | | isclickTrigger | 是否使用点击触发编辑状态 | boolean | false | | onChange | 当值发生改变的时候触发的方法 | Function | 无 | +| format | Currency:货币数字; | string | 无 | diff --git a/src/render/InputRender.js b/src/render/InputRender.js index 36379e8..003b6ec 100644 --- a/src/render/InputRender.js +++ b/src/render/InputRender.js @@ -25,9 +25,39 @@ export default class InputRender extends Component { this.check(); } }; + //货币的格式化方法 + formatCurrency = money => { + if (money && money != null && !!Number(money)) { + money = String(money); + let left = money.split(".")[0], + right = money.split(".")[1]; + right = right + ? right.length >= 2 ? "." + right.substr(0, 2) : "." + right + "0" + : ".00"; + let temp = left + .split("") + .reverse() + .join("") + .match(/(\d{1,3})/g); + return ( + (Number(money) < 0 ? "-" : "") + + temp + .join(",") + .split("") + .reverse() + .join("") + + right + ); + } else if (money === 0) { + //注意===在这里的使用,如果传入的money为0,if中会将其判定为boolean类型,故而要另外做===判断 + return "0.00"; + } else { + return ""; + } + }; render() { - const { value, editable } = this.state; - let { isclickTrigger } = this.props; + let { value, editable } = this.state; + let { isclickTrigger,format } = this.props; let cellContent = ""; if (editable) { cellContent = isclickTrigger ? ( @@ -55,6 +85,9 @@ export default class InputRender extends Component { ); } else { + if(format && format === "Currency"){ + value = this.formatCurrency(value); + } cellContent = isclickTrigger ? (
{value || " "}