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(
<>
<Form layout="vertical">
<FormItem label="姓名">
{getFieldDecorator('name')( //2、getFieldDecorator 的使用方法,这种写法真的很蛋疼
<Input />
)}
</FormItem>
<FormItem label="年龄">
{getFieldDecorator('age')(
<Input />
)}
</FormItem>
<FormItem label="城市">
{getFieldDecorator('address')(
<Input />
)}
</FormItem>
</Form>
</>
)
}
}
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 (
<Modal
visible={visible}
title="新增"
okText="保存"
onCancel={this.handleCancel}
onOk={this.handleCreate}
>
<Forms
wrappedComponentRef={(form) => this.formRef = form} //5、使用wrappedComponentRef 拿到子组件传递过来的ref(官方写法)
/>
</Modal>
);
}
}
官方文档
class CustomizedForm extends React.Component { ... }
// use wrappedComponentRef
const EnhancedForm = Form.create()(CustomizedForm);
<EnhancedForm wrappedComponentRef={(form) => this.form = form} />
this.form // => The instance of CustomizedForm
antd 父组件获取子组件中form表单的值的更多相关文章
- [js开源组件开发]query组件,获取url参数和form表单json格式
query组件,获取url参数和form表单json格式 距离上次的组件[js开源组件开发]ajax分页组件一转眼过去了近二十天,或许我一周一组件的承诺有了质疑声,但其实我一直在做,只是没人看到……, ...
- MVC中Form表单的提交
概述 Web页面进行Form表单提交是数据提交的一种,在MVC中Form表单提交到服务器.服务端接受Form表单的方式有多种,如果一个Form有2个submit按钮,那后台如何判断是哪个按钮提交的数据 ...
- laravel中form表单,ajax传值没反应
laravel中form表单,ajax传值没反应时,可能是令牌有问题. form中添加: {{csrf_token()}} ajax中添加: data: {'page': page, '_token' ...
- asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!)
原文:asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!) 我想用post的方式把一个页面表单的值,传到另一个页面.当我点击Default.as ...
- Jquery Form表单取值
之前js取form表单的值都是一个一个的取,数量一多之后容易出错而且烦透了.感谢那些愿意分享的人. 页面定义form,并给form指定id值,里面的元素只要是需要键值对应的都赋予name属性,并且na ...
- 取值:form表单取值、input框绑定取值
1. form表单取值1.1 方式一,通过<form bindsubmit="formSubmit">与<button formType="submit ...
- 关于Vue中,父组件获取子组件的数据(子组件调用父组件函数)的方法
1. 父组件调用子组件时,在调用处传给子组件一个方法 :on-update="updateData" 2. 子组件在props中,接收这个方法并声明 props: { onUp ...
- Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...
- 子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp
(一) popsDowm 三种方法获取父组件数据:被动获得(1):主动获取(2). 1.被动获得: 父组件:v-bind: 绑定变量参数和方法参数:子组件:props 接收参数.可以在模板中直接使用也 ...
随机推荐
- strcpy unsigned char
http://bbs.csdn.net/topics/250068243 char *strcpy(char* dest, const char *src); 用unsigned char编译会出错 ...
- 转载:Github项目解析(七)-->防止按钮重复点击
不错的东西,记录下... http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/qq_23547831/article/deta ...
- 【前端vue开发】vue开发watch检测的使用
<span style="color:#006600;"><div id="app"> <input type="tex ...
- 【本地服务器】用nodejs搭建最简单、轻量化的http server
1. 引言 前端程序猿主要关注的是页面,你可能根本就用不到.net,java,php等后台语言. 但是你制作出来的网页总要运行.总要测试吧?——那就免不了用到http server.我先前都是用vis ...
- ASP.NET中Literal,只增加纯粹的内容,不附加产生html代码
页面代码 <div style="float: right; color: #666; line-height: 30px; margin-right: 12px;" id= ...
- sizeof求结构体大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 接口测试工具--Poster与Postman的简单实用
HTTP/SOAP协议接口的功能测试: 1.浏览器URL(GET请求) http://127.0.0.1:8000/login/?username=zhangsan&password=1234 ...
- ASP.NET:插件化机制
概述 nopCommerce的插件机制的核心是使用BuildManager.AddReferencedAssembly将使用Assembly.Load加载的插件程序集添加到应用程序域的引用中.具体实现 ...
- LogStash plugins-inputs-file介绍(三)
官方文档 https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html 重要参数: path # 文件路径 sin ...
- R语言实战(六)重抽样与自助法
本文对应<R语言实战>第12章:重抽样与自助法 之前学习的基本统计分析.回归分析.方差分析,是假定观测数据抽样自正态分布或者其他性质较好的理论分布,进而进行的假设检验和总体参数的置信区间估 ...