網(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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-17 R語言UpSet包實現(xiàn)集合可視化示例詳解_R語言
- 2022-11-16 Python?讀取?.gz?文件全過程_python
- 2022-07-26 Android自定義評分控件的完整實例_Android
- 2023-11-17 Python如何使用matlibplot繪制3D柱形圖
- 2022-12-13 C語言MFC導出dll回調函數(shù)方法詳解_C 語言
- 2023-07-26 vite中的glob-import批量導入
- 2022-09-02 ORACLE正則匹配查詢LIKE查詢多個值檢索數(shù)據(jù)庫對象_oracle
- 2023-03-27 詳解C++11中的類型推斷_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支