node.js - 用react的for循環(huán)怎么實現(xiàn)重復輸入RadioButton
問題描述

我用了antdesign的插件,想實現(xiàn)從后臺獲取一個數(shù)據(jù)num,然后根據(jù)這個num循環(huán)n變,輸出單選框<RadioButton value='c'>張三</RadioButton>,但是控制臺直接報錯了ps.這是jsx頁面
[ERROR] parser.babel-5.x: /page/localExaminationPlan2/app/components/appointExpertPostForm.jsx: Unexpected token (125:22) [E:/mavenWorkspace/labsforce/FrontEnd/lcy/myProject/page/localExaminationPlan2/app/components/appointExpertPostForm.jsx]
我該怎么寫?
let formClass = React.createClass({ //注意處理提交表單,準備數(shù)據(jù)給ok action handleSubmit(e) { //表單校驗 console.log(’-----’); let validateResult = this.props.form.validateFieldsAndScroll((errors, values) => { if (!!errors) {console.log(’Errors in form!!!’);return; }else{let params = ObjectAssign(//將隱藏值一并提交,如父id this.props.appointExpertPostFormReduce.get(’fieldValues’).toJSON(), this.props.form.getFieldsValue());this.props.appointExpertPostFormAction.onOk( params, this.props.notificationAction, Notification, this.postFormCallBack); } }); }, //表單提交成功回調(diào) postFormCallBack(){ this.props.declarationDetailTableComponent.queryRecords(); },queryRecords(params={}){ //2.add order query 由table onFieldsChange 傳入?yún)?shù) let wheresParma = []; let queryParam = {}; let queryFormParams = {}; queryFormParams.wheres = wheresParma; queryParam.queryFormParams = queryFormParams; let paramsAssign = ObjectAssign(this.props.appointExpertPostFormReduce.get(’pagination’).toJSON(),queryParam,params); paramsAssign.queryFormParams = JSON.stringify(paramsAssign.queryFormParams); this.props.appointExpertPostFormAction.onShowPostForm(paramsAssign,this.props.notificationAction,Notification); }, render() { const {appointExpertPostFormReduce,appointExpertPostFormAction} = this.props; const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 27 }, }; return ( <p style={{display:'inline'}}><Modal visible={this.props.appointExpertPostFormReduce.get(’addModleVisible’)} confirmLoading={this.props.appointExpertPostFormReduce.get(’confirmLoading’)} width={583} maskClosable={false} footer={[ <Button style={{backGround:'#000'}} key='btn' type='ghost' size='large' onClick={this.props.appointExpertPostFormAction.onCancel}> 取 消 </Button>, <Button key='submit' type='primary' size='large' onClick={this.handleSubmit}> 確 定 </Button>, ]}> <Form horizontal> <p id='box'> <FormItem {...formItemLayout} label='選擇專家'> <RadioGroup defaultValue='a' size='large'><RadioButton value='a'>趙六</RadioButton><RadioButton value='b'>李四</RadioButton><RadioButton value='c'>王五</RadioButton><RadioButton value='d'>張三</RadioButton> </RadioGroup> </FormItem> </p></Form></Modal> </p> ); },});var PostForm = Form.create()(formClass);module.exports = PostForm;
問題解答
回答1:比較簡單的,在render函數(shù)里面定義一個array,然后for里面生成的東西依次push進去,最后顯示
render() { let shows = [] for (let i = 0; i < this.props.appointExpertPostFormReduce.num; i++){shows.push(<RadioButton value={/*具體value*/} key={i}>{/*具體顯示*/}</RadioButton>) }return ( <p style={{display:'inline'}}> //前面省略,貼代碼最好省略無關(guān)的內(nèi)容 <Form horizontal> <p id='box'> <FormItem {...formItemLayout} label='選擇專家'> <RadioGroup defaultValue='a' size='large'><RadioButton value='a'>趙六</RadioButton><RadioButton value='b'>李四</RadioButton><RadioButton value='c'>王五</RadioButton><RadioButton value='d'>張三</RadioButton>{shows} </RadioGroup> </FormItem></p> );
},});
回答2:JSX里的{}不能直接用for,而且值要出來不用再加花括號,這代碼有明顯的錯誤。
要像這樣才行,下面用箭頭函數(shù)包起來,直接用函數(shù)也行:
<p>{()=> { for(var i=1; i<10; i++){ <p>i</p> } }}</p>
不過一般如果到render中的return語句,應該要在這之前先求出值再進去加在花括號中。而不是在里面作運算。
回答3:var items = this.props.appointExpertPostFormReduce.map((item, i)=>{ return (<RadioButton></RadioButton>{/***/} ); });
相關(guān)文章:
1. PHPExcel表格導入數(shù)據(jù)庫怎么導入2. 預訂金和尾款分別支付3. thinkphp6使用驗證器 信息如何輸出到前端頁面4. javascript - h5微信中怎么禁止橫屏5. macos - 無法source activate python276. python - 調(diào)用api輸出頁面,會有標簽出現(xiàn),請問如何清掉?7. 運行python程序時出現(xiàn)“應用程序發(fā)生異常”的內(nèi)存錯誤?8. 我在導入模板資源時遇到無法顯示的問題,請老師解答下9. empty比isset更嚴格一點10. javascript - 微信網(wǎng)頁開發(fā)從菜單進入頁面后,按返回鍵沒有關(guān)閉瀏覽器而是刷新當前頁面,求解決?

網(wǎng)公網(wǎng)安備