react ant design编辑行引入日期格式

需求是点击编辑,整个一行需要能编辑,有三个是日期选择格式的
因为用的版本是3.x版本,所以我把整个官方给的文档拷贝了
官方代码:https://3x.ant.design/components/table-cn/#components-table-demo-edit-row
然后融合自己的其他功能的代码,其中日期格式的如下:虽然丑陋了些,但是实现了功能


const data = [];
const EditableContext = React.createContext();
/**
 * 给列添加input 和 日期格式
 */
class EditableCell extends React.Component {
    getInput = (value) => {
        if (this.props.inputType === 'date') {
        	//当inputType为date时,则返回 DatePicker 插入到列中
            return <DatePicker showTime format="YYYYMMDD" />;
        }
        return <Input maxLength={50} />;
    };
    renderCell = ({ getFieldDecorator }) => {
        const {
            editing,
            dataIndex,
            title,
            inputType,
            record,
            index,
            children,
            ...restProps
        } = this.props;
        //这里也需要判断,因为日期格式需要moment()来初始化否则会报错:
        //The value/defaultValue of DatePicker or MonthPicker must be a moment object after `antd@2.0`,用moment转化就能解决
        if (editing && inputType === 'date') {
            return (
                <td {...restProps}>
                    <Form.Item style={{ margin: 0 }}>
                        {getFieldDecorator(dataIndex, {
                            rules: [
                                {
                                    required: true,
                                    message: `Please Input ${title}!`,
                                },
                            ],
                            initialValue: moment(record[dataIndex]),
                        })(this.getInput())}
                    </Form.Item>
                </td>
            );
        } else if (editing && inputType === 'text') {
            return (
                <td {...restProps}>
                    <Form.Item style={{ margin: 0 }}>
                        {getFieldDecorator(dataIndex, {
                            rules: [
                                {
                                    required: true,
                                    message: `Please Input ${title}!`,
                                },
                            ],
                            initialValue: record[dataIndex],
                        })(this.getInput())}
                    </Form.Item>
                </td>
            );
        } else {
            return (
                <td {...restProps}>
                    {children}
                </td>
            );
        }

    };
    render() {
        return <EditableContext.Consumer>{this.renderCell}</EditableContext.Consumer>;
    }
}

class NewBondManageCopy extends React.Component {
    constructor(props) {
        super(props);
        this.state={
        	data,
			//把bondColumns导入赋值给  columns      	
            columns: bondColumns,
            //可编辑的列
            editingKey: "",
        }
    }
 	//给table 赋值data   保存时需要 将格式转化回来
    valueOfData = (row) => {
        return {
            bondCode: row.bondCode,
            bondFullName: row.bondFullName,
            bondName: row.bondName,
            bookkeepingDate: row.bookkeepingDate.format("YYYYMMDD"),
            bookkeepingTime: row.bookkeepingTime.format("YYYYMMDD"),
            creditratingagency: row.creditratingagency,
            exchmarket: row.exchmarket,
            guarantor: row.guarantor,
            ifAttachProspectus: row.ifAttachProspectus,
            ifAttachRatingReport: row.ifAttachRatingReport,
            ifCompetitiveSales: row.ifCompetitiveSales,
            interestRateRange: row.interestRateRange,
            issuedScale: row.issuedScale,
            issuedTerm: row.issuedTerm,
            outDebtRatingRank: row.outDebtRatingRank,
            outMainRatingRank: row.outMainRatingRank,
            paymentDate: row.paymentDate.format("YYYYMMDD"),
            remark: row.remark,
            underwriter: row.underwriter
        }
    }

    save(form, record) {
        let id = record.id
        let key = record.key
        const { dispatch } = this.props;
        let that = this;

        //判断,日期格式,判断
        form.validateFields((error, row) => {
            if (error) {
                return;
            }
            that.handleJugement(row);
            const newData = [...this.state.data];
            const index = newData.findIndex(item => key === item.key);
            if (index > -1) {
                const item = newData[index];
                let postData = that.valueOfData(row);
                postData.id = id;
                dispatch({
					url:"url"
                    payload: postData,
                    callback: res => {
                        if (res.resultCode == "200") {
                            message.success('保存成功');
                            newData.splice(index, 1, {
                                ...item,
                                ...that.valueOfData(row),
                            });
                            that.setState({ data: newData, editingKey: '' });
                        } else {
                            message.error('保存失败')
                        }
                    }
                })
            } else {
                newData.push(row);
                this.setState({ data: newData, editingKey: '' });
            }
        });
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值