add pagination
This commit is contained in:
parent
4438c57b4d
commit
cd4809f828
29
src/App.vue
29
src/App.vue
@ -18,6 +18,12 @@
|
|||||||
|
|
||||||
<div v-if="isPostLoading" style="margin-top: 15px">Loading...</div>
|
<div v-if="isPostLoading" style="margin-top: 15px">Loading...</div>
|
||||||
<post-list v-else :posts="sortedAndSearchedPosts" @removePost="removePost" class="postList"/>
|
<post-list v-else :posts="sortedAndSearchedPosts" @removePost="removePost" class="postList"/>
|
||||||
|
|
||||||
|
<my-pagination
|
||||||
|
:currentPage="page"
|
||||||
|
:totalPages="totalPages"
|
||||||
|
@changePage="changePage"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@ -25,9 +31,11 @@
|
|||||||
import PostForm from '@/components/PostForm.vue';
|
import PostForm from '@/components/PostForm.vue';
|
||||||
import PostList from '@/components/PostList.vue';
|
import PostList from '@/components/PostList.vue';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import MyPagination from '@/components/ui/Pagination.vue';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
|
MyPagination,
|
||||||
PostForm,
|
PostForm,
|
||||||
PostList
|
PostList
|
||||||
},
|
},
|
||||||
@ -41,7 +49,10 @@ export default {
|
|||||||
{value: 'title', name: 'По названию'},
|
{value: 'title', name: 'По названию'},
|
||||||
{value: 'body', name: 'По описанию'}
|
{value: 'body', name: 'По описанию'}
|
||||||
],
|
],
|
||||||
searchQuery: ''
|
searchQuery: '',
|
||||||
|
page: 1,
|
||||||
|
limit: 10,
|
||||||
|
totalPages: 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@ -57,11 +68,20 @@ export default {
|
|||||||
},
|
},
|
||||||
async fetchPosts() {
|
async fetchPosts() {
|
||||||
try {
|
try {
|
||||||
const {data} = await axios.get('https://jsonplaceholder.typicode.com/posts?_limit=10');
|
const { data, headers } = await axios.get(`https://jsonplaceholder.typicode.com/posts`, {
|
||||||
this.posts = data
|
params: {
|
||||||
|
_limit: this.limit,
|
||||||
|
_page: this.page
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.posts = data;
|
||||||
|
this.totalPages = Math.ceil(headers['x-total-count'] / this.limit);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert(e.message);
|
alert(e.message);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
changePage(newPage) {
|
||||||
|
this.page = newPage;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async mounted() {
|
async mounted() {
|
||||||
@ -91,6 +111,9 @@ export default {
|
|||||||
} else {
|
} else {
|
||||||
document.body.style.overflow = 'auto';
|
document.body.style.overflow = 'auto';
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
page() {
|
||||||
|
this.fetchPosts()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="post">
|
<div class="post">
|
||||||
<div>
|
<div>
|
||||||
|
{{ post.id }}
|
||||||
<div><strong>Название:</strong> {{post.title}}</div>
|
<div><strong>Название:</strong> {{post.title}}</div>
|
||||||
<div><strong>Описание:</strong> {{post.body}}</div>
|
<div><strong>Описание:</strong> {{post.body}}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
52
src/components/ui/Pagination.vue
Normal file
52
src/components/ui/Pagination.vue
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<template>
|
||||||
|
<div class="page__wrapper">
|
||||||
|
<div
|
||||||
|
v-for="page in totalPages"
|
||||||
|
:key="page"
|
||||||
|
class="page"
|
||||||
|
:class="{
|
||||||
|
'current-page': page === currentPage
|
||||||
|
}"
|
||||||
|
@click="$emit('changePage', page)"
|
||||||
|
>
|
||||||
|
{{page}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
name: 'my-pagination',
|
||||||
|
props: {
|
||||||
|
totalPages: {
|
||||||
|
type: Number,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
currentPage: {
|
||||||
|
type: Number,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.page__wrapper {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
margin-top: 15px;
|
||||||
|
column-gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page {
|
||||||
|
border: 1px solid teal;
|
||||||
|
padding: 10px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.current-page {
|
||||||
|
border: 2px solid teal;
|
||||||
|
font-weight: bold;
|
||||||
|
color: teal;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -2,10 +2,12 @@ import Button from '@/components/ui/Button.vue';
|
|||||||
import Input from '@/components/ui/Input.vue';
|
import Input from '@/components/ui/Input.vue';
|
||||||
import Dialog from '@/components/ui/Dialog.vue';
|
import Dialog from '@/components/ui/Dialog.vue';
|
||||||
import Select from '@/components/ui/Select.vue';
|
import Select from '@/components/ui/Select.vue';
|
||||||
|
import Pagination from '@/components/ui/Pagination.vue';
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
Button,
|
Button,
|
||||||
Input,
|
Input,
|
||||||
Dialog,
|
Dialog,
|
||||||
Select
|
Select,
|
||||||
|
Pagination
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user