199 lines
4.7 KiB
Vue
199 lines
4.7 KiB
Vue
<template>
|
|
<div>
|
|
<Transition name="fade">
|
|
<p
|
|
v-if="message"
|
|
class="message"
|
|
>
|
|
{{ message }}
|
|
</p>
|
|
</Transition>
|
|
<VueSelect
|
|
:id="name"
|
|
ref="select"
|
|
:loading="loading"
|
|
v-model="modelRelated"
|
|
:clearable="false"
|
|
label="name"
|
|
:options="data"
|
|
@search="search"
|
|
:filterable="true"
|
|
:disabled="dReadonly || dDisabled"
|
|
:searchable="true"
|
|
placeholder="Une marque existante"
|
|
>
|
|
<template #list-footer>
|
|
<li class="pagination">
|
|
<button
|
|
@click="
|
|
$event.preventDefault();
|
|
page -= 1;
|
|
"
|
|
:disabled="!hasPrevPage"
|
|
class="btn btn-sm btn-primary mt-2 mb-2 ms-2 me-2"
|
|
>
|
|
{{ __("Précédent") }}
|
|
</button>
|
|
<button
|
|
@click="
|
|
$event.preventDefault();
|
|
page += 1;
|
|
"
|
|
:disabled="!hasNextPage"
|
|
class="btn btn-sm btn-primary mt-2 mb-2"
|
|
>
|
|
{{ __("Suivant") }}
|
|
</button>
|
|
</li>
|
|
</template>
|
|
</VueSelect>
|
|
<input
|
|
type="hidden"
|
|
v-model="modelRelated.id"
|
|
:name="name"
|
|
>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts">
|
|
import { defineComponent } from "vue";
|
|
import route from "../modules/route";
|
|
import trans from "./../modules/trans";
|
|
import VueSelect from "vue-select";
|
|
|
|
export default defineComponent({
|
|
components: {
|
|
VueSelect,
|
|
},
|
|
mixins: [route, trans],
|
|
data(): {
|
|
name: string;
|
|
dDisabled: boolean;
|
|
dReadonly: boolean;
|
|
modelRelated: VselectOption;
|
|
loading: boolean;
|
|
searchText: string;
|
|
data: LaravelModelList;
|
|
page: number;
|
|
perPage: number;
|
|
hasNextPage: boolean;
|
|
hasPrevPage: boolean;
|
|
message: string | null;
|
|
} {
|
|
return {
|
|
name: "",
|
|
dDisabled: false,
|
|
dReadonly: false,
|
|
modelRelated: {
|
|
id: 0,
|
|
name: "",
|
|
value: 0,
|
|
},
|
|
loading: false,
|
|
searchText: "",
|
|
data: [],
|
|
page: 1,
|
|
perPage: 12,
|
|
hasNextPage: true,
|
|
hasPrevPage: false,
|
|
message: null,
|
|
};
|
|
},
|
|
mounted() {
|
|
const json = String(this.$attrs.json ?? "{}"),
|
|
data = JSON.parse(json);
|
|
this.name = data.name;
|
|
this.dDisabled = !!data.disabled;
|
|
this.dReadonly = !!data.readonly;
|
|
if (data.modelSelected) {
|
|
this.modelRelated = data.modelSelected;
|
|
} else if (data.modelRelated) {
|
|
this.modelRelated = data.modelRelated;
|
|
} else {
|
|
this.modelRelated = {
|
|
id: 0,
|
|
name: "",
|
|
value: 0,
|
|
};
|
|
}
|
|
this.search(false);
|
|
},
|
|
watch: {
|
|
page() {
|
|
if (this.page < 1) {
|
|
this.page = 1;
|
|
}
|
|
if (this.page > 1) {
|
|
this.hasPrevPage = true;
|
|
} else {
|
|
this.hasPrevPage = false;
|
|
}
|
|
this.search();
|
|
},
|
|
data() {
|
|
if (this.data.length == this.perPage) {
|
|
this.hasNextPage = true;
|
|
} else {
|
|
this.hasNextPage = false;
|
|
}
|
|
},
|
|
},
|
|
methods: {
|
|
search(search: false | null | string = null): void {
|
|
if (search !== false && search !== null) {
|
|
this.searchText = search;
|
|
}
|
|
let init = search === false ? true : false,
|
|
routeUrl = this.route("brands.jsonPaginate");
|
|
|
|
if (!routeUrl) {
|
|
throw new Error("undefined route brands.jsonPaginate");
|
|
}
|
|
|
|
if (this.page) {
|
|
routeUrl += "?page=" + this.page;
|
|
}
|
|
if (this.searchText) {
|
|
routeUrl += "&s=" + this.searchText;
|
|
}
|
|
|
|
this.loading = true;
|
|
|
|
window.axios
|
|
.get(routeUrl)
|
|
.then((response) => {
|
|
this.data = response.data.data as LaravelModelList;
|
|
if (init && this.data.length && this.modelRelated.value === null) {
|
|
this.modelRelated = this.data[0] as VselectOption;
|
|
}
|
|
})
|
|
.catch((e) => {
|
|
this.message =
|
|
"Erreur lors de l'opération, merci d'actualiser la page.";
|
|
if (e.response?.status === 422) {
|
|
const errors = e.response?.data?.errors;
|
|
if (errors instanceof String) {
|
|
this.message = errors as string;
|
|
}
|
|
if (errors instanceof Object) {
|
|
this.message = Object.values(errors).reduce(function (
|
|
previous,
|
|
current
|
|
) {
|
|
return String(previous) + String(current);
|
|
}) as string;
|
|
}
|
|
console.warn(errors);
|
|
}
|
|
})
|
|
.then(() => {
|
|
this.loading = false;
|
|
});
|
|
},
|
|
},
|
|
});
|
|
</script>
|
|
|
|
<style lang="scss">
|
|
@import "./../../../node_modules/vue-select/dist/vue-select.css";
|
|
</style>
|