fix(仪表板): 修复反复点击树排序可能出现结果不一致问题

This commit is contained in:
wangjiahao 2024-03-21 09:38:41 +08:00
parent ad02177bd6
commit 696c4a7a2f
2 changed files with 10 additions and 6 deletions

View File

@ -1,23 +1,25 @@
import { BusiTreeNode } from '@/models/tree/TreeNode'
import _ from 'lodash'
export default function treeSort(tree: BusiTreeNode[], sortType: string, historySortType: string) {
export default function treeSort(tree: BusiTreeNode[], sortType: string) {
const result = _.cloneDeep(tree)
sortPer(result, sortType, historySortType)
sortPer(result, sortType)
_.forEach(result, node => {
if (node.children && node.children.length > 0) {
sortPer(node.children, sortType, historySortType)
sortPer(node.children, sortType)
}
})
return result
}
export const sortPer = (subTree: BusiTreeNode[], sortType: string, historySortType: string) => {
export const sortPer = (subTree: BusiTreeNode[], sortType: string) => {
if (sortType === 'name_desc') {
subTree.sort((a, b) => b.name.localeCompare(a.name, 'zh-Hans-CN', { sensitivity: 'accent' }))
} else if (sortType === 'name_asc') {
subTree.sort((a, b) => a.name.localeCompare(b.name, 'zh-Hans-CN', { sensitivity: 'accent' }))
} else if (sortType !== historySortType) {
} else if (sortType === 'time_asc') {
return subTree.reverse()
} else {
return subTree
}
}

View File

@ -60,6 +60,7 @@ const returnMounted = ref(false)
const state = reactive({
curSortType: 'time_desc',
resourceTree: [] as BusiTreeNode[],
originResourceTree: [] as BusiTreeNode[],
folderMenuList: [
{
label: '移动到',
@ -242,6 +243,7 @@ function flatTree(tree: BusiTreeNode[]) {
}
const afterTreeInit = () => {
state.originResourceTree = _.cloneDeep(state.resourceTree)
mounted.value = true
if (selectedNodeKey.value && returnMounted.value) {
expandedArray.value = getDefaultExpandedKeys()
@ -391,7 +393,7 @@ const getDefaultExpandedKeys = () => {
}
const sortTypeChange = sortType => {
state.resourceTree = treeSort(state.resourceTree, sortType, state.curSortType)
state.resourceTree = treeSort(state.originResourceTree, sortType)
state.curSortType = sortType
}