網(wǎng)站首頁 編程語言 正文
問題:
- 前一段時(shí)間使用antd的Tree組件的時(shí)候遇到一個(gè)問題:將
Tree
組件放在Form
表單的時(shí)候,想用initialValue
給Tree
的defaultExpandedKeys,defaultSelectedKeys和defaultCheckedKeys
賦初始值,已找到解決方法。
解決思路:
-
Tree
不是 form control,你不能直接把Tree
丟給getFieldDecorator
,需要把Tree
封裝下再丟給getFieldDecorator
。 - 注意
4.x
版本的antd不支持Form.create()()
方法。
代碼展示
1. 封裝Tree
export default class TreeDemo extends React.Component {
render() {
return (
<Tree
checkable
expandedKeys={['STATUS']}
checkedKeys={this.props.value}
onCheck={this.props.onChange}
>
<TreeNode title="全選" key="STATUS">
<TreeNode title="未成交" key="A"></TreeNode>
<TreeNode title="已成交" key="B"></TreeNode>
<TreeNode title="流拍" key="C"></TreeNode>
<TreeNode title="未來供應(yīng)" key="D"></TreeNode>
</TreeNode>
</Tree>
);
}
}
2. Form表單
import React from 'react';
import { toJS } from 'mobx';
import { observer } from "mobx-react";
import { Row, Col, Button, Form, Popconfirm, message, Checkbox, Tree } from 'antd';
const { Item } = Form
const { TreeNode } = Tree;
class Index extends React.Component {
constructor(props) {
super(props)
this.state = {checkList:['A']}
}
//確定
handleSubmit = e => {
e && e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
console.log(toJS(values));
if (!err) {
//驗(yàn)證無誤
console.log('驗(yàn)證無誤---');
this.props.state.getParcelList()
}
});
}
//土地狀態(tài)
onCheck = checkedKeys => {
//過濾掉父級的key
checkedKeys = checkedKeys.filter(item => item !== 'STATUS')
this.setState({checkList:checkedKeys})
};
render() {
const { getFieldDecorator } = this.props.form
return (
<div>
<Form layout="inline" autoComplete="off" onSubmit={this.handleSubmit} >
<Row>
<Col span={4}>demo</Col>
<Col span={20}>
<Item>
{
getFieldDecorator('demo', {
initialValue: this.state.checkList,
})
(
<TreeDemo onChange={this.onCheck} />
)}
</Item>
</Col>
</Row>
<Row type='flex' justify="center">
<Button type='primary' htmlType="submit">確定</Button>
</Row>
</Form>
</div>
)
}
}
export default Form.create({
onValuesChange(props, changeValues, allValues) {
// console.log(allValues, 'allValues');
}
})(Index);
原文鏈接:https://blog.csdn.net/weixin_44471622/article/details/106994055
相關(guān)推薦
- 2022-09-06 一文詳解Python如何優(yōu)雅地對數(shù)據(jù)進(jìn)行分組_python
- 2022-05-11 RestTemplate的post使用,token調(diào)用
- 2022-09-01 C++?OpenCV實(shí)戰(zhàn)之形狀識別_C 語言
- 2022-03-31 C語言中冒泡排序算法詳解_C 語言
- 2022-06-16 ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán)功能_實(shí)用技巧
- 2022-10-28 React?createElement方法使用原理分析介紹_React
- 2022-04-05 Python中hash加密簡介及使用方法_python
- 2024-07-18 redisson分布式鎖中waittime的設(shè)置
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 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錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支