日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

react antd 动态表单

發(fā)布時(shí)間:2025/3/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 react antd 动态表单 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { Form, InputNumber, Input, DatePicker, Button, Select } from 'antd'; import moment from 'moment'; // 推薦在入口文件全局設(shè)置 locale import 'moment/locale/zh-cn'; moment.locale('zh-cn');const FormItem = Form.Item; const Option = Select.Option;// 后臺(tái)返回的數(shù)據(jù)格式 const data = [{'field': 'jobid','text': '工號(hào)','errorMessage': '請(qǐng)輸入工號(hào)','required': true,'type': 'int','value': 100},{'field': 'date','text': '日期','errorMessage': '請(qǐng)輸入日期','required': false,'type': 'date','value': '2017-10-20'},{'field': 'username','text': '用戶(hù)名','errorMessage': '請(qǐng)輸入用戶(hù)名','required': true,'type': 'char','value': 'hello world'},{'field': 'customer','text': '客戶(hù)','errorMessage': '請(qǐng)輸入客戶(hù)','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);}});}/*** 根據(jù)后臺(tái)返回的 data 中 type 類(lèi)型生成不同的組件* @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 日期格式需要強(qiáng)制轉(zhuǎn)化為 moment 格式item.value = item.type == 'date' ? moment(item.value, 'YYYY-MM-DD') : item.value;return (<FormItemkey={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 undefinedat 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);}

?

轉(zhuǎn)載于:https://www.cnblogs.com/Nyan-Workflow-FC/p/8609477.html

總結(jié)

以上是生活随笔為你收集整理的react antd 动态表单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。