import React from "react"; import { Spin, Button, Tabs, Table, message, Modal, Input, Tag, Popconfirm } from "antd"; import img from "./example.png" import $ from "jquery/src/ajax"; const ResolutionPro = React.createClass({ getInitialState() { return { visible: false, proArr: [ { name: "子项目1", number: 0 }, { name: "子项目2", number: 0 }, { name: "子项目3", number: 0 }, ], inputArr: [] }; }, componentWillReceiveProps(nextProps) { this.setState({ visible: nextProps.resolutionVisible }); const arr = [ { name: "子项目1", number: 0 }, { name: "子项目2", number: 0 }, { name: "子项目3", number: 0 } ]; this.setState({ proArr: arr }); }, handleOk() { const arr = [ { name: "子项目1", number: 0 }, { name: "子项目2", number: 0 }, { name: "子项目3", number: 0 } ]; this.setState({ visible: false, proArr: arr }); }, addPro() { const obj = { name: "子项目" + (this.state.proArr.length + 1), number: 0 } this.state.proArr.push(obj) this.setState({ proArr: this.state.proArr }) }, deletePro() { if(this.state.proArr.length == 2) { message.warning("不能继续删除子项目!") return } this.state.proArr.splice(this.state.proArr.length - 1, 1) this.setState({ proArr: this.state.proArr }) }, submit() { const arr = this.state.proArr const numArr = [] arr.forEach(item => { let num = +item.number if(num != 0) { numArr.push(num); } }); let sum = 0 numArr.forEach( item => { sum += item }) if(sum != this.props.number) { message.warning("拆分数量总和与原项目数量不匹配,请重新填写数量!") return } let str = numArr.join(",") this.setState({ loading: true }); $.ajax({ method: "post", dataType: "json", crossDomain: false, url: globalConfig.context + "/api/admin/orderProject/addSplitProject", data: { tid: this.props.id, splitList: str }, success: function(data) { let thedata = data.data; if (!thedata) { if (data.error && data.error.length) { message.warning(data.error[0].message); } thedata = {}; } else { message.success("拆分成功!") this.props.cancel() this.props.loadData() } }.bind(this) }).always( function() { this.setState({ loading: false }); }.bind(this) ); }, changeNum() { let sum = 0 this.state.proArr.forEach(item => { sum += (+item.number) }) return sum }, render() { return (
当前项目名称:{this.props.name}
当前项目数量:{this.props.number}
选项:
{this.state.proArr.map((item, index) => { return (
{item.name} { if (e.target.value.trim() == "") { this.state.proArr[index].number = 0; this.setState({ proArr: this.state.proArr }); } }} onChange={e => { const reg = /^[0-9]\d*$/; if (!reg.test(+e.target.value)) { return; } this.state.proArr[index].number = e.target.value; this.setState( { proArr: this.state.proArr }, () => { // console.log(this.state.proArr); } ); }} /> {index == this.state.proArr.length - 1 ? "=" : "+"} {index == this.state.proArr.length - 1 ? this.changeNum() : ""}
); })}

各子项目相加需等于当前项目数量!

系统提示:请确定将当前项目数量按选项,进行拆分,拆分为子项目后,不可修改!!! } onConfirm={this.submit} okText="确定" cancelText="取消" >
); } }); export default ResolutionPro;