IT/javascript JQUERY

Vue.js Table의 Th유동적으로 변경

하루콩콩 2024. 11. 16. 23:08
반응형

HTML : 태그 안에 WeekDays로 동적으로 렌더링 한다.
                <tr>
                    <th colspan="2">{{$parent.weekDays[0]}}</th>
                    <th colspan="2">{{$parent.weekDays[1]}}</th>
                    <th colspan="2">{{$parent.weekDays[2]}}</th>
                    <th colspan="2">{{$parent.weekDays[3]}}</th>
                    <th colspan="2">{{$parent.weekDays[4]}}</th>
                </tr>
 
Script 
            new Vue({ el: "#app", data() { },
            computed: {
                // 이번 주의 월요일부터 금요일까지의 날짜 배열을 반환
                weekDays() {
                    const days = [];
                    const today = new Date();
                    const dayOfWeek = today.getDay();
                    const monday = new Date(today); // 월요일 날짜를 계산
                    monday.setDate(today.getDate() - (dayOfWeek - 1)); // 월요일 구하기

                    for (let i = 0; i < 5; i++) {
                        const weekDay = new Date(monday);
                        weekDay.setDate(monday.getDate() + i); // 월요일부터 금요일까지 날짜 구하기
                        days.push(this.formatDate(weekDay)); // 포맷팅된 날짜를 배열에 추가
                    }

                    return days;
                },
            },
            methods: {
                // 날짜를 'MM/DD' 형식으로 포맷팅하는 함수
                formatDate(date) {
                    const month = date.getMonth() + 1; // 월은 0부터 시작하므로 1을 더해줌
                    const day = date.getDate();
                    return `${month}/${day}`;
                },
           },

반응형

'IT > javascript JQUERY' 카테고리의 다른 글

vue.js v-bind:class 활용  (0) 2024.11.22
Vue.js 쓰기 권한 주기  (0) 2024.11.18
AUIGrid 그리드 최하단에 건수 보이게 하기  (0) 2024.11.14
Vue.js 비동기 처리 await  (0) 2023.08.23
Vue.js 조건에 따른 CLASS 적용  (0) 2023.08.22