Skip to content

Commit ba57c4b

Browse files
added a paginated autocomplete basic component
1 parent 4f79a45 commit ba57c4b

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<template>
2+
<v-autocomplete v-bind="$attrs"
3+
:items="items"
4+
:search-input.sync="searchInput"
5+
:loading="loading"
6+
>
7+
<template v-slot:append-item>
8+
<v-flex v-if="canLoadMoreItems" class="text-xs-center">
9+
<v-btn color="accent" @click="getItemsFromApi(page + 1)">Meer laden</v-btn>
10+
</v-flex>
11+
</template>
12+
</v-autocomplete>
13+
</template>
14+
<script>
15+
export default {
16+
name: 'PaginatedAutocomplete',
17+
data() {
18+
return {
19+
items: [],
20+
page: 1,
21+
lastPage: 1,
22+
searchInput: null,
23+
};
24+
},
25+
props: {
26+
default() {
27+
return {
28+
url: '',
29+
perPage: 15,
30+
loading: true
31+
};
32+
},
33+
pagination: {
34+
type: Object,
35+
},
36+
},
37+
watch: {
38+
searchInput() {
39+
this.getItemsFromApi(this.page);
40+
},
41+
},
42+
computed: {
43+
canLoadMoreItems() {
44+
return this.page < this.lastPage;
45+
},
46+
},
47+
created() {
48+
this.getItemsFromApi(1);
49+
},
50+
methods: {
51+
getItemsFromApi(page) {
52+
this.loading = true;
53+
this.$http.get(this.pagination.url + '?perPage=' + this.pagination.perPage + '&page=' + page + '&q=' + (this.searchInput || '')).then((response) => {
54+
response.data.data = response.data.data.map(obj => {
55+
return {[this.$attrs['item-text']]: obj[this.$attrs['item-text']], [this.$attrs['item-value']]: obj[this.$attrs['item-value']]};
56+
});
57+
if (this.items === []) {
58+
this.items = response.data.data;
59+
} else {
60+
this.items = [...this.items, ...response.data.data];
61+
}
62+
this.page = response.data.meta.current_page;
63+
this.lastPage = response.data.meta.last_page;
64+
this.loading = false;
65+
});
66+
67+
},
68+
},
69+
};
70+
</script>
71+
72+
<style scoped>
73+
74+
</style>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cli-plugin-kingscode-scaffold",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)