鍍金池/ 問答/HTML/ Antd的form組件無法重置,正確顯示一條記錄的值

Antd的form組件無法重置,正確顯示一條記錄的值

如圖:每一條記錄點擊編輯的時候form顯示相應的記錄的值,我的做法是點擊編輯獲取記錄的record存在state.formData中,如何把state.formData當做props參數(shù)傳遞給form組件,form組件的初始值從this.props.formData里面取。

圖片描述

問題是:我如果只點擊編輯然后關掉,再點擊其他記錄的編輯顯示是沒有問題的。但是只要我點擊編輯,修改了input的值,關閉以后。再點擊其他任何記錄,form顯示的都是剛剛修改了然后關閉了的記錄。

比如:選擇某一條修改兩處為test之后,沒有點擊確定直接點擊關閉。
圖片描述

如何點擊其他所有記錄都變成了這樣:
圖片描述

我在form的render函數(shù)下面打印了接受到的props.formData參數(shù)是沒有問題的,是記錄傳遞過來的值,但是顯示的時候有問題:
這個是整個form的代碼

import React from 'react';
import { Form , Input , Button } from 'antd';

const FormItem = Form.Item;
const formItemLayout = {
    labelCol : {span : 5},
    wrapperCol : {span : 15} 
};
class FormLayout extends React.Component{
    handleSubmit(e){
        e.preventDefault();
        this.props.comfirmHandle(this.props.form.getFieldsValue()); //獲取當前表單數(shù)據(jù)并當做回調(diào)函數(shù)的參數(shù)傳遞給父組件
    }

    // componentWillReceiveProps(nextProps){
    //     if(!nextProps.visible){
    //         this.props.form.resetFeilds();
    //     }
    // }

    render(){
        const { getFieldDecorator ,getFeildsValue } = this.props.form;
        const { record } = this.props;

        return (
            <Form onSubmit= {this.handleSubmit.bind(this)}>
                <FormItem label="編號" {...formItemLayout}>
                    {getFieldDecorator('id', { 
                        rules: [{ required: true, message: '請輸入書籍編號!' }],
                        initialValue : record ? record.id : ""
                    })(
                        <Input placeholder="請輸入書籍編號"/>
                    )}
                </FormItem>
                <FormItem label="名稱" {...formItemLayout}>
                    {getFieldDecorator('name', { 
                        rules: [{ required: true, message: '請輸入書籍名稱!' }],
                        initialValue : record ? record.name : ""
                    })(
                        <Input placeholder="請輸入書籍名稱"/>
                    )}
                </FormItem>
                <FormItem label="價格"  {...formItemLayout}>
                    {getFieldDecorator('price', {
                        rules: [{ required: true, message: '請輸入價格!' }],
                        initialValue : record ?  record.price : ""
                    })(
                        <Input placeholder="請輸入價格"/>
                    )}
                </FormItem>
                <FormItem label="借閱者編號"  {...formItemLayout}>
                    {getFieldDecorator('owner_id', { 
                        rules: [{ required: true, message: '請輸入借閱者編號!' }],
                        initialValue : record ? record.owner_id :""
                    })(
                        <Input placeholder="請輸入借閱者編號"/>
                    )}
                </FormItem>
                <FormItem wrapperCol={{ span: 10, offset: 10 }}>
                    <Button type="primary" htmlType="submit">
                        確定
                    </Button>
                </FormItem>
            </Form>
        );
    }
}

export default FormLayout = Form.create()(FormLayout);
回答
編輯回答
蟲児飛

每次打開Modal的時候,resetFields一下就好了。

2018年2月27日 16:45
編輯回答
敢試

瀉藥,大概看了一下

  1. 不點確定關閉窗口時 (onCancel),你應該清空 state.formData 里的值,或者調(diào) form.resetFields()
  2. 可以給 Modal 加個參數(shù) destroyOnClose
2017年10月8日 10:18