Vite出勤行事曆

Fullcalendar官網Vue3

安裝fullcalendar/vue3@6.1.10

1
2
npm install @fullcalendar/vue3@6.1.10
npm install --save @fullcalendar/core@6.1.10 @fullcalendar/daygrid@6.1.10 @fullcalendar/interaction@6.1.10 @fullcalendar/timegrid@6.1.10 @fullcalendar/list@6.1.10

模糊搜尋

定義checkWorkDataCalendarSearch, 利用vue input 表單雙向綁定 v-model=”checkWorkDataCalendarSearch”
引入computed
邏輯:
定義一個object就是獲得的陣列=>
如果 雙向綁定的 搜尋input(checkWorkDataCalendarSearch.value)沒有值,return 定義的這個object
=>否則 filter 如果 title indexOf !=-1 (=-1就是沒有找到) return 過濾的 item; 否則 return 定義的這個object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const filterCheckWorkDataCalendar = computed(() =>{
let postList_array = scheduleCalendarLists.value;
console.log('scheduleCalendarLists.value',scheduleCalendarLists.value)
if(checkWorkDataCalendarSearch.value === '') {
return postList_array;
}
checkWorkDataCalendarSearch.value = checkWorkDataCalendarSearch.value.trim().toLowerCase();
postList_array = postList_array.filter(function (opt) {
if ( opt.title.toLowerCase().indexOf(checkWorkDataCalendarSearch.value) !== -1 )
{
return opt;
}
})
return postList_array;
});

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
<ul class="set_work_check">
<li v-for="(item,id) in setCheckModesMainMenu" :key="id">
<router-link
:to="item.href"
:class="{activePage: item.title===pageName}"> {{item.title}}</router-link>
</li>
</ul>
<div class="checkCalendar">
<!--搜尋-->
<div class="checkWorkData_means">
<div class="btns_group">

<!---模糊搜尋 綁定-->
<input v-model="checkWorkDataCalendarSearch"
placeholder="search" />
<i class="icon-ym icon-ym-search"></i>

</div>
</div>
<FullCalendar :options="calendarOptions"></FullCalendar>
</div>
</template>

<route lang="yaml">
meta:
layout: LayoutBack
requireAutd: true
</route>

<script setup lang="ts">
import axios from "axios";
import {ref,onMounted,computed} from 'vue';
import FullCalendar from '@fullcalendar/vue3';
import { CalendarOptions , EventClickArg } from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid';
import interactionPlugin from '@fullcalendar/interaction';
import timeGridPlugin from '@fullcalendar/timegrid';
import listPlugin from '@fullcalendar/list';
const getToken =localStorage.getItem("token");
const headers = {"Authorization": 'Bearer '+getToken};
const pageName=ref<string>('出勤行事曆');
interface menuType{
id:string,
title:string,
href:string,
}
const setCheckModesMainMenu=ref<menuType[]>([
{ id: 'setWorkCheckList', title: '出勤設定列表', href: '/admin/set_work_check' },
{ id: 'checkWorkCheck', title: '出勤打卡', href: '/admin/checkWorkCheck' },
{ id: 'checkWorkLists', title: '出勤列表', href: '/admin/checkWorkLists' },
{ id: 'checkCalendar', title: '出勤行事曆', href: '/admin/calendar' },
])
const checkWorkDataCalendarSearch =ref<string>('');
interface workCheckType{
_id:string,
workCheck_Id:string,
userId:string,
userName: string,
start: string,
onWorkStatus: true,
offWorkStatus: false,
title: string,
borderColor: string,
ip: string,
lat: number,
lng: number,
}
const scheduleCalendarLists=ref<workCheckType[]>([]);

//獲取數據
const getCalendar = async () => {
try{
const api=`${import.meta.env.VITE_API_URL}/auth/workChecks`;
const res = await axios.get(api,{ headers });
if(res.status===200){
/*重點 res.data.reverse()*/
if(localStorage.getItem('userName')==='user'){
scheduleCalendarLists.value = res.data;
calendarOptions.value.events=filterCheckWorkDataCalendar;
}else{
scheduleCalendarLists.value = res.data.filter(function (item: workCheckType) {
console.log(item,item.userName,item.userName===localStorage.getItem('userName'))
if (item.userName===localStorage.getItem('userName')){
return item;
}
});
calendarOptions.value.events=filterCheckWorkDataCalendar;
}


}
}
catch(err){
console.log(err)
}
};
//模糊搜尋
const filterCheckWorkDataCalendar = computed(() =>{
let postList_array = scheduleCalendarLists.value;
console.log('scheduleCalendarLists.value',scheduleCalendarLists.value)
if(checkWorkDataCalendarSearch.value === '') {
return postList_array;
}
checkWorkDataCalendarSearch.value = checkWorkDataCalendarSearch.value.trim().toLowerCase();
postList_array = postList_array.filter(function (opt) {
if ( opt.title.toLowerCase().indexOf(checkWorkDataCalendarSearch.value) !== -1 )
{
return opt;
}
})
return postList_array;
});
interface headerToolbarType{
left:string,
center:string,
right:string,
}
// web headerToolbar顯示方式
const webDisableHeaderToolbar=ref<headerToolbarType>({
left: 'prev',
center: 'title,dayGridMonth,timeGridWeek,timeGridDay',
right: 'next'
})
// mobile headerToolbar顯示方式
const mobileDisableHeaderToolbar=ref<headerToolbarType>({
left: 'prev',
center: 'title,dayGridMonth,listWeek,timeGridDay',
right: 'next'
})
//
const calendarOptions = ref<CalendarOptions>({
timeZone: 'Asia/Taipei',
locale:'zh-TW',
plugins: [dayGridPlugin, interactionPlugin, timeGridPlugin,listPlugin],
headerToolbar:window.screen.width < 767?mobileDisableHeaderToolbar:webDisableHeaderToolbar ,
//螢幕尺寸767以下顯示
initialView: window.screen.width < 767 ? 'listWeek' : 'dayGridMonth',
//高度設置
height: window.screen.width < 767 ? 800 : 800,
//*****初始化,新增事件初始化改為空值陣列,
// initialEvents: scheduleCalendarList.value,
events: [],
//時間設置
eventTimeFormat: {
hour: '2-digit',
minute: '2-digit',
hour12: false
},
//刪除事件
// eventClick: openDeleteEvent,
//新增事件
// select: addSchedule,
editable: false,//允許拖拉縮放
droppable: true, //允許將事情放到日曆上
selectable: true, //允許選擇
selectMirror: true,//允許選擇鏡像
dayMaxEvents: true,//允許一天最多事件時就顯示彈出窗口
weekends: true,//顯示週末
//eventsSet:handleEvents,
// drop: dropEvents;
// 更新遠程數據庫
// eventAdd:
// eventChange:
// eventRemove:
});
onMounted(()=>{
getCalendar();
})
</script>