feat: useArrayData
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javier Segarra 2024-06-18 12:59:47 +02:00
parent e775d922e8
commit 817fcc4485
1 changed files with 16 additions and 32 deletions

View File

@ -68,15 +68,7 @@ const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($
const myOptions = ref([]); const myOptions = ref([]);
const myOptionsOriginal = ref([]); const myOptionsOriginal = ref([]);
const vnSelectRef = ref(); const vnSelectRef = ref();
const dataRef = ref(
useFetchData({
url: $props.url,
where: $props.where,
limit: $props.limit,
sortBy: $props.sortBy,
fields: $props.fields,
})
);
const selectValue = computed({ const selectValue = computed({
get() { get() {
return $props.modelValue; return $props.modelValue;
@ -92,15 +84,19 @@ function setOptions(data, append = true) {
if (append) myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); if (append) myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
} }
const useURL = computed(() => $props.url?.length > 0); const useURL = computed(() => $props.url?.length > 0);
const arrayData = useURL.value import { useAttrs } from 'vue';
? useArrayData($props.url, {
url: $props.url, const $attrs = useAttrs();
where: $props.where, const arrayDataKey = $props.url !== '' ? $props.url : $attrs.label;
limit: $props.limit, const arrayDataOptions =
sortBy: $props.sortBy, {
fields: $props.fields, url: $props.url,
}) where: $props.where,
: ref(null); limit: $props.limit,
sortBy: $props.sortBy,
fields: $props.fields,
} ?? {};
const arrayData = useArrayData(arrayDataKey, arrayDataOptions);
onMounted(async () => { onMounted(async () => {
setOptions(options.value); setOptions(options.value);
if (!$props.options) fetchFilter($props.modelValue); if (!$props.options) fetchFilter($props.modelValue);
@ -140,7 +136,7 @@ function filter(val, options) {
} }
async function fetchFilter(val) { async function fetchFilter(val) {
if (!useURL.value || !dataRef.value) return; if (!useURL.value) return;
const { fields, sortBy, limit } = $props; const { fields, sortBy, limit } = $props;
let key = new RegExp(/\d/g).test(val) let key = new RegExp(/\d/g).test(val)
@ -148,7 +144,7 @@ async function fetchFilter(val) {
: optionValue.value; : optionValue.value;
const where = { ...{ [key]: { like: `%${val}%` } }, ...$props.where }; const where = { ...{ [key]: { like: `%${val}%` } }, ...$props.where };
return dataRef.value.fetch({ fields, where, order: sortBy, limit }); return arrayData.value.fetch({ fields, where, order: sortBy, limit });
} }
async function filterHandler(val, update) { async function filterHandler(val, update) {
@ -197,18 +193,6 @@ async function onScroll(scrollEv) {
</script> </script>
<template> <template>
<!-- <component :is="fetchDataComponent" ref="fetchDataRef" />
<FetchData
v-if="useURL"
ref="dataRef"
:url="$props.url"
@on-fetch="(data) => setOptions(data)"
:where="where || { [optionValue]: value }"
:limit="limit"
:sort-by="sortBy"
:fields="fields"
/> -->
<QSelect <QSelect
:loading="isLoading" :loading="isLoading"
@virtual-scroll="onScroll" @virtual-scroll="onScroll"