網(wǎng)站首頁 編程語言 正文
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) => {
//此處打印的東西見下圖
//此處存在問題如果強制轉(zhuǎn)換了一次重復(fù)轉(zhuǎn)換會報錯 參數(shù)undefined
if (
typeof info.app == "string" &&
typeof info.file == "string"
)
{
console.log(info);
//把select多選的類型強制轉(zhuǎn)成object類型并且賦值給原來的屬性
var newObj1 = eval('('+info.app+')');
var newObj = eval('('+info.file+')');
info.file=newObj
info.app=newObj1
store.getInformation();
//儲存到當(dāng)前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類型的屬性當(dāng)然不滿足了,所以需要進行數(shù)據(jù)處理
//此處添加了 回顯實例 此代碼antd版本為 3 必填校驗已經(jīng)實現(xiàn)直接cv即可
//此處為input框
<Form.Item
label="應(yīng)用名稱"
// hasFeedback
required
>
{getFieldDecorator(
"name",
{ initialValue: form["name"] },
{
rules: [{ required: true, message: "請輸入應(yīng)用名稱!" }],
}
)(<Input placeholder="請輸入應(yīng)用名稱" />)}
</Form.Item>
// 此處為number框
<Form.Item label="金額" required>
{getFieldDecorator(
"money",
{ initialValue: form["money"] },
{
rules: [{ required: true, message: "請輸入應(yīng)用上架金額!" }],
}
)(
<InputNumber
style={{ width: "100%" }}
min="0"
step="1"
precision="2"
placeholder="請輸入應(yīng)用上架金額"
/>
)}
</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>
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_37619109/article/details/89528730
相關(guān)推薦
- 2022-12-05 python?os.stat()如何獲取相關(guān)文件的系統(tǒng)狀態(tài)信息_python
- 2023-05-30 Jquery使用原生AJAX方法請求數(shù)據(jù)_jquery
- 2022-12-12 C++?Boost?Chrono實現(xiàn)計時碼表流程詳解_C 語言
- 2022-06-21 Android?Studio實現(xiàn)下拉列表效果_Android
- 2022-04-24 詳解golang?定時任務(wù)time.Sleep和time.Tick實現(xiàn)結(jié)果比較_Golang
- 2022-09-22 stack和queue的模擬實現(xiàn)
- 2024-02-17 阻塞IO、非阻塞IO、IO多路復(fù)用、AIO的區(qū)別
- 2024-04-08 Spring在多線程環(huán)境下如何確保事務(wù)一致性
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支