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 ...
随机推荐
- CoreThink开发(十三)增加页面加载动画
效果: 加载动画是由jquery和fakeloader这个js库实现的. 其实这个也可以做成一个插件,用数据库记录是否开启,选择动画的样式,那样扩展性会更好. 源码资源已经上传在我的csdn下载中. ...
- 【算法题12 解码方法decode way】
1.来源LeetCode91 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请 ...
- Python 网络编程了解
阅读目录 一: 网络编程socket http://www.cnblogs.com/zhoujunhao/articles/7592671.html 二: TCP粘包处理 http://www.cnb ...
- python网络编程——SocketServer/Twisted/paramiko模块
在之前博客C/S架构的网络编程中,IO多路复用是将多个IO操作复用到1个服务端进程中进行处理,即无论有多少个客户端进行连接请求,服务端始终只有1个进程对客户端进行响应,这样的好处是节省了系统开销(se ...
- ie9下面的console的bug
摘自:http://blog.csdn.net/cdnight/article/details/51094464 ie9下面,很奇怪的是有console的代码有时候执行不下去,不过当f12打开控制台的 ...
- 【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作
include指令与include动作 1.样例代码 <%@ page contentType="text/html;charset=UTF-8" language=&quo ...
- Zabbix 自定义Key
系统:Linux Centos 7.4 x64.Windos 2008 x64 服务:Zabbix 3.0.16 说明1:自定义Key 主要通过自定义 脚本 或者 命令 来实现自定义监控类型,需要在a ...
- HTML5/CSS3实现图片倒影效果
在线演示 本地下载
- invalid derived query的解决办法
标签: eclipse / invalid / derived / 解决办法 / 校验功能 479 在Eclipse的运行过程中,突然有一个接口跳出如下错误: invalid derived quer ...
- javascript-实现小抽奖程序
直接上代码 <style> *{ margin: 0; padding:0;} .prize_wrap{ width: 300px; height: 150px; } .prize_wra ...