| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <template>
- <main>
- <TopVisual :className="className" :title="title" :navigation="navigation" />
- <section class="notice--section">
- <div class="sub--container type2">
- <div class="title--wrap">
- <h2 class="title">공지사항</h2>
- </div>
- <div class="search--wrap">
- <USelect v-model="searchValue" :items="searchItems" />
- <UInput
- v-model="searchKeyword"
- placeholder="검색어를 입력해주세요."
- @keyup.enter="performSearch"
- />
- <UButton @click="performSearch" class="search--btn"></UButton>
- </div>
- <div class="notice--wrap">
- <div class="notice--list">
- <NuxtLink
- v-for="news in paginatedNews"
- :key="news.id"
- :to="news.link"
- class="notice"
- >
- <span class="news--index">{{ news.id }}</span>
- <h4>{{ news.title }}</h4>
- <span class="news--date">{{ news.date }}</span>
- </NuxtLink>
- </div>
- <div class="pagination--wrap">
- <UButton
- @click="prevPage"
- class="prev--btn"
- :disabled="currentPage === 1"
- ></UButton>
- <div class="numbs">
- <UButton
- v-for="page in totalPages"
- :key="page"
- @click="goToPage(page)"
- :class="{ active: currentPage === page }"
- >
- {{ page }}
- </UButton>
- </div>
- <UButton
- @click="nextPage"
- class="next--btn"
- :disabled="currentPage === totalPages"
- ></UButton>
- </div>
- </div>
- </div>
- </section>
- </main>
- </template>
- <script setup>
- import { ref, computed, onMounted } from "vue";
- import TopVisual from "~/components/topVisual.vue";
- const searchItems = ref(["제목", "내용", "제목+내용"]);
- const searchValue = ref("제목");
- const searchKeyword = ref("");
- const totalCount = ref(0);
- const loading = ref(true);
- const className = ref("contact");
- const title = ref("Contact");
- const navigation = ref([
- {
- name: "Contact",
- link: "/contact/notice",
- gnbList: [
- { name: "Company", link: "/company/intro" },
- { name: "Product", link: "/products/materials" },
- { name: "Technology", link: "/technology/facilities" },
- { name: "Media", link: "/media/news" },
- { name: "Contact", link: "/contact/notice" },
- ],
- },
- {
- name: "공지사항",
- link: "/contact/notice",
- gnbList: [
- { name: "공지사항", link: "/contact/notice" },
- { name: "FAQ", link: "/contact/faq" },
- { name: "고객센터", link: "/contact/support" },
- { name: "오시는길", link: "/contact/location" },
- ],
- },
- ]);
- // 뉴스 데이터 배열 - API에서 받아올 것
- const newsData = ref([]);
- // 검색 실행 함수
- const performSearch = async () => {
- currentPage.value = 1; // 검색시 첫 페이지로 이동
- await fetchNoticeList(1);
- };
- // 검색 초기화 함수
- const resetSearch = async () => {
- searchKeyword.value = "";
- searchValue.value = "제목";
- currentPage.value = 1;
- await fetchNoticeList(1);
- };
- // API에서 공지사항 데이터 가져오기
- const fetchNoticeList = async (page = 1) => {
- try {
- loading.value = true;
- // 검색 종류 매핑
- const getSearchKind = (searchType) => {
- switch (searchType) {
- case "제목": return "title";
- case "내용": return "contents";
- case "제목+내용": return "title_contents";
- default: return "title";
- }
- };
- // CodeIgniter 방식으로 호출
- const response = await $postForm(`/board_list/notice`, {
- page: page,
- searchKind: getSearchKind(searchValue.value),
- searchKeyword: searchKeyword.value || "",
- });
- // 백엔드가 JSON으로 응답하는지 확인
- if (response && typeof response === "object") {
- // JSON 응답인 경우
- if (response.success && response.list) {
- // 전체 개수와 현재 페이지를 기준으로 번호 계산
- totalCount.value = response.totalCount || 0;
- const currentPageNum = page || 1;
- const pageSize = 20; // 백엔드의 페이지 사이즈와 동일
- newsData.value = response.list.map((item, index) => {
- // 번호 = 전체개수 - ((현재페이지-1) * 페이지크기 + 인덱스)
- const displayNumber = totalCount.value - ((currentPageNum - 1) * pageSize + index);
- return {
- id: displayNumber, // 순차적인 번호로 표시
- title: item.title,
- date: item.regdate,
- link: `/contact/noticeView?idx=${item.board_idx}`, // 실제 링크는 board_idx 사용
- };
- });
- } else {
- console.error("JSON 응답 형식이 올바르지 않습니다:", response);
- newsData.value = [];
- }
- } else if (typeof response === "string") {
- // HTML 응답인 경우 (백엔드에서 AJAX 감지 실패시)
- console.warn("HTML 응답을 받았습니다. AJAX 감지가 실패했을 수 있습니다.");
- console.log("HTML 내용:", response.substring(0, 200) + "...");
- newsData.value = [];
- } else {
- console.error("예상하지 못한 응답 형식:", typeof response, response);
- newsData.value = [];
- }
- } catch (error) {
- console.error("공지사항 데이터 로드 실패:", error);
- // 에러시 기본 더미 데이터 사용
- } finally {
- loading.value = false;
- }
- };
- // 페이지네이션 로직
- const currentPage = ref(1);
- const itemsPerPage = 10;
- const backendPageSize = 20;
- const totalPages = computed(() => Math.ceil(totalCount.value / itemsPerPage));
- const paginatedNews = computed(() => {
- const start = (currentPage.value - 1) * itemsPerPage;
- const end = start + itemsPerPage;
- return newsData.value.slice(start, end);
- });
- // 백엔드에서 필요한 데이터가 있는지 확인하고 필요시 API 호출
- const needToFetchData = (targetPage) => {
- const startIndex = (targetPage - 1) * itemsPerPage;
- const endIndex = startIndex + itemsPerPage - 1;
- return endIndex >= newsData.value.length && newsData.value.length < totalCount.value;
- };
- const goToPage = async (page) => {
- if (page >= 1 && page <= totalPages.value) {
- currentPage.value = page;
-
- if (needToFetchData(page)) {
- // 백엔드 페이지 계산: 프론트 페이지를 백엔드 페이지로 변환
- const backendPage = Math.ceil((page * itemsPerPage) / backendPageSize);
- await fetchNoticeList(backendPage);
- }
- }
- };
- const nextPage = async () => {
- if (currentPage.value < totalPages.value) {
- await goToPage(currentPage.value + 1);
- }
- };
- const prevPage = async () => {
- if (currentPage.value > 1) {
- await goToPage(currentPage.value - 1);
- }
- };
- // 컴포넌트 마운트 시 데이터 로드
- onMounted(() => {
- fetchNoticeList(1);
- });
- </script>
|