pagination

This commit is contained in:
leejinseok
2020-02-17 12:17:12 +09:00
parent 5b72ccb241
commit 2a48f02b19
6 changed files with 108 additions and 19 deletions

View File

@@ -175,4 +175,6 @@ img {vertical-align: bottom;}
opacity: 0; opacity: 0;
} }
} }
</style> </style>

View File

@@ -9,6 +9,17 @@
<span @click="clickUser">{{ article.user.name }}</span> <span @click="clickUser">{{ article.user.name }}</span>
</article> </article>
<div class="paginatior-wrapper" v-if="pages">
<ul class="clearfix">
<li v-for="page in pages" v-bind:key="page" v-bind:class="{active: isActivePage(page)}">
<router-link :to="{ path: '/articles', query: { page: page - 1 }}" v-if="typeof(page) === 'number'">
{{ page }}
</router-link>
</li>
</ul>
</div>
<br/> <br/>
<div v-if="user"> <div v-if="user">
@@ -32,6 +43,7 @@
import authService from "../../services/authService"; import authService from "../../services/authService";
import articleService from "../../services/articleService"; import articleService from "../../services/articleService";
import Header from '../../components/common/Header'; import Header from '../../components/common/Header';
import paginationUtil from "../../utils/paginationUtil";
export default { export default {
name: "List", name: "List",
@@ -47,6 +59,7 @@
data() { data() {
return { return {
articles: [], articles: [],
pages: null,
pending: true, pending: true,
user: null user: null
}; };
@@ -57,6 +70,15 @@
authService.session = authService.session.bind(this); authService.session = authService.session.bind(this);
}, },
async created() { async created() {
await this.fetchArticles();
},
watch: {
'$route': async function () {
await this.fetchArticles();
}
},
methods: {
async fetchArticles() {
this.startSpin(); this.startSpin();
try { try {
@@ -66,21 +88,29 @@
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
const page = this.$route.query.page || 0;
const articles = await articleService.getArticles({}); const data = await articleService.getArticles({page});
const {content, totalPages, totalElements } = data;
this.articles = articles.content; this.pages = paginationUtil(page, totalPages);
this.articles = content;
this.pending = false; this.pending = false;
this.stopSpin(true); this.stopSpin(true);
}, },
methods: {
async logout() { async logout() {
if (!confirm("정말 로그아웃 하시겠습니까?")) return; if (!confirm("정말 로그아웃 하시겠습니까?")) return;
await authService.logout(); await authService.logout();
}, },
clickUser(evt) { clickUser(evt) {
console.log(evt.target); console.log(evt.target);
},
isActivePage(page) {
const currentPage = this.$route.query.page || 0;
console.log(currentPage, page);
return page - 1 === currentPage;
} }
} }
}; };
@@ -110,4 +140,23 @@
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
} }
.paginatior-wrapper {
}
.paginatior-wrapper ul {
}
.paginatior-wrapper ul li {
float: left;
border: 1px solid dimgray;
padding: 6px 4px;
}
.paginatior-wrapper ul li.active {
background-color: dimgray;
color: #fff;
}
</style> </style>

View File

@@ -3,17 +3,18 @@ import commonUtil from "../utils/commonUtil";
import authApi from "../api/authApi"; import authApi from "../api/authApi";
export default { export default {
async getArticles({page = 0, size = 10}) { async getArticles({page = 0, size = 3}) {
try { try {
const accessToken = this.$cookie.get('accessToken'); const accessToken = this.$cookie.get('accessToken');
const authorization = accessToken ? commonUtil.getAuthenticationHeaderBearer(accessToken) : ''; const authorization = accessToken ? commonUtil.getAuthenticationHeaderBearer(accessToken) : '';
const {data} = await articleApi.getArticles({ const result = await articleApi.getArticles({
page, page,
size size
}, authorization); }, authorization);
return data;
return result.data;
} catch (err) { } catch (err) {
alert('문제가 발생하였습니다.'); alert('문제가 발생하였습니다.');

View File

@@ -1,5 +1,12 @@
export default { export default {
getAuthenticationHeaderBearer(accessToken) { getAuthenticationHeaderBearer(accessToken) {
return 'Bearer ' + accessToken; return 'Bearer ' + accessToken;
},
pagination(totalElements, totalPages, pageSize) {
// 페이지 그룹 사이즈 (5, 10)
// 시작 페이지, 마침 페이지
// 최초 페이지, 마지막 페이지
// 이전페이지 여부, 다음 페이지 여부
// 현재 페이지
} }
}; };

View File

@@ -0,0 +1,30 @@
export default function (currentPage, pageCount) {
let current = currentPage,
last = pageCount,
delta = 2,
left = current - delta,
right = current + delta + 1,
range = [],
rangeWithDots = [],
l;
for (let i = 1; i <= last; i++) {
if (i == 1 || i == last || i >= left && i < right) {
range.push(i);
}
}
for (let i of range) {
if (l) {
if (i - l === 2) {
rangeWithDots.push(l + 1);
} else if (i - l !== 1) {
rangeWithDots.push('...');
}
}
rangeWithDots.push(i);
l = i;
}
return rangeWithDots;
}

View File

@@ -41,7 +41,7 @@ public class ArticleServiceTest {
Article article = baseTest.findTestArticle(); Article article = baseTest.findTestArticle();
ArticleRequestDto articleRequestDto = new ArticleRequestDto(); ArticleRequestDto articleRequestDto = new ArticleRequestDto();
articleRequestDto.setTitle("update title"); articleRequestDto.setTitle("update title");
articleRequestDto.setTitle("update content"); articleRequestDto.setContent("update content");
articleService.update(article.getId(), articleRequestDto, baseTest.findTestUser()); articleService.update(article.getId(), articleRequestDto, baseTest.findTestUser());
} }