list.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div class="admin--branch-list">
  3. <!-- 테이블 -->
  4. <div class="admin--table-wrapper">
  5. <table class="admin--table">
  6. <thead>
  7. <tr>
  8. <th>NO</th>
  9. <th>지점명</th>
  10. <th>대표번호</th>
  11. <th>주소</th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. <tr v-if="isLoading">
  16. <td colspan="4" class="admin--table-loading">
  17. 데이터를 불러오는 중...
  18. </td>
  19. </tr>
  20. <tr v-else-if="!branches || branches.length === 0">
  21. <td colspan="4" class="admin--table-empty">
  22. 등록된 지점이 없습니다.
  23. </td>
  24. </tr>
  25. <tr v-else v-for="(branch, index) in branches" :key="branch.id">
  26. <td>{{ totalCount - ((currentPage - 1) * perPage + index) }}</td>
  27. <td class="admin--table-title">{{ branch.name }}</td>
  28. <td>{{ branch.phone }}</td>
  29. <td>{{ branch.address }}</td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. </div>
  34. <!-- 페이지네이션 -->
  35. <div v-if="totalPages > 1" class="admin--pagination">
  36. <button
  37. class="admin--pagination-btn"
  38. :disabled="currentPage === 1"
  39. @click="changePage(currentPage - 1)"
  40. >
  41. 이전
  42. </button>
  43. <button
  44. v-for="page in visiblePages"
  45. :key="page"
  46. class="admin--pagination-btn"
  47. :class="{ 'is-active': page === currentPage }"
  48. @click="changePage(page)"
  49. >
  50. {{ page }}
  51. </button>
  52. <button
  53. class="admin--pagination-btn"
  54. :disabled="currentPage === totalPages"
  55. @click="changePage(currentPage + 1)"
  56. >
  57. 다음
  58. </button>
  59. </div>
  60. </div>
  61. </template>
  62. <script setup>
  63. import { ref, computed, onMounted } from 'vue'
  64. definePageMeta({
  65. layout: 'admin',
  66. middleware: ['auth']
  67. })
  68. const { get } = useApi()
  69. const isLoading = ref(false)
  70. const branches = ref([])
  71. const currentPage = ref(1)
  72. const perPage = ref(10)
  73. const totalCount = ref(0)
  74. const totalPages = ref(0)
  75. // 보이는 페이지 번호 계산
  76. const visiblePages = computed(() => {
  77. const pages = []
  78. const maxVisible = 5
  79. let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2))
  80. let end = Math.min(totalPages.value, start + maxVisible - 1)
  81. if (end - start < maxVisible - 1) {
  82. start = Math.max(1, end - maxVisible + 1)
  83. }
  84. for (let i = start; i <= end; i++) {
  85. pages.push(i)
  86. }
  87. return pages
  88. })
  89. // 데이터 로드
  90. const loadBranches = async () => {
  91. isLoading.value = true
  92. const params = {
  93. page: currentPage.value,
  94. per_page: perPage.value
  95. }
  96. const { data, error } = await get('/branch/list', params)
  97. if (data) {
  98. branches.value = data.items || []
  99. totalCount.value = data.total || 0
  100. totalPages.value = Math.ceil(totalCount.value / perPage.value)
  101. }
  102. isLoading.value = false
  103. }
  104. // 페이지 변경
  105. const changePage = (page) => {
  106. if (page < 1 || page > totalPages.value) return
  107. currentPage.value = page
  108. loadBranches()
  109. }
  110. onMounted(() => {
  111. loadBranches()
  112. })
  113. </script>