为InputRender增加format参数,货币数字格式转换

This commit is contained in:
huyueb 2017-10-16 17:01:41 +08:00
parent f9e5c299f1
commit e0e550f360
2 changed files with 36 additions and 2 deletions

View File

@ -10,4 +10,5 @@
| value | render的值 | string | 无 | | value | render的值 | string | 无 |
| isclickTrigger | 是否使用点击触发编辑状态 | boolean | false | | isclickTrigger | 是否使用点击触发编辑状态 | boolean | false |
| onChange | 当值发生改变的时候触发的方法 | Function | 无 | | onChange | 当值发生改变的时候触发的方法 | Function | 无 |
| format | Currency:货币数字; | string | 无 |

View File

@ -25,9 +25,39 @@ export default class InputRender extends Component {
this.check(); 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() { render() {
const { value, editable } = this.state; let { value, editable } = this.state;
let { isclickTrigger } = this.props; let { isclickTrigger,format } = this.props;
let cellContent = ""; let cellContent = "";
if (editable) { if (editable) {
cellContent = isclickTrigger ? ( cellContent = isclickTrigger ? (
@ -55,6 +85,9 @@ export default class InputRender extends Component {
</div> </div>
); );
} else { } else {
if(format && format === "Currency"){
value = this.formatCurrency(value);
}
cellContent = isclickTrigger ? ( cellContent = isclickTrigger ? (
<div className="editable-cell-text-wrapper" onClick={this.edit}> <div className="editable-cell-text-wrapper" onClick={this.edit}>
{value || " "} {value || " "}