IT/development

[Vue.js] ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ฒ˜๋ฆฌ

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2023. 8. 9.

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ

<div id="main">
<!-- ์ฝ”๋“œ ์ƒ๋žต... -->

<!--ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ปดํฌ๋„ŒํŠธ-->
<!-- 
props๋กœ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ด€๋ จ parameter 5๊ฐœ ์ „๋‹ฌํ•˜๊ณ 
์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ @update:current-page๋กœ ๋ฐ์ดํ„ฐ(page) ์ „๋‹ฌ ๋ฐ›์Œ
-->
<paging-component :current-page="currentPage" :first-page="firstPage" :last-page="lastPage" :start-idx="startIdx" :end-idx="endIdx" @update:current-page="onUpdateCurrentPage"></paging-component>
</div>

<script layout:fragment="script" th:inline="javascript" type="text/javascript">
        new Vue({
           //element id
           el: '#main',
           //vue์—์„œ ๊ด€๋ฆฌํ•  ๋ฐ์ดํ„ฐ
           data: {
                 items: [],   // ๋ฐ์ดํ„ฐ ๋ชฉ๋ก
                 currentPage: 1,  //ํ˜„์žฌ ํŽ˜์ด์ง€
                 firstPage: 0,        //์ฒซ๋ฒˆ ์งธ ํŽ˜์ด์ง€
                 lastPage: 0,         //๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€
                 startIdx: 0,      //์ฒซ๋ฒˆ ์งธ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
                 endIdx: 0,       //๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ
                 empNm: '',         //์‚ฌ์›๋ช…
           },   
          //data๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ณ€๊ฒฝํ•  ์†์„ฑ(data์˜ ๋ณ€๊ฒฝ ๊ฐ์ง€)
           computed: {
            //๋ณ€๊ฒฝ๋œ ๋ฐ์ดํ„ฐ
            displayedItems() {
              return this.items;
            },
           },
           //vue ์ปดํฌ๋„ŒํŠธ ํ™”๋ฉด์— ๋ Œ๋”๋ง ๋œ ์งํ›„ ์‹คํ–‰(์ปดํฌ๋„ŒํŠธ์˜ ํ…œํ”Œ๋ฆฟ์ด DOM์— ์ถ”๊ฐ€/ํ™”๋ฉด์— ํ‘œ์‹œ๋œ ์งํ›„)
           mounted() {
            //๋ฐ์ดํ„ฐ ์กฐํšŒ
            this.fetchData();
           },
           methods: {
           //๋ฐ์ดํ„ฐ ์กฐํšŒ
           fetchData() {
             const url = '/api/board';
             //2๋ฒˆ ์งธ parameter(queryString)
             const params = { params:
                    { currentPage: this.currentPage,
                      empNm: this.empNm
                    }
                   };
             //ajax ํ˜ธ์ถœ
             axios.get(url, params)
             .then(function(response) {
               this.items = response.data.list;
               this.firstPage = response.data.paging.firstPage;
               this.lastPage = response.data.paging.lastPage;
               this.startIdx = response.data.paging.firstPageNo;
               this.endIdx = response.data.paging.lastPageNo;
             }.bind(this))
             .catch(function(error) {
               // ์—๋Ÿฌ ์ฒ˜๋ฆฌ
               console.log(error);
             });
           },
           // ํŽ˜์ด์ง€ ์ด๋™ ์ด๋ฒคํŠธ ์‹œ ์ˆ˜ํ–‰(์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌ๋ฐ›์€ page)
           onUpdateCurrentPage(page) {
             this.currentPage = page;
             this.fetchData(); // fetchData() ๋ฉ”์„œ๋“œ ์‹คํ–‰
           },
           //๊ฒ€์ƒ‰
           search() {
             this.currentPage = 1;
             this.fetchData();
           },
          },
        });

์ž์‹์ปดํฌ๋„ŒํŠธ

// ํŽ˜์ด์ง• ๊ด€๋ จ ์ปดํฌ๋„ŒํŠธ
// ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ฐ›์•„์„œ ํŽ˜์ด์ง• ์‹œ์ž‘~์ข…๋ฃŒ์ธ๋ฑ์Šค ์„ธํŒ… ํ›„ ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ฒ˜๋ฆฌ
Vue.component('paging-component', {
    props: ['currentPage', 'firstPage','lastPage', 'startIdx', 'endIdx'],
    computed: {
    //ํŽ˜์ด์ง• ๊ฐœ์ˆ˜ ์„ธํŒ…(์‹œ์ž‘์ธ๋ฑ์Šค, ์ข…๋ฃŒ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์•„์„œ ๋™์ ์œผ๋กœ array ์„ธํŒ…)
        pagedArray() {
            const pagedArr = [];
                for (let i = this.startIdx; i <= this.endIdx; i++) {
                    pagedArr.push(i);
                }
            return pagedArr;
        },
    },
    methods: {
       movePage(page) {
       	 //์ด๋ฒคํŠธ ๋ฐœ์ƒ(๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ) 
         this.$emit('update:current-page', page);
       },
     },
    //ํŽ˜์ด์ง€๋„ค์ด์…˜ ํ…œํ”Œ๋ฆฟ
    template: `        
        <div class="paging tbl-paging">
          <ul class='pagination'>
            <!--first-->
            <li class='paginate_button page-item first'>
              <button
                type='button'
                class='page-link'
                :style="{ cursor: currentPage !== firstPage ? 'pointer' : 'default' }"
                :disabled="currentPage === firstPage"
                @click="movePage(firstPage)"    
              > 
                <span class='hidden'>์ฒซ ํŽ˜์ด์ง€</span>
              </button>
            </li>
            <!--prev-->
            <li class='paginate_button page-item prev'>
              <button
                type='button'
                class='page-link'
                :style="{ cursor: currentPage !== firstPage ? 'pointer' : 'default' }"
                :disabled="currentPage === firstPage"
                @click="movePage(currentPage - 1)"
              >         
                <span class='hidden'>์ด์ „ ํŽ˜์ด์ง€</span>
              </button>
            </li>   
            <!--โ‘  ~ โ‘ฉ-->    
            <li         
              class='paginate_button page-item'
              v-for="(page, index) in pagedArray"
              :key="index"  
              :class="{ 'active': currentPage === page }"
              @click="movePage(page)"       
            >                            
              <button type='button' class='page-link' style='cursor: pointer;'>
                {{ page }}
              </button>  
            </li>
            <!--next-->
            <li class='paginate_button page-item next'>
              <button
                type='button'
                :disabled="currentPage === lastPage"
                class='page-link'
                :style="{ cursor: currentPage !== lastPage ? 'pointer' : 'default' }"
                @click="movePage(currentPage + 1)"
              >     
                <span class='hidden'>๋‹ค์Œ ํŽ˜์ด์ง€</span>
              </button>
            </li>
            <!--last-->
            <li class='paginate_button page-item last'>
              <button
                type='button'
                :disabled="currentPage === lastPage"    
                class='page-link'
                :style="{ cursor: currentPage !== lastPage ? 'pointer' : 'default' }"
                @click="movePage(last)"
              >     
                <span class='hidden'>๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€</span>
              </button>
            </li>
          </ul>
        </div>
      `,
});

๋Œ“๊ธ€