需求是点击编辑,整个一行需要能编辑,有三个是日期选择格式的
因为用的版本是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: '' });
}
});
}