Add rudimentary pagination

This commit is contained in:
Daniel_I_Am 2021-09-02 14:33:38 +02:00
parent 9716b989ab
commit 0716c80a54
4 changed files with 93 additions and 13 deletions

View File

@ -47,7 +47,7 @@ class BlogArticleController extends Controller
$query = $query->where('published', true); $query = $query->where('published', true);
} }
$data = $query->limit(5)->get(); $data = $query->paginate(request()->get('limit', 5));
return response()->json($data); return response()->json($data);
} }

View File

@ -23,32 +23,52 @@
</div> </div>
</li> </li>
</ul> </ul>
<pagination
v-if="paginate"
v-model:page="page"
v-model:itemsPerPage="itemsPerPage"
:max-pages="lastPage"
></pagination>
</div> </div>
</section> </section>
</template> </template>
<script> <script>
import Pagination from "./Pagination";
export default { export default {
components: {
Pagination
},
data() { data() {
return { return {
loading: false, loading: false,
articles: [], articles: [],
error: null, error: null,
page: 0,
lastPage: 0,
itemsPerPage: 0,
}; };
}, },
props: {
paginate: {
type: Boolean,
default: false,
},
max: {
type: Number,
default: 5,
},
summary: {
type: Boolean,
default: false,
},
},
mounted() { mounted() {
axios.get("/api/blog/recent").then((res) => { this.itemsPerPage = this.max;
if (res.status > 399) {
this.error = res.statusText;
} else {
this.articles = res.data;
}
this.loading = false; this.updateList(this.itemsPerPage, this.page);
}).catch(ex => {
this.loading = false;
this.error = ex.message;
});
}, },
methods: { methods: {
relDate(date) { relDate(date) {
@ -67,6 +87,34 @@ export default {
this.$router.push({ name: 'blog-article', params: { year, month, day, id, slug }}); this.$router.push({ name: 'blog-article', params: { year, month, day, id, slug }});
}, },
updateList(limit, page) {
axios.get("/api/blog/recent", {
params: {
limit: limit,
page: page,
}
}).then((res) => {
if (res.status > 399) {
this.error = res.statusText;
} else {
this.articles = res.data.data;
this.lastPage = res.data.last_page;
}
this.loading = false;
}).catch(ex => {
this.loading = false;
this.error = ex.message;
});
}, },
},
watch: {
itemsPerPage() {
this.page = 0;
},
page(value) {
this.updateList(this.itemsPerPage, value);
},
}
} }
</script> </script>

View File

@ -0,0 +1,32 @@
<template>
<button @click="gotoPage(0)">First</button>
<button @click="gotoPage(page - 1)">Previous</button>
<span>{{ page + 1 }} / {{ maxPages }}</span>
<button @click="gotoPage(page + 1)">Next</button>
<button @click="gotoPage(maxPages)">Last</button>
</template>
<script>
export default {
props: {
page: Number,
itemsPerPage: Number,
maxPages: Number,
},
methods: {
gotoPage(page) {
if (page < 0) {
page = 0;
}
if (page >= this.maxPages) {
page = this.maxPages - 1;
}
if (page !== this.page) {
this.$emit('update:page', page);
}
},
}
}
</script>

View File

@ -4,7 +4,7 @@
<container> <container>
<split-content> <split-content>
<template v-slot:main> <template v-slot:main>
<blog-recent></blog-recent> <blog-recent :paginate="true" :max="10" :summary="true"></blog-recent>
</template> </template>
<template v-slot:aside> <template v-slot:aside>