Vue로 PWA 개발 - 그랜파 개발자
일별 조회 현황 기능을 구현하기 위하여 먼저 기간을 입력하여 날짜를 받아오는 기능을 구현합니다.
To create a Vue.js component that allows users to input a period (start date and end date) and then displays the dates within that period, you can follow these steps.
사용자가 기간(시작 날짜 및 종료 날짜)을 입력한 다음 해당 기간 내의 날짜를 표시할 수 있는 Vue.js 구성 요소를 만들려면 다음 단계를 따르세요.
Here's how to do it using Vuetify for styling and Vue.js for functionality.
스타일링을 위해 Vuetify를 사용하고 기능을 위해 Vue.js를 사용하여 이를 수행하는 방법은 다음과 같습니다.
<template>
<v-container>
<v-row class="mx-0">
<v-col cols="12" md="6">
<!-- Start Date Input -->
<v-text-field
label="Start Date"
v-model="startDate"
type="date"
></v-text-field>
</v-col>
<v-col cols="12" md="6">
<!-- End Date Input -->
<v-text-field
label="End Date"
v-model="endDate"
type="date"
></v-text-field>
</v-col>
</v-row>
<v-btn @click="calculateDates">Calculate Dates</v-btn>
<v-divider class="my-5"></v-divider>
<v-list>
<!-- List of Dates -->
<v-list-item-group>
<v-list-item v-for="(date, index) in dateList" :key="index">
<v-list-item-content>
<v-list-item-title>{{ date }}</v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-container>
</template>
<script>
export default {
data() {
return {
startDate: "", // User-input start date
endDate: "", // User-input end date
dateList: [] // List of dates to display
};
},
methods: {
calculateDates() {
// Ensure the start and end dates are valid before calculating
if (!this.startDate || !this.endDate || new Date(this.startDate) > new Date(this.endDate)) {
this.dateList = [];
return;
}
// Call the function to get the list of dates between the start and end date
this.dateList = this.getDatesInRange(this.startDate, this.endDate);
},
// Function to calculate dates between startDate and endDate
getDatesInRange(startDate, endDate) {
const start = new Date(startDate);
const end = new Date(endDate);
const dateArray = [];
let currentDate = start;
// Loop to generate dates between start and end date
while (currentDate <= end) {
dateArray.push(new Date(currentDate).toISOString().split("T")[0]);
currentDate.setDate(currentDate.getDate() + 1);
}
return dateArray;
}
}
};
</script>
<style scoped>
.v-btn {
margin-top: 20px;
}
</style>
When a user inputs: 사용자가 입력할 때:
The component will display: 구성 요소에 다음이 표시됩니다.
2024-10-01
2024-10-02
2024-10-03
2024-10-04
2024-10-05
Make sure you have Vuetify installed in your project to use the <v-text-field>, <v-btn>, and other components.
<v-text-field>, <v-btn> 및 기타 구성요소를 사용하려면 프로젝트에 Vuetify가 설치되어 있는지 확인하세요.
This component ensures the user selects a valid date range and shows the list of dates within that range dynamically.
이 구성요소는 사용자가 유효한 날짜 범위를 선택하도록 하고 해당 범위 내의 날짜 목록을 동적으로 표시합니다.
This will allow users to input a period, calculate the dates, and see them displayed in real-time.
이를 통해 사용자는 기간을 입력하고 날짜를 계산하여 실시간으로 표시되는 것을 볼 수 있습니다.