Skip to content

Commit 1ccf978

Browse files
committed
支持按开始日期自动排序
1 parent a9596a3 commit 1ccf978

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

src/components/GanttChart.vue

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const props = withDefaults(defineProps<Props>(), {
4545
afternoon: { start: 13, end: 17 },
4646
}),
4747
taskListConfig: undefined,
48+
autoSortByStartDate: false,
4849
})
4950
5051
const emit = defineEmits([
@@ -115,6 +116,8 @@ interface Props {
115116
}
116117
// 任务列表配置
117118
taskListConfig?: TaskListConfig
119+
// 是否启用自动排序(根据开始时间排序任务)
120+
autoSortByStartDate?: boolean
118121
}
119122
120123
// 使用taskListConfig中的默认宽度,如果未配置则使用320px
@@ -557,10 +560,54 @@ const tasksForTaskList = computed(() => {
557560
558561
// 添加原始任务数据(完全保持层级结构,不扁平化)
559562
if (props.tasks && props.tasks.length > 0) {
560-
result.push(...props.tasks)
561-
}
563+
// 根据配置决定是否排序
564+
if (props.autoSortByStartDate) {
565+
// 递归排序函数:根据实际开始时间排序
566+
const sortTasksByStartDate = (tasks: Task[]): Task[] => {
567+
return [...tasks]
568+
.map(task => {
569+
// 递归处理子任务
570+
const sortedTask = { ...task }
571+
if (task.children && task.children.length > 0) {
572+
sortedTask.children = sortTasksByStartDate(task.children)
573+
}
574+
return sortedTask
575+
})
576+
.sort((a, b) => {
577+
// 获取实际开始时间(考虑子任务的最早时间)
578+
const getEarliestStartDate = (task: Task): Date => {
579+
// 如果有子任务,找子任务中的最早时间
580+
if (task.children && task.children.length > 0) {
581+
const childDates = task.children
582+
.map(child => getEarliestStartDate(child))
583+
.filter(date => date.getTime() > 0) // 过滤无效日期
584+
585+
if (childDates.length > 0) {
586+
return new Date(Math.min(...childDates.map(d => d.getTime())))
587+
}
588+
}
589+
590+
// 没有子任务或子任务都没有时间,使用自身时间
591+
return task.startDate ? new Date(task.startDate) : new Date('9999-12-31')
592+
}
593+
594+
const dateA = getEarliestStartDate(a)
595+
const dateB = getEarliestStartDate(b)
596+
597+
// 按时间排序,时间相同时按ID排序
598+
const timeDiff = dateA.getTime() - dateB.getTime()
599+
return timeDiff !== 0 ? timeDiff : a.id - b.id
600+
})
601+
}
562602
563-
return result
603+
// 启用排序:对任务进行递归排序
604+
const sortedTasks = sortTasksByStartDate(props.tasks)
605+
result.push(...sortedTasks)
606+
} else {
607+
// 不排序:直接使用原始任务数据
608+
result.push(...props.tasks)
609+
}
610+
} return result
564611
})
565612
566613
// 为Timeline提供正确的扁平化数据

0 commit comments

Comments
 (0)