vue甘特图vxe-gantt如何设置日期轴显示为周模式

0 阅读1分钟

通过 taskViewConfig.scales 参数,可以灵活配置甘特图日期轴的显示层级。本文将介绍如何将日期轴显示为“月 + 周”模式,以及如何自定义每周的起始日期。

核心配置示例

要显示“月 + 第几周”的双层日期轴,只需在 scales 数组中同时配置 'month' 和 'week' 即可。

image

<template>
  <div>
    <vxe-gantt v-bind="ganttOptions"></vxe-gantt>
  </div>
</template>

<script setup>
import { reactive } from 'vue'

const ganttOptions = reactive({
  border: true,
  columnConfig: {
    resizable: true
  },
  taskViewConfig: {
    scales: ['month', 'week'],
    tableStyle: {
      width: 320
    }
  },
  columns: [
    { field: 'name', title: '任务名称' },
    { field: 'start', title: '开始时间' },
    { field: 'end', title: '结束时间' }
  ],
  data: [
    { id: 10001, name: 'A项目', start: '2026-01-01', end: '2026-01-09', progress: 3 },
    { id: 10002, name: '城市道路修理进度', start: '2026-01-05', end: '2026-01-14', progress: 10 },
    { id: 10003, name: 'B大工程', start: '2026-01-12', end: '2026-01-21', progress: 90 },
    { id: 10004, name: '超级大工程', start: '2026-01-18', end: '2026-01-30', progress: 15 }
  ]
})
</script>

指定周的起始日期

默认情况下,周的划分可能以周一或周日为起始,具体取决于组件语言环境。如果需要明确控制每周的起始日期,可以将 scales 中的项从字符串改为对象形式,并通过 startDay 参数指定。

国内项目通常将周一作为一周的起始,默认值为startDay: 1,也自定义设置 。

taskViewConfig: {
  scales: [
    { type: 'month' },
    { type: 'week', startDay: 0 }  // 0 = 周日,1 = 周一,以此类推
  ]
}

合理配置 scales 参数,可以让甘特图的日期轴更贴合项目管理中对时间周期的展示需求。