Skip to content

Commit 5de8ae8

Browse files
update paginated autocomplete
1 parent bca4a87 commit 5de8ae8

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

generator/templates/Default/src/components/PaginatedAutocomplete.vue

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
:items="items"
44
:search-input.sync="searchInput"
55
:loading="loading"
6+
:value="$attrs.value"
7+
@change="handleChange"
8+
v-if="enabled"
69
>
710
<template v-slot:append-item>
811
<v-flex v-if="canLoadMoreItems" class="text-xs-center">
@@ -20,23 +23,35 @@
2023
page: 1,
2124
lastPage: 1,
2225
searchInput: null,
26+
loading: true,
27+
enabled: true,
2328
};
2429
},
2530
props: {
26-
default() {
27-
return {
28-
url: '',
29-
perPage: 15,
30-
loading: true
31-
};
32-
},
3331
pagination: {
3432
type: Object,
33+
default() {
34+
return {
35+
url: '',
36+
perPage: 15,
37+
};
38+
},
3539
},
3640
},
3741
watch: {
3842
searchInput() {
39-
this.getItemsFromApi(this.page);
43+
let selectedItemsText = this.items.filter(obj => {
44+
return obj.id === this.$attrs.value;
45+
})[0][this.$attrs['item-text']];
46+
if(selectedItemsText !== this.searchInput) {
47+
this.getItemsFromApi(this.page, this.searchInput);
48+
}
49+
},
50+
'$attrs.value': {
51+
handler() {
52+
this.loadSelectedItemsIfTheyNotExist();
53+
},
54+
deep: false,
4055
},
4156
},
4257
computed: {
@@ -45,30 +60,71 @@
4560
},
4661
},
4762
created() {
48-
this.getItemsFromApi(1);
63+
this.reload();
4964
},
5065
methods: {
51-
getItemsFromApi(page) {
66+
getItemsFromApi(page, searchInput) {
5267
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-
}
68+
this.$http.get(this.pagination.url + '?perPage=' + this.pagination.perPage + '&page=' + page + '&q=' + (searchInput || '')).then((response) => {
69+
response.data.data = response.data.data.map(obj => this.mapItems(obj));
70+
71+
this.items = [...this.items, ...response.data.data];
6272
this.page = response.data.meta.current_page;
6373
this.lastPage = response.data.meta.last_page;
6474
this.loading = false;
6575
});
6676
6777
},
78+
// load selected items first
79+
getSelectedItems() {
80+
let ids = this.$attrs.value;
81+
if (typeof ids === 'undefined' || ids === null) {
82+
return;
83+
}
84+
if (typeof ids === 'number') {
85+
ids = [ids];
86+
}
87+
this.$http.get(this.pagination.url + '?only=' + ids.join() + '&perPage=' + ids.length).then((response) => {
88+
response.data.data = response.data.data.map(obj => this.mapItems(obj));
89+
this.items = [...this.items, ...response.data.data];
90+
});
91+
},
92+
handleChange(value) {
93+
this.$emit('input', value);
94+
},
95+
mapItems(obj) {
96+
return {
97+
[this.$attrs['item-text']]: obj[this.$attrs['item-text']],
98+
[this.$attrs['item-value']]: obj[this.$attrs['item-value']],
99+
};
100+
},
101+
reload() {
102+
this.items = [];
103+
this.getSelectedItems();
104+
this.getItemsFromApi(1);
105+
},
106+
loadSelectedItemsIfTheyNotExist() {
107+
let ids = this.$attrs.value;
108+
if (typeof ids === 'undefined' || ids === null) {
109+
return;
110+
}
111+
if (typeof ids === 'number') {
112+
ids = [ids];
113+
}
114+
ids.forEach((value) => {
115+
let found = this.items.find((obj) => {
116+
return obj.id === value;
117+
});
118+
if (typeof found === 'undefined') {
119+
this.getSelectedItems();
120+
return;
121+
}
122+
});
123+
},
68124
},
69125
};
70126
</script>
71127

72128
<style scoped>
73129
74-
</style>
130+
</style>

0 commit comments

Comments
 (0)