perf: filterURL

This commit is contained in:
Javier Segarra 2024-05-27 09:50:21 +02:00
parent 2485ab755d
commit 83f5351774
2 changed files with 51 additions and 11 deletions

View File

@ -78,6 +78,7 @@ const requiredFieldRule = (val) => val ?? t('globals.fieldRequired');
const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props); const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props);
const myOptions = ref([]); const myOptions = ref([]);
const myOptionsFiltered = ref([]);
const myOptionsOriginal = ref([]); const myOptionsOriginal = ref([]);
const vnSelectRef = ref(); const vnSelectRef = ref();
const dataRef = ref(); const dataRef = ref();
@ -91,7 +92,7 @@ const value = computed({
}, },
}); });
function setOptions(data, append = false) { function setOptions(data, append = true) {
// if (isLoading.value) { // if (isLoading.value) {
// data.unshift(...myOptions.value); // data.unshift(...myOptions.value);
// } // }
@ -102,7 +103,7 @@ function setOptions(data, append = false) {
// // myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); // // myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
// } else { // } else {
myOptions.value = JSON.parse(JSON.stringify(data)); myOptions.value = JSON.parse(JSON.stringify(data));
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);
@ -117,11 +118,13 @@ const arrayData = useURL.value
: ref(null); : ref(null);
onMounted(async () => { onMounted(async () => {
setOptions(options.value); setOptions(options.value);
if ($props.url && $props.modelValue) fetchFilter($props.modelValue); if (useURL.value) {
else {
const { data } = await arrayData.fetch({ append: true }); const { data } = await arrayData.fetch({ append: true });
setOptions(data); setOptions(data);
return;
} }
if ($props.options) setOptions($props.options);
else fetchFilter($props.modelValue);
}); });
watch(modelValue, (newValue) => { watch(modelValue, (newValue) => {
@ -136,7 +139,7 @@ function filter(val, options) {
if (!search) return options; if (!search) return options;
return options.filter((row) => { const optionsFiltered = options.filter((row) => {
if ($props.filterOptions.length) { if ($props.filterOptions.length) {
return $props.filterOptions.some((prop) => { return $props.filterOptions.some((prop) => {
const propValue = String(row[prop]).toLowerCase(); const propValue = String(row[prop]).toLowerCase();
@ -149,23 +152,42 @@ function filter(val, options) {
return id == search || optionLabel.includes(search); return id == search || optionLabel.includes(search);
}); });
return optionsFiltered;
} }
async function fetchFilter(val) { function buildwhere(val) {
if (!$props.url || !dataRef.value) return; if (!useURL.value || !dataRef.value) return;
const { fields, sortBy, limit } = $props;
let key = optionLabel.value; let key = optionLabel.value;
if (new RegExp(/\d/g).test(val)) key = optionFilter.value ?? optionValue.value; if (new RegExp(/\d/g).test(val)) key = optionFilter.value ?? optionValue.value;
const where = { ...{ [key]: { like: `%${val}%` } }, ...$props.where }; const where = { ...{ [key]: { like: `%${val}%` } }, ...$props.where };
return where;
}
async function fetchFilter(val) {
const { fields, sortBy, limit } = $props;
const where = buildwhere(val);
return dataRef.value.fetch({ fields, where, order: sortBy, limit }); return dataRef.value.fetch({ fields, where, order: sortBy, limit });
} }
async function filterHandler(val, update) { async function filterHandler(val, update) {
if (!$props.defaultFilter) return update(); if (!$props.defaultFilter) return update();
const newOptions = filter(val, myOptionsOriginal.value); let newOptions = [];
if (myOptionsFiltered.value.length > 0) {
newOptions = filter(val, myOptionsFiltered.value);
myOptionsFiltered.value = [];
} else newOptions = filter(val, myOptionsOriginal.value);
if (useURL.value && myOptions.value.length < 1) {
// arrayData.store.filter.where = { [optionLabel.value]: val };
arrayData.store.filter.where = { [optionFilter.value]: val };
// arrayData.store.filter.where = buildwhere(val);
const { data } = await arrayData.fetch({ append: false });
newOptions = data;
myOptionsFiltered.value = data;
// setOptions(data, false);
}
update( update(
() => { () => {
myOptions.value = newOptions; myOptions.value = newOptions;
@ -184,7 +206,7 @@ async function onScroll(scrollEv) {
const lastIndex = myOptions.value.length - 1; const lastIndex = myOptions.value.length - 1;
if (from === 0 && index === 0) return; if (from === 0 && index === 0) return;
if (!$props.url && !$props.fetchRef) return; if (!useURL.value && !$props.fetchRef) return;
if (direction === 'decrease') return; if (direction === 'decrease') return;
if (to === lastIndex && arrayData.store.hasMoreData && !isLoading.value) { if (to === lastIndex && arrayData.store.hasMoreData && !isLoading.value) {
isLoading.value = true; isLoading.value = true;
@ -198,7 +220,7 @@ async function onScroll(scrollEv) {
<template> <template>
<FetchData <FetchData
v-if="$props.url" v-if="useURL"
ref="dataRef" ref="dataRef"
:url="$props.url" :url="$props.url"
@on-fetch="(data) => setOptions(data)" @on-fetch="(data) => setOptions(data)"

View File

@ -40,6 +40,23 @@ const filterOptions = {
}; };
</script> </script>
<template> <template>
<FetchData
url="ContactChannels"
@on-fetch="(data) => (contactChannels = data)"
auto-load
/>
<FetchData
url="BusinessTypes"
@on-fetch="(data) => (businessTypes = data)"
auto-load
/>
<FetchData
:filter="filter"
@on-fetch="(data) => (clients = data)"
auto-load
url="Clients"
/>
<FormModel :url="`Clients/${route.params.id}`" auto-load model="customer"> <FormModel :url="`Clients/${route.params.id}`" auto-load model="customer">
<template #form="{ data, validate, filter }"> <template #form="{ data, validate, filter }">
<VnRow class="row q-gutter-md q-mb-md"> <VnRow class="row q-gutter-md q-mb-md">
@ -113,6 +130,7 @@ const filterOptions = {
map-options map-options
option-label="name" option-label="name"
option-value="id" option-value="id"
option-filter="firstName"
use-input use-input
v-model="data.salesPersonFk" v-model="data.salesPersonFk"
> >