import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Form, InputNumber, Input, DatePicker, Button, Select } from 'antd';
import moment from 'moment';
// 推荐在入口文件全局设置 locale
import 'moment/locale/zh-cn';
moment.locale('zh-cn'); const FormItem = Form.Item;
const Option = Select.Option; // 后台返回的数据格式
const data = [
{
'field': 'jobid',
'text': '工号',
'errorMessage': '请输入工号',
'required': true,
'type': 'int',
'value': 100
},{
'field': 'date',
'text': '日期',
'errorMessage': '请输入日期',
'required': false,
'type': 'date',
'value': '2017-10-20'
},{
'field': 'username',
'text': '用户名',
'errorMessage': '请输入用户名',
'required': true,
'type': 'char',
'value': 'hello world'
},{
'field': 'customer',
'text': '客户',
'errorMessage': '请输入客户',
'required': true,
'type': 'select',
'value': '中兴',
'options': ['贝尔', '中兴', '烽火']
}
] // formItem css 样式
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 14 },
}
} // 保存按钮 css 样式
const tailFormItemLayout = {
wrapperCol: {
xs: {
span: 24,
offset: 0,
},
sm: {
span: 14,
offset: 6,
},
}
} // form css 样式
const formLayout = {
width: 400,
marginTop: 100,
marginLeft: 'auto',
marginRight: 'auto'
} class App extends Component {
handleSubmit(e) {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
} /**
* 根据后台返回的 data 中 type 类型生成不同的组件
* @param item json
* @param Component
*/
switchItem(item) {
const type = item.type;
switch (type) {
case 'int':
return <InputNumber style={{ width: '100%' }} />;
break;
case 'char':
return <Input />;
break;
case 'date':
return <DatePicker style={{ width: '100%' }} />;
break;
case 'select':
return (
<Select>
{
item.options.map((option, index) => {
return (<Option key={index} value={option}>{option}</Option>)
})
}
</Select>
)
default:
return <Input />;
break;
}
} render() {
const { getFieldDecorator } = this.props.form;
return (
<Form onSubmit={this.handleSubmit} style={formLayout}>
{
data.map((item, index) => {
// type 为 date 日期格式需要强制转化为 moment 格式
item.value = item.type == 'date' ? moment(item.value, 'YYYY-MM-DD') : item.value;
return (
<FormItem
key={index}
{...formItemLayout}
label={item.text}
hasFeedback
>
{getFieldDecorator(item.field, {
initialValue: item.value,
rules: [{
required: item.required,
message: item.errorMessage
}],
})(
this.switchItem(item)
)}
</FormItem>
)
})
}
<FormItem {...tailFormItemLayout}>
<Button type="primary" htmlType="submit">
保存
</Button>
</FormItem>
</Form>
)
}
} const AppForm = Form.create()(App); ReactDOM.render(<AppForm />, document.getElementById('root'));

https://ant.design/components/form-cn/

Uncaught TypeError: Cannot read property 'props' of undefined
at handleSubmit (http://localhost:8088/js/app.js:78431:17)
at HTMLUnknownElement.callCallback (http://localhost:8088/js/app.js:59422:14)
at Object.invokeGuardedCallbackDev (http://localhost:8088/js/app.js:59461:16)
at Object.invokeGuardedCallback (http://localhost:8088/js/app.js:59318:27)
at Object.invokeGuardedCallbackAndCatchFirstError (http://localhost:8088/js/app.js:59332:43)
at executeDispatch (http://localhost:8088/js/app.js:59716:19)
at executeDispatchesInOrder (http://localhost:8088/js/app.js:59738:5)
at executeDispatchesAndRelease (http://localhost:8088/js/app.js:59836:5)
at executeDispatchesAndReleaseTopLevel (http://localhost:8088/js/app.js:59847:10)
at Array.forEach (native)

解决方法:

constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}

react antd 动态表单的更多相关文章

  1. react + antd Form表单校验

    非空限制 {getFieldDecorator('name', { rules: [{ required: true, message: '名称不能为空', }],})( <Input plac ...

  2. 【react】实现动态表单中嵌套动态表单

    要实现一个功能动态表单中嵌套动态表单如下: 仔细看看antd的文档其实不难 具体步骤如下 1.建立一个 名为 ConcatRegion的组件(动态表单A)代码如下 export function Co ...

  3. antd+vue3实现动态表单的自动校验

    由于vue3用的人还不多,所以有些问题博主踩了坑只能自己爬出来了,特此做个记录.如有错误,请大家指正. 回归正题,我所做的业务是,动态添加表单项,对每一项单独做校验,效果如下: 主要代码如下: 1 & ...

  4. [K/3Cloud] 如何从被调用的动态表单界面返回数据

    在需要返回数据的地方调用表单返回方法完成数据返回 this.View.ReturnToParentWindow(retData); 在调用界面的回调函数中取出返回结果的ReturnData即可使用. ...

  5. Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成)

    Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成) 动态表单生成 ElementUI官网引导 Element表单生成 Element动态增减表单,在线代码 关键配置 templa ...

  6. 简易OA漫谈之工作流设计(六,快捷表单和动态表单)

    如果没有表单设计功能,我们一般建物理表,再把表单挂接到流程, 我们可以把外接表单的地址填到表单地址中,地址中会传递一个id. 如果使用外接表单,在审批的时候可能会“不太友好”,因为在审批单上看不到任何 ...

  7. angularjs 动态表单, 原生事件中调用angular方法

    1. 原生事件中调用angular方法, 比如 input的onChange事件想调用angular里面定义的方法 - onChange="angular.element(this).sco ...

  8. vue 开发系列(八) 动态表单开发

    概要 动态表单指的是我们的表单不是通过vue 组件一个个编写的,我们的表单是根据后端生成的vue模板,在前端通过vue构建出来的.主要的思路是,在后端生成vue的模板,前端通过ajax的方式加载后端的 ...

  9. Struts动态表单(DynamicForm)

    动态表单的含义是不要手动定义,直接在配置文件中进行定义. 1.手动进行定义 <form-beans > <form-bean name="userForm" ty ...

随机推荐

  1. Pytorch(一)

    一.Pytorch介绍 Pytorch 是Torch在Python上的衍生物 和Tensorflow相比: Pytorch建立的神经网络是动态的,而Tensorflow建立的神经网络是静态的 Tens ...

  2. 【Spring MVC】spring mvc中相同的url请求返回不同的结果

    在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码, ...

  3. C#设置当前程序通过IE代理服务器上网

    注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...

  4. java 自制Tomcat Andorid IOS 端 证书

    java 自制证书 最近做项目用到Https 需要自制各种证书,Tomcat 用的JKS 格式, Andorid 端使用 BKS 格式, IOS 端使用 P12格式正式, 以及各种证书格式之间的转换. ...

  5. python之路 面向对象基础 XML

    一.面向对象基础 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...

  6. 杭电1025Constructing Roads In JGShining's Kingdom

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025 题目: Problem Description JGShining's kingdom consis ...

  7. javascript 的dateObj.getTime() 在为C#的获取方式

    public string GetTime(DateTime dt) { Int64 retval = 0; DateTime st = new DateTime(1970, 1, 1); TimeS ...

  8. Mysql:实现分组查询拼接未分组同一字段字符group_concat()

    Mysql:实现分组查询拼接未分组同一字段字符group_concat() MySQL中,如果想实现将分组之后的多个数据合并到一列,可以使用group_concat函数,如下图所示: 在oralce中 ...

  9. Apache 浏览器访问限制配置

    浏览器访问限制配置 user_agent收入的浏览器中,我们通过百度,谷歌很容易就可以查到相关的一些资料,方便了我们对知识的查找,但在某些特定情况下,我们并不希望有人可以通过某写搜索引擎直接访问到我们 ...

  10. BeatSaber节奏光剑插件开发官方教程2-简单的插件示例

    原文:https://wiki.assistant.moe/modding/example-mod 一.在开始之前 1 确保你已经看过教你如何添加插件模板的教程,且你已经使用插件模板创建了一个新项目 ...