Toggle navigation
Toggle navigation
This project
Loading...
Sign in
임태민
/
Mapmory
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
임태민
2021-06-04 15:09:12 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
87dd7410042bd0ef5017be40b18e7e95d4378820
87dd7410
1 parent
62be3b14
Create post paging function
post 목록의 paging 기능을 추가하였습니다.
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
13 deletions
routes/posts.js
views/posts/index.ejs
routes/posts.js
View file @
87dd741
...
...
@@ -6,13 +6,28 @@ var util = require('../util');
// Post home
router
.
get
(
'/'
,
function
(
req
,
res
){
Post
.
find
({})
.
populate
(
'author'
)
.
sort
(
'-createdAt'
)
.
exec
(
function
(
err
,
posts
){
if
(
err
){
return
res
.
json
(
err
)};
res
.
render
(
'posts/index'
,
{
posts
:
posts
});
router
.
get
(
'/'
,
async
function
(
req
,
res
){
// Paging 기능 추가
var
page
=
Math
.
max
(
1
,
parseInt
(
req
.
query
.
page
));
var
limit
=
Math
.
max
(
1
,
parseInt
(
req
.
query
.
limit
));
page
=
!
isNaN
(
page
)?
page
:
1
;
limit
=
!
isNaN
(
limit
)?
limit
:
10
;
var
skip
=
(
page
-
1
)
*
limit
;
var
count
=
await
Post
.
countDocuments
({});
var
maxPage
=
Math
.
ceil
(
count
/
limit
);
var
posts
=
await
Post
.
find
({})
.
populate
(
'author'
)
.
sort
(
'-createdAt'
)
.
skip
(
skip
)
.
limit
(
limit
)
.
exec
();
res
.
render
(
'posts/index'
,
{
posts
:
posts
,
currentPage
:
page
,
maxPage
:
maxPage
,
limit
:
limit
});
});
...
...
views/posts/index.ejs
View file @
87dd741
...
...
@@ -47,15 +47,31 @@
</tr>
<
% }) %>
</tbody>
</table>
<nav
class=
"nav justify-content-center bg-light"
>
<
%
var offset = 2;
var previousBtnEnabled = currentPage>1;
var nextBtnEnabled = currentPage
<maxPage
;
%
>
<ul
class=
"pagination pagination-sm justify-content-center align-items-center h-100 mb-0"
>
<li
class=
"page-item <%= previousBtnEnabled?'':'disabled' %>"
>
<a
class=
"page-link"
href=
"/posts?page=<%= currentPage-1 %>&limit=<%= limit %>"
<%=
previousBtnEnabled
?''
:
'
tabindex=
-1'
%
>
>«
</a>
</li>
<
% for(i=1;i
<
=maxPage;i++){ %>
<
% if(i==1 || i==maxPage || (i>=currentPage-offset
&&
i
<
=currentPage+offset)){ %>
<li
class=
"page-item <%= currentPage==i?'active':'' %>"
><a
class=
"page-link"
href=
"/posts?page=<%= i %>&limit=<%= limit %>"
>
<
%= i %>
</a></li>
<
% } else if(i==2 || i==maxPage-1){ %>
<li><a
class=
"page-link"
>
...
</a></li>
<
% } %>
<
% } %>
<li
class=
"page-item <%= nextBtnEnabled?'':'disabled' %>"
>
<a
class=
"page-link"
href=
"/posts?page=<%= currentPage+1 %>&limit=<%= limit %>"
<%=
nextBtnEnabled
?''
:
'
tabindex=
-1'
%
>
>»
</a>
</li>
</ul>
</nav>
</div>
</div>
</body>
</html>
\ No newline at end of file
...
...
Please
register
or
login
to post a comment