日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

antd如何获取表单的值_antd 父组件获取子组件中form表单的值

發布時間:2025/3/20 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 antd如何获取表单的值_antd 父组件获取子组件中form表单的值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

還是拿代碼來講吧,詳情見注釋

子組件

import React, { Component } from 'react';

import { Form, Input } from 'antd';

const FormItem = Form.Item;

class Forms extends Component{

getItemsValue = ()=>{ //3、自定義方法,用來傳遞數據(需要在父組件中調用獲取數據)

const valus= this.props.form.getFieldsValue(); //4、getFieldsValue:獲取一組輸入控件的值,如不傳入參數,則獲取全部組件的值

return valus;

}

render(){

const { form } = this.props;

const { getFieldDecorator } = form; //1、將getFieldDecorator 解構出來,用于和表單進行雙向綁定

return(

<>

{getFieldDecorator('name')( //2、getFieldDecorator 的使用方法,這種寫法真的很蛋疼

)}

{getFieldDecorator('age')(

)}

{getFieldDecorator('address')(

)}

>

)

}

}

export default Form.create()(Forms); //創建form實例

getFieldDecorator 的具體參數見官方文檔

父組件

import React, { Component } from 'react';

import { Modal } from 'antd';

import Forms from './Forms'

export default class Modals extends Component {

handleCancel = () => {

this.props.closeModal(false);

}

handleCreate = () => {

console.log(this.formRef.getItemsValue()); //6、調用子組件的自定義方法getItemsValue。注意:通過this.formRef 才能拿到數據

this.props.getFormRef(this.formRef.getItemsValue());

this.props.closeModal(false);

}

render() {

const { visible } = this.props;

return (

visible={visible}

title="新增"

okText="保存"

onCancel={this.handleCancel}

onOk={this.handleCreate}

>

wrappedComponentRef={(form) => this.formRef = form} //5、使用wrappedComponentRef 拿到子組件傳遞過來的ref(官方寫法)

/>

);

}

}

官方文檔

class CustomizedForm extends React.Component { ... }

// use wrappedComponentRef

const EnhancedForm = Form.create()(CustomizedForm);

this.form = form} />

this.form // => The instance of CustomizedForm

總結

以上是生活随笔為你收集整理的antd如何获取表单的值_antd 父组件获取子组件中form表单的值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。