1. import React, { Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Form, InputNumber, Input, DatePicker, Button, Select } from 'antd';
  4. import moment from 'moment';
  5. // 推荐在入口文件全局设置 locale
  6. import 'moment/locale/zh-cn';
  7. moment.locale('zh-cn');
  8.  
  9. const FormItem = Form.Item;
  10. const Option = Select.Option;
  11.  
  12. // 后台返回的数据格式
  13. const data = [
  14. {
  15. 'field': 'jobid',
  16. 'text': '工号',
  17. 'errorMessage': '请输入工号',
  18. 'required': true,
  19. 'type': 'int',
  20. 'value': 100
  21. },{
  22. 'field': 'date',
  23. 'text': '日期',
  24. 'errorMessage': '请输入日期',
  25. 'required': false,
  26. 'type': 'date',
  27. 'value': '2017-10-20'
  28. },{
  29. 'field': 'username',
  30. 'text': '用户名',
  31. 'errorMessage': '请输入用户名',
  32. 'required': true,
  33. 'type': 'char',
  34. 'value': 'hello world'
  35. },{
  36. 'field': 'customer',
  37. 'text': '客户',
  38. 'errorMessage': '请输入客户',
  39. 'required': true,
  40. 'type': 'select',
  41. 'value': '中兴',
  42. 'options': ['贝尔', '中兴', '烽火']
  43. }
  44. ]
  45.  
  46. // formItem css 样式
  47. const formItemLayout = {
  48. labelCol: {
  49. xs: { span: 24 },
  50. sm: { span: 6 },
  51. },
  52. wrapperCol: {
  53. xs: { span: 24 },
  54. sm: { span: 14 },
  55. }
  56. }
  57.  
  58. // 保存按钮 css 样式
  59. const tailFormItemLayout = {
  60. wrapperCol: {
  61. xs: {
  62. span: 24,
  63. offset: 0,
  64. },
  65. sm: {
  66. span: 14,
  67. offset: 6,
  68. },
  69. }
  70. }
  71.  
  72. // form css 样式
  73. const formLayout = {
  74. width: 400,
  75. marginTop: 100,
  76. marginLeft: 'auto',
  77. marginRight: 'auto'
  78. }
  79.  
  80. class App extends Component {
  81. handleSubmit(e) {
  82. e.preventDefault();
  83. this.props.form.validateFields((err, values) => {
  84. if (!err) {
  85. console.log('Received values of form: ', values);
  86. }
  87. });
  88. }
  89.  
  90. /**
  91. * 根据后台返回的 data 中 type 类型生成不同的组件
  92. * @param item json
  93. * @param Component
  94. */
  95. switchItem(item) {
  96. const type = item.type;
  97. switch (type) {
  98. case 'int':
  99. return <InputNumber style={{ width: '100%' }} />;
  100. break;
  101. case 'char':
  102. return <Input />;
  103. break;
  104. case 'date':
  105. return <DatePicker style={{ width: '100%' }} />;
  106. break;
  107. case 'select':
  108. return (
  109. <Select>
  110. {
  111. item.options.map((option, index) => {
  112. return (<Option key={index} value={option}>{option}</Option>)
  113. })
  114. }
  115. </Select>
  116. )
  117. default:
  118. return <Input />;
  119. break;
  120. }
  121. }
  122.  
  123. render() {
  124. const { getFieldDecorator } = this.props.form;
  125. return (
  126. <Form onSubmit={this.handleSubmit} style={formLayout}>
  127. {
  128. data.map((item, index) => {
  129. // type 为 date 日期格式需要强制转化为 moment 格式
  130. item.value = item.type == 'date' ? moment(item.value, 'YYYY-MM-DD') : item.value;
  131. return (
  132. <FormItem
  133. key={index}
  134. {...formItemLayout}
  135. label={item.text}
  136. hasFeedback
  137. >
  138. {getFieldDecorator(item.field, {
  139. initialValue: item.value,
  140. rules: [{
  141. required: item.required,
  142. message: item.errorMessage
  143. }],
  144. })(
  145. this.switchItem(item)
  146. )}
  147. </FormItem>
  148. )
  149. })
  150. }
  151. <FormItem {...tailFormItemLayout}>
  152. <Button type="primary" htmlType="submit">
  153. 保存
  154. </Button>
  155. </FormItem>
  156. </Form>
  157. )
  158. }
  159. }
  160.  
  161. const AppForm = Form.create()(App);
  162.  
  163. ReactDOM.render(<AppForm />, document.getElementById('root'));

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

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

解决方法:

  1. constructor(props) {
  2. super(props);
  3. this.handleSubmit = this.handleSubmit.bind(this);
  4. }

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. CoreThink开发(十三)增加页面加载动画

    效果: 加载动画是由jquery和fakeloader这个js库实现的. 其实这个也可以做成一个插件,用数据库记录是否开启,选择动画的样式,那样扩展性会更好. 源码资源已经上传在我的csdn下载中. ...

  2. 【算法题12 解码方法decode way】

    1.来源LeetCode91 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请 ...

  3. Python 网络编程了解

    阅读目录 一: 网络编程socket http://www.cnblogs.com/zhoujunhao/articles/7592671.html 二: TCP粘包处理 http://www.cnb ...

  4. python网络编程——SocketServer/Twisted/paramiko模块

    在之前博客C/S架构的网络编程中,IO多路复用是将多个IO操作复用到1个服务端进程中进行处理,即无论有多少个客户端进行连接请求,服务端始终只有1个进程对客户端进行响应,这样的好处是节省了系统开销(se ...

  5. ie9下面的console的bug

    摘自:http://blog.csdn.net/cdnight/article/details/51094464 ie9下面,很奇怪的是有console的代码有时候执行不下去,不过当f12打开控制台的 ...

  6. 【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作

    include指令与include动作 1.样例代码 <%@ page contentType="text/html;charset=UTF-8" language=&quo ...

  7. Zabbix 自定义Key

    系统:Linux Centos 7.4 x64.Windos 2008 x64 服务:Zabbix 3.0.16 说明1:自定义Key 主要通过自定义 脚本 或者 命令 来实现自定义监控类型,需要在a ...

  8. HTML5/CSS3实现图片倒影效果

    在线演示 本地下载

  9. invalid derived query的解决办法

    标签: eclipse / invalid / derived / 解决办法 / 校验功能 479 在Eclipse的运行过程中,突然有一个接口跳出如下错误: invalid derived quer ...

  10. javascript-实现小抽奖程序

    直接上代码 <style> *{ margin: 0; padding:0;} .prize_wrap{ width: 300px; height: 150px; } .prize_wra ...