網站首頁 編程語言 正文
react ui庫antd中form表單使用SelectTree反顯問題
最近遇到這個問題,后來找到原因
1.formItem 需要使用initialValue賦值。
2.這個組件需要一開始就存在不能是條件渲染,非要用條件渲染需要讓它先顯示,然后根據條件讓它不顯示。
例子:
state={
?treeList:[],
?showTree:ture,
?value:[]
}
componentDidMount(){
? ?//這里請求數據用定時器代替
? ?setTimeOut(()=>{ ?//一般會先拿到listTree先渲染
? ? ? this.setState({
? ? ? ? treeList:[{value:'aaa',title:'你好react'}]
? ? ? })
? ? ? setTimeOut(()=>{ //第二次請求反顯的值和是否顯示
? ? ? ? ?if(需要顯示tree){ //下面倆條if一般不會有同時出現的時候
? ? ? ? ? ? ?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表單里的數據,方法也不難 在鏈接處添加點擊事件 代碼如下(簡單記錄)
onClick={() => this.getInformation(info)}
//此處是點擊事件的方法
getInformation = (info) => {
//此處打印的東西見下圖
//此處存在問題如果強制轉換了一次重復轉換會報錯 參數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}/>
//父組件處接收參數 在render函數處接收
const form = this.props["putfile"];
此處為上面log打印的東西
打印的數據格式都是string類型的,對于select的多選string類型的屬性當然不滿足了,所以需要進行數據處理
//此處添加了 回顯實例 此代碼antd版本為 3 必填校驗已經實現直接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>
總結
原文鏈接:https://blog.csdn.net/weixin_37619109/article/details/89528730
相關推薦
- 2023-02-04 Android自定義view實現雪花特效實例代碼_Android
- 2022-07-24 C++算法學習之分支限界法的應用_C 語言
- 2023-07-02 Linux系統下如何實現修改主機名_Linux
- 2023-03-27 react使用.env文件管理全局變量的方法_React
- 2022-06-08 nacos項目啟動報錯:Connection refused: no further informa
- 2022-04-17 python中lambda匿名函數詳解_python
- 2022-04-18 css設置彈性布局讓中間內容寬度自適應
- 2022-10-01 Python利用Bokeh進行數據可視化的教程分享_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支