react的ui庫antd中form表單使用SelectTree反顯問題及解決
react ui庫antd中form表單使用SelectTree反顯問題
最近遇到這個問題,后來找到原因
1.formItem 需要使用initialValue賦值。
2.這個組件需要一開始就存在不能是條件渲染,非要用條件渲染需要讓它先顯示,然后根據(jù)條件讓它不顯示。
例子:
state={ ?treeList:[], ?showTree:ture, ?value:[] } componentDidMount(){ ? ?//這里請求數(shù)據(jù)用定時器代替 ? ?setTimeOut(()=>{ ?//一般會先拿到listTree先渲染 ? ? ? this.setState({ ? ? ? ? treeList:[{value:'aaa',title:'你好react'}] ? ? ? }) ? ? ? setTimeOut(()=>{ //第二次請求反顯的值和是否顯示 ? ? ? ? ?if(需要顯示tree){ //下面?zhèn)z條if一般不會有同時出現(xiàn)的時候 ? ? ? ? ? ? ?this.setState({ ? ? ? ? ? ? ? ?value:['aaa'], ? ? ? ? ? ? ? ?})? ? ? ? ? ?} ? ? ? ? ?if(不需要顯示tree){ ? ? ? ? ? ? this.setState({ ? ? ? ? ? ? ? ?showTree:false, ? ? ? ? ? ? ? ?})? ? ? ? ? ?} ? ? ? ?? ? ? ? }) ? ?},2000) } ?render() { ? ? const {treeList,value,showTree}=this.state; ? ? const tProps = { ? ? ? treeData:treeList, ? ? ? value:value ? ? }; ? ? return? ? ? <Form> ? ? ? <Form.Item> ? ? ? ? ? {getFieldDecorator('selectTree', { ? ? ? ? ? ? initialValue:[] ? ? ? ? ? })( ? ? ? ? ? ? ?{showTree&&<TreeSelect {...tProps} />} ? ? ? ? ? )} ? ? ? ? </Form.Item> ? ? ?</Form> ? ? ? ? }
react antd form表單回顯踩坑
需求如下
在彈窗里顯示一個表單,并進行回顯,涉及到的組件有,簡單的input框,inputNumber框,select,此處前端懶得讓后端寫接口點擊自己獲取到form表單里的數(shù)據(jù),方法也不難 在鏈接處添加點擊事件 代碼如下(簡單記錄)
onClick={() => this.getInformation(info)} //此處是點擊事件的方法 getInformation = (info) => { //此處打印的東西見下圖 //此處存在問題如果強制轉換了一次重復轉換會報錯 參數(shù)undefined if ( typeof info.app == "string" && typeof info.file == "string" ) { console.log(info); //把select多選的類型強制轉成object類型并且賦值給原來的屬性 var newObj1 = eval('('+info.app+')'); var newObj = eval('('+info.file+')'); info.file=newObj info.app=newObj1 store.getInformation(); //儲存到當前state中 this.setState({getInform:info}) }else{ this.setState({getInform:info}) } }; //此處通過組件通信暴露給父組件 <Get putfile={this.state.getInform}/> //父組件處接收參數(shù) 在render函數(shù)處接收 const form = this.props["putfile"];
此處為上面log打印的東西
打印的數(shù)據(jù)格式都是string類型的,對于select的多選string類型的屬性當然不滿足了,所以需要進行數(shù)據(jù)處理
//此處添加了 回顯實例 此代碼antd版本為 3 必填校驗已經(jīng)實現(xiàn)直接cv即可 //此處為input框 <Form.Item label="應用名稱" // hasFeedback required > {getFieldDecorator( "name", { initialValue: form["name"] }, { rules: [{ required: true, message: "請輸入應用名稱!" }], } )(<Input placeholder="請輸入應用名稱" />)} </Form.Item> // 此處為number框 <Form.Item label="金額" required> {getFieldDecorator( "money", { initialValue: form["money"] }, { rules: [{ required: true, message: "請輸入應用上架金額!" }], } )( <InputNumber style={{ width: "100%" }} min="0" step="1" precision="2" placeholder="請輸入應用上架金額" /> )} </Form.Item> // 此處為select多選框 (此處未做必填校驗) <Col span={12}> <Form.Item label="文件類型" hasFeedback validateStatus=""> {getFieldDecorator("file", { initialValue: form["file"], })( <Select mode="tags" style={{ width: "100%" }} placeholder="請選擇文件類型" onChange={this.handleChange} > {children} </Select> )} </Form.Item> </Col>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
react 頁面加載完成后自動執(zhí)行標簽的點擊事件的兩種操作方法
這篇文章主要介紹了react 頁面加載完成后自動執(zhí)行標簽的點擊事件,本文給大家分享兩種操作方法結合示例代碼給大家講解的非常詳細,需要的朋友可以參考下2022-12-12詳解使用create-react-app添加css modules、sasss和antd
這篇文章主要介紹了詳解使用create-react-app添加css modules、sasss和antd,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Native?Memory?Tracking追蹤區(qū)域示例分析
這篇文章主要為大家介紹了Native?Memory?Tracking追蹤區(qū)域示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11