当前位置:网站首页>antd TreeSelect获取父节点的值
antd TreeSelect获取父节点的值
2022-07-26 09:30:00 【豆芽不吃豆】
在antd对Treeselect组件的渲染中,onChange事件是无法获取父元素的值的,官方解释是处于对性能的考虑,没有对父元素进行关联。
文档末尾也给出了如何获取父元素值的方法,解题思路是:根据treeData的数据结构利用递归回溯去查找父节点的值
忠于文档~
import React from "react";
import {
TreeSelect } from "antd";
import {
Post } from "../../api/index";
const valueMap = {
};
function loops(list, parent) {
return (list || []).map(({
children, value }) => {
const node = (valueMap[value] = {
parent,
value
});
node.children = loops(children, node);
return node;
});
}
// 查找父节点的值
function getPath(value) {
const path = [];
let current = valueMap[value];
while (current) {
path.unshift(current.value);
current = current.parent;
}
return path;
}
/** * 格式化树形机构 */
function formatTree(list = [], formatFun, childrenName = 'children', index = 0, dep = -1) {
return list.map((z, i) => {
const hasChildren = !!(z[childrenName] || []).length ;
let c = null;
if (dep === - 1 || index < dep) {
c = hasChildren ? formatTree(z[childrenName] || [], formatFun, childrenName, index + 1, dep) : null;
console.log(111, z.name, index < dep, c)
}
//c = hasChildren ? formatTree(z[childrenName] || [], formatFun, childrenName, index + 1) : null;
return {
...formatFun(z, i),
hasChildren,
level: index,
children: c,
};
});
}
class Test extends React.PureComponent {
state = {
districts: "北京",
districtsList:[],
};
componentDidMount() {
Post("district/treeList").then((res) => {
const districts = res.data[0].name;
getData.setDistricts(districts); // 保存为全局
this.setState(
{
districtsList: formatTree(
res.data,
function (z) {
return {
title: z.name,
value: z.name,
};
},
"childList",
0,
1
),
districts: districts,
},
() => {
// 这是我自己其他的业务逻辑 可忽略
this.update();
const updateTime = process.env.REACT_APP_SITE_REFRESH_TIME;
this.timer = setInterval(() => {
this.update();
}, +updateTime * 1000);
// 这才是重点
loops(this.state.districtsList)
}
);
});
}
onChangeArea = value => {
console.log(getPath(value));
const new_district = getPath(value).join('-');
getData.setDistricts(new_district);
this.setState(
{
districts: value,
}
);
};
render() {
const {
districts,districtsList} = this.state;
return (
<TreeSelect
showSearch
dropdownMatchSelectWidth={
300}
size="small"
value={
districts}
treeData={
districtsList}
placeholder="请选择区域"
treeDefaultExpandAll
onChange={
this.onChangeArea}
/>
);
}
}
边栏推荐
猜你喜欢

高斯消元

arc-gis基础操作3

Paper notes: knowledge map kgat (unfinished temporary storage)

电机转速模糊pid控制

arc-gis的基本使用2

Selection and practice of distributed tracking system

What are CSDN spaces represented by

Fiddler下载安装

微信小程序学习笔记1

2022 mobile crane driver test question simulation test question bank simulation test platform operation
随机推荐
您的登录IP不在管理员配置的登录掩码范围内
Force button list question
【Flutter -- 布局】Align、Center、Padding 使用详解
大二上第一周学习笔记
MySQL transaction
PMM(Percona Monitoring and Management )安装记录
Basic use of ArcGIS 1
Cat installation and use
MySql5.7.25源码安装记录
设置视图动态图片
Process32First返回false,错误x信息24
什么是异步操作
配置ADCS后访问certsrv的问题
Personality test system V1.0
微信小程序开发
[arkit, realitykit] turn pictures into 3D models
V-for dynamically sets the SRC of img
神经网络与深度学习-6- 支持向量机1 -PyTorch
[MySQL] understand the important architecture of MySQL (I)
Audio and video knowledge