当前位置:网站首页>Antd treeselect gets the value of the parent node
Antd treeselect gets the value of the parent node
2022-07-26 09:31:00 【Bean sprouts don't eat beans】
stay antd Yes Treeselect Rendering of components ,onChange Events cannot get the value of the parent element , The official explanation is for performance , The parent element is not associated .
The end of the document also shows how to get the value of the parent element , The solution is : according to treeData The data structure uses recursive backtracking to find the value of the parent node
Be loyal to the document ~
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;
});
}
// Find the value of the parent node
function getPath(value) {
const path = [];
let current = valueMap[value];
while (current) {
path.unshift(current.value);
current = current.parent;
}
return path;
}
/** * Format tree structure */
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: " Beijing ",
districtsList:[],
};
componentDidMount() {
Post("district/treeList").then((res) => {
const districts = res.data[0].name;
getData.setDistricts(districts); // Save as global
this.setState(
{
districtsList: formatTree(
res.data,
function (z) {
return {
title: z.name,
value: z.name,
};
},
"childList",
0,
1
),
districts: districts,
},
() => {
// This is my other business logic Negligible
this.update();
const updateTime = process.env.REACT_APP_SITE_REFRESH_TIME;
this.timer = setInterval(() => {
this.update();
}, +updateTime * 1000);
// That's the point
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=" Please select the area "
treeDefaultExpandAll
onChange={
this.onChangeArea}
/>
);
}
}
边栏推荐
- 微信小程序开发
- Order based evaluation index (especially for recommendation system and multi label learning)
- antUI中a-modal 拖拽功能制作
- [MySQL] understand the important architecture of MySQL (I)
- 登录模块用例编写
- Windows下Redis哨兵模式搭建
- matlab中的AR模型短时预测交通流
- Solve "note: one or more layouts are missing the layout_width or layout_height attributes."
- 省政府召开全省高温天气安全防范工作电视电话会议
- 电机转速模糊pid控制
猜你喜欢
随机推荐
(一)面扫描仪与机械臂的手眼标定(眼在手上)
antd TreeSelect获取父节点的值
ZXing简化版,转载
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
Arc GIS basic operation 3
M-ary number STR to n-ary number
antUI中a-modal 拖拽功能制作
JS判断数据类型 Object.prototype.toString.call和typeof
多层嵌套后的 Fragment 懒加载实现
sublime 安装插件
JS 连等赋值操作
附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
wap端微信h5支付,用于非微信浏览器
PHP一次请求生命周期
会议OA项目(三)---我的会议(会议排座、送审)
[shutter -- layout] detailed explanation of the use of align, center and padding
选择器的使用
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
I'm faded
Qt随手笔记(三)在vs中使用QtCharts画折线图