網(wǎng)站首頁 編程語言 正文
import { Tree } from 'antd';
import { useState } from 'react';
let findResult;
const treeList = [
{
title: 'parent 1',
key: '0-0',
children: [
{
title: 'parent 1-0',
key: '0-0-0',
disabled: true,
children: [
{
title: 'leaf',
key: '0-0-0-0',
disableCheckbox: true,
},
{
title: 'leaf',
key: '0-0-0-1',
},
],
},
{
title: 'parent 1-1',
key: '0-0-1',
children: [{ title: <span style={{ color: '#1890ff' }}>sss</span>, key: '0-0-1-0' }],
},
],
},
];
const findItem = (data, node, parentItem) => {
data.find((item) => {
if (item?.key === node?.key) {
if (parentItem) {
item.parent = parentItem;
return (findResult = item);
} else return (findResult = item);
} else if (item?.children?.length) {
return (findResult = findItem(item.children, node, item));
}
});
return findResult;
},
// dropPosition -1是移動到和他平級在他上面 1是移動到和他平級在他下面 0是移動到他下面作為他子集
dealDrap = (dragNode, node, treeData, dropPosition) => {
let dragNodeResult, nodeResult;
findResult = '';
dragNodeResult = findItem(treeData, dragNode);
findResult = '';
nodeResult = findItem(treeData, node);
// 0是移動到他下面作為他子集
if (dropPosition === 0) {
if (dragNodeResult?.parent?.key === nodeResult?.key) return true;
return false;
}
// -1是移動到和他平級在他上面 1是移動到和他平級在他下面
if (dropPosition === 1 || dropPosition === -1) {
// 都有父
if (dragNodeResult?.parent && nodeResult?.parent) {
// 父相等
if (dragNodeResult?.parent?.key == nodeResult?.parent?.key) {
return true;
} else {
return false;
}
}
//有父無父
if (dragNodeResult?.parent && !nodeResult?.parent) {
return false;
}
// 無父有父
if (!dragNodeResult?.parent && nodeResult?.parent) {
return false;
}
if (!dragNodeResult?.parent && !nodeResult?.parent) {
return true;
}
}
};
// canRandom true可以隨意拖動 false只限平級拖動
const TreeTuo = ({ canRandom }) => {
const [treeData, setTreeData] = useState(treeList),
// 官方提供的方法可以隨意拖動
onDrop = info => {
const dropKey = info.node.key;
const dragKey = info.dragNode.key;
const dropPos = info.node.pos.split('-');
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
// 實現(xiàn)平級拖動的限制
if (!canRandom) {
const dealData = JSON.parse(JSON.stringify(treeData));
const result = dealDrap(info.dragNode, info.node, [...dealData], dropPosition);
if (!result) return;
}
const loop = (data, key, callback) => {
for (let i = 0; i < data.length; i++) {
if (data[i].key === key) {
return callback(data[i], i, data);
}
if (data[i].children) {
loop(data[i].children, key, callback);
}
}
};
const data = [...treeData];
// Find dragObject
let dragObj;
loop(data, dragKey, (item, index, arr) => {
arr.splice(index, 1);
dragObj = item;
});
if (!info.dropToGap) {
// Drop on the content
loop(data, dropKey, item => {
item.children = item.children || [];
// where to insert 示例添加到頭部,可以是隨意位置
item.children.unshift(dragObj);
});
} else if (
(info.node.props.children || []).length > 0 && // Has children
info.node.props.expanded && // Is expanded
dropPosition === 1 // On the bottom gap
) {
loop(data, dropKey, item => {
item.children = item.children || [];
// where to insert 示例添加到頭部,可以是隨意位置
item.children.unshift(dragObj);
// in previous version, we use item.children.push(dragObj) to insert the
// item to the tail of the children
});
} else {
let ar;
let i;
loop(data, dropKey, (item, index, arr) => {
ar = arr;
i = index;
});
if (dropPosition === -1) {
ar.splice(i, 0, dragObj);
} else {
ar.splice(i + 1, 0, dragObj);
}
}
setTreeData(data);
};
return <Tree draggable blockNode onDrop={onDrop} treeData={treeData} />;
};
export default TreeTuo;
原文鏈接:https://blog.csdn.net/weixin_44147791/article/details/124084064
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2023-04-02 攔截信號Golang應(yīng)用優(yōu)雅關(guān)閉的操作方法_Golang
- 2023-01-12 Android?RecyclerChart其它圖表繪制示例詳解_Android
- 2022-10-29 C#?CLR學(xué)習(xí)?C++使用namespace實例詳解_C 語言
- 2022-04-25 C++特殊成員函數(shù)以及其生成機制詳解_C 語言
- 2022-08-12 Android學(xué)習(xí)之BottomSheetDialog組件的使用_Android
- 2022-10-12 Nginx?403?forbidden錯誤的原因以及解決方法_nginx
- 2022-07-18 RabbitMQ集群負(fù)載均衡使用Haproxy的原因
- 2022-03-05 CentOS系統(tǒng)下安裝及配置JDK介紹_Linux
- 欄目分類
-
- 最近更新
-
- 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)程分支