10、Gantt 休息日部分 calendar

1、在甘特图中添加日历

 1 task.calendar_id = 'default';
 2 // adding a previously created calendar
 3 var calendarId = gantt.addCalendar(calendar);
 4  
 5 // adding a calendar with a new config
 6 var calendarId = gantt.addCalendar({
 7     id:"default", // optional
 8     worktime: {
 9         hours: ["8:00-17:00"],
10         days: [ 1, 1, 1, 1, 1, 1 ,1]
11     }
12 });
13  
14 var calendar = gantt.getCalendar(calendarId);
2、计算任务的结束日期
 1 gantt.config.work_time = true;
 2 gantt.init("gantt_here");
 3  
 4 // calculate the end date using global worktime settings
 5 gantt.calculateEndDate({start_date: new Date(2013,02,15), duration: 48});
 6 // or
 7 gantt.calculateEndDate(new Date(2013,02,15), 48);
 8  
 9 // calculate end date for a specific task calendar
10 gantt.calculateEndDate({start_date: new Date(2013,02,15), duration: 48, task:task});
11 // or, a short form:
12 // will use calendar currently assigned to a task, task.start_date and task.duration
13 gantt.calculateEndDate(task);
您还可以使用calculateEndDate方法计算开始日期
1 //calculate the start date:
2 task.start_date = gantt.calculateEndDate({
3     start_date: task.end_date,
4     duration: -task.duration
5 });
3、允许在工作时间而不是日历时间计算任务的持续时间
1 gantt.config.duration_unit = 'day'; //工期计算的基本单位 hour
2 gantt.config.duration_step = 1; //工期计算的倍数
3 gantt.config.work_time = true; // 允许在工作时间而不是日历时间计算任务的持续时间
4、指定将应用于时间轴区域单元格的 CSS 类
 1 gantt.templates.timeline_cell_class = function (task, date) {
 2   // console.log(gantt.isWorkTime({ date: date, task: task }), task, date);
 3   if (!gantt.isWorkTime({ date: date, task: task })) return 'weekend';
 4   return '';
 5 };
 6 
 7 .weekend {
 8   background: #f4f7f4;
 9 }
10 .gantt_selected .weekend {
11   background: rgba(191, 28, 45, 0.08);
12 }
 1 <Select
 2   mode="multiple"
 3   :maxTagCount="1"
 4   v-model:value="calendarTypeValue"
 5   style="width: 160px; margin-right: 10px"
 6   placeholder="请选择休息日"
 7   @change="handleCalendarTypeChange"
 8 >
 9   <Option
10     v-for="item in calendarTypeList"
11     :key="item.value"
12     :value="item.value"
13     :id="item.value"
14     :name="item.label"
15     :disabled="
16       calendarTypeValue.length > 5 &&
17       calendarTypeValue.indexOf(item.value + '') === -1
18     "
19   >
20     {{ item.label }}
21   </Option>
22 </Select>
 1 // 日历类型
 2 const calendarTypeList = ref([]);
 3 const calendarTypeValue = ref([]);
 4 
 5 
 6 /**
 7  * 选择日历类型
 8  * @param e
 9  * @param option
10  */
11 function handleCalendarTypeChange(e, option) {
12   console.log(e, option, e.length < 6);
13   renderCalendar();
14 }
15 
16 function renderCalendar() {
17   const ganttObj = gantt;
18   console.log('fjd===========================================', calendarTypeValue.value);
19   // 每天都是工作日
20   ganttObj.addCalendar({
21     id: 'default', // optional
22     worktime: {
23       days: [
24         calendarTypeValue.value.indexOf('7') == -1 ? 1 : 0,
25         calendarTypeValue.value.indexOf('1') == -1 ? 1 : 0,
26         calendarTypeValue.value.indexOf('2') == -1 ? 1 : 0,
27         calendarTypeValue.value.indexOf('3') == -1 ? 1 : 0,
28         calendarTypeValue.value.indexOf('4') == -1 ? 1 : 0,
29         calendarTypeValue.value.indexOf('5') == -1 ? 1 : 0,
30         calendarTypeValue.value.indexOf('6') == -1 ? 1 : 0,
31       ],
32     },
33   });
34   ganttObj.batchUpdate(function () {
35     ganttObj.eachTask(function (task) {
36       task.calendar_id = 'default';
37       console.log(task);
38       task.start_date = ganttObj.getClosestWorkTime({
39         dir: 'future',
40         date: task.start_date,
41         unit: ganttObj.config.duration_unit,
42         task: task,
43       });
44       task.end_date = ganttObj.calculateEndDate(task);
45       ganttObj.updateTask(task.id);
46     });
47   });
48 }

 

 
 

posted @ 2021-11-16 17:40  你在说啥  阅读(400)  评论(0)    收藏  举报