Unverified 提交 5ad93c60 作者: chengj 提交者: GitHub

feat(function): get selected tree node (#1857)

上级 ce7f382b
...@@ -119,6 +119,7 @@ ...@@ -119,6 +119,7 @@
getAllKeys, getAllKeys,
getChildrenKeys, getChildrenKeys,
getEnabledKeys, getEnabledKeys,
getSelectedNode,
} = useTree(treeDataRef, getFieldNames); } = useTree(treeDataRef, getFieldNames);
function getIcon(params: Recordable, icon?: string) { function getIcon(params: Recordable, icon?: string) {
...@@ -320,6 +321,7 @@ ...@@ -320,6 +321,7 @@
insertNodesByKey, insertNodesByKey,
deleteNodeByKey, deleteNodeByKey,
updateNodeByKey, updateNodeByKey,
getSelectedNode,
checkAll, checkAll,
expandAll, expandAll,
filterByLevel: (level: number) => { filterByLevel: (level: number) => {
......
...@@ -185,4 +185,9 @@ export interface TreeActionType { ...@@ -185,4 +185,9 @@ export interface TreeActionType {
updateNodeByKey: (key: string, node: Omit<TreeDataItem, 'key'>) => void; updateNodeByKey: (key: string, node: Omit<TreeDataItem, 'key'>) => void;
setSearchValue: (value: string) => void; setSearchValue: (value: string) => void;
getSearchValue: () => string; getSearchValue: () => string;
getSelectedNode: (
key: KeyType,
treeList?: TreeItem[],
selectNode?: TreeItem | null,
) => TreeItem | null;
} }
import type { InsertNodeParams, KeyType, FieldNames } from './tree'; import type { InsertNodeParams, KeyType, FieldNames, TreeItem } from './tree';
import type { Ref, ComputedRef } from 'vue'; import type { Ref, ComputedRef } from 'vue';
import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree'; import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
...@@ -176,6 +176,23 @@ export function useTree(treeDataRef: Ref<TreeDataItem[]>, getFieldNames: Compute ...@@ -176,6 +176,23 @@ export function useTree(treeDataRef: Ref<TreeDataItem[]>, getFieldNames: Compute
} }
} }
} }
// Get selected node
function getSelectedNode(key: KeyType, list?: TreeItem[], selectedNode?: TreeItem | null) {
if (!key && key !== 0) return null;
const treeData = list || unref(treeDataRef);
treeData.forEach((item) => {
if (selectedNode?.key || selectedNode?.key === 0) return selectedNode;
if (item.key === key) {
selectedNode = item;
return;
}
if (item.children && item.children.length) {
selectedNode = getSelectedNode(key, item.children, selectedNode);
}
});
return selectedNode || null;
}
return { return {
deleteNodeByKey, deleteNodeByKey,
insertNodeByKey, insertNodeByKey,
...@@ -185,5 +202,6 @@ export function useTree(treeDataRef: Ref<TreeDataItem[]>, getFieldNames: Compute ...@@ -185,5 +202,6 @@ export function useTree(treeDataRef: Ref<TreeDataItem[]>, getFieldNames: Compute
getAllKeys, getAllKeys,
getChildrenKeys, getChildrenKeys,
getEnabledKeys, getEnabledKeys,
getSelectedNode,
}; };
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button> <a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button>
<a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button> <a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button>
<a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button> <a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button>
<a-button @click="handleGetSelectNode" class="mr-2"> 获取选中节点 </a-button>
<a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button> <a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
<a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button> <a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
...@@ -69,6 +70,12 @@ ...@@ -69,6 +70,12 @@
createMessage.success(JSON.stringify(keys)); createMessage.success(JSON.stringify(keys));
} }
function handleGetSelectNode() {
const keys = getTree().getSelectedKeys();
const node = getTree().getSelectedNode(keys[0]);
createMessage.success(node !== null ? JSON.stringify(node) : null);
}
function handleSetExpandData() { function handleSetExpandData() {
getTree().setExpandedKeys(['0-0']); getTree().setExpandedKeys(['0-0']);
} }
...@@ -120,6 +127,7 @@ ...@@ -120,6 +127,7 @@
handleGetSelectData, handleGetSelectData,
handleSetExpandData, handleSetExpandData,
handleGetExpandData, handleGetExpandData,
handleGetSelectNode,
appendNodeByKey, appendNodeByKey,
deleteNodeByKey, deleteNodeByKey,
updateNodeByKey, updateNodeByKey,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论