react antd 动态表单
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 动态表单的更多相关文章
- react + antd Form表单校验
非空限制 {getFieldDecorator('name', { rules: [{ required: true, message: '名称不能为空', }],})( <Input plac ...
- 【react】实现动态表单中嵌套动态表单
要实现一个功能动态表单中嵌套动态表单如下: 仔细看看antd的文档其实不难 具体步骤如下 1.建立一个 名为 ConcatRegion的组件(动态表单A)代码如下 export function Co ...
- antd+vue3实现动态表单的自动校验
由于vue3用的人还不多,所以有些问题博主踩了坑只能自己爬出来了,特此做个记录.如有错误,请大家指正. 回归正题,我所做的业务是,动态添加表单项,对每一项单独做校验,效果如下: 主要代码如下: 1 & ...
- [K/3Cloud] 如何从被调用的动态表单界面返回数据
在需要返回数据的地方调用表单返回方法完成数据返回 this.View.ReturnToParentWindow(retData); 在调用界面的回调函数中取出返回结果的ReturnData即可使用. ...
- Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成)
Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成) 动态表单生成 ElementUI官网引导 Element表单生成 Element动态增减表单,在线代码 关键配置 templa ...
- 简易OA漫谈之工作流设计(六,快捷表单和动态表单)
如果没有表单设计功能,我们一般建物理表,再把表单挂接到流程, 我们可以把外接表单的地址填到表单地址中,地址中会传递一个id. 如果使用外接表单,在审批的时候可能会“不太友好”,因为在审批单上看不到任何 ...
- angularjs 动态表单, 原生事件中调用angular方法
1. 原生事件中调用angular方法, 比如 input的onChange事件想调用angular里面定义的方法 - onChange="angular.element(this).sco ...
- vue 开发系列(八) 动态表单开发
概要 动态表单指的是我们的表单不是通过vue 组件一个个编写的,我们的表单是根据后端生成的vue模板,在前端通过vue构建出来的.主要的思路是,在后端生成vue的模板,前端通过ajax的方式加载后端的 ...
- Struts动态表单(DynamicForm)
动态表单的含义是不要手动定义,直接在配置文件中进行定义. 1.手动进行定义 <form-beans > <form-bean name="userForm" ty ...
随机推荐
- python 脚本转成exe可执行程序
本文所使用的工具是cx_Freeze,相对py2exe和PyInstaller来说,cx_Freeze的兼容性更好,支持最新的Python 3.X,支持PyQT,并且可以跨平台支持Windows和Li ...
- python网络编程——IO多路复用select/poll/epoll的使用
转载博客: http://www.haiyun.me/archives/1056.html http://www.cnblogs.com/coser/archive/2012/01/06/231521 ...
- iPhone获取手机里面所有的APP(私有库)+ 通过包名打开应用
1.获取到手机里面所有的APP包名 - (void)touss { Class lsawsc = objc_getClass("LSApplicationWorkspace"); ...
- Java并发之CyclicBarria的使用
Java并发之CyclicBarria的使用 一.简介 笔者在写CountDownLatch这个类的时候,看到了博客园上的<浅析Java中CountDownLatch用法>这篇博文,为博主 ...
- Web层辅助工具类
Java web开发中经常用到的一些方法: import java.io.BufferedReader; import java.net.InetAddress; import java.net.Un ...
- sql临时表的优点
1: 临时表来组织数据,更高效的查询速度. 2:临时表的操作不会写入日志文件:好处:提高了 临时表操作的速度:坏处: 数据一旦丢失,无法恢复. 3: 临时表只允许当前会话框进行访问,因此不会担心死锁 ...
- 基于Promise对象的新一代Ajax API--fetch
***************************************************************** #fetch Request 使用isomorphic-fetch发 ...
- Sybase:删除表中的某列
Sybases:删除表中的某列 alter table tb1(表名) drop clo1(列名); commit;
- Linux:redhat6.5使用yum时提示需要注册问题解决方案
Linux:redhat6.5使用yum时提示需要注册问题解决方案 一.问题 新安装了redhat6.5.安装后,登录系统,使用yum时候.提示: This system is not registe ...
- Java 关于assert
昨天在看NIO源码的时候,看到这样一句,顿时懵逼了 : assert (i >= 0); assert关键词我是有映像的, 中文叫断言.但是从来没有去了解过它的功能, 今天在感冒中抽出时间, 把 ...