refs #6694 VnLocation #154
|
@ -0,0 +1,179 @@
|
|||
<script setup>
|
||||
import { ref, toRefs, computed, watch } from 'vue';
|
||||
import CreateNewPostcode from 'src/components/CreateNewPostcodeForm.vue';
|
||||
import VnSelectCreate from 'components/common/VnSelectCreate.vue';
|
||||
import VnSelectFilter from 'components/common/VnSelectFilter.vue';
|
||||
import FetchData from 'components/FetchData.vue';
|
||||
const emit = defineEmits(['update:modelValue', 'update:options']);
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
const { t } = useI18n();
|
||||
const citiesLocationOptions = ref([]);
|
||||
const provincesLocationOptions = ref([]);
|
||||
const countriesOptions = ref([]);
|
||||
const postcodesOptions = ref([]);
|
||||
const postcodeFetchDataRef = ref(null);
|
||||
|
||||
const $props = defineProps({
|
||||
modelValue: {
|
||||
type: [String, Number, Object],
|
||||
default: null,
|
||||
},
|
||||
options: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
optionLabel: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
optionValue: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
filterOptions: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
isClearable: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
defaultFilter: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
});
|
||||
const locationsRef = ref();
|
||||
const locationsRefFilter = ref({ fields: ['code', 'nickname'], limit: 30 });
|
||||
|
||||
const { optionLabel, optionValue, options } = toRefs($props);
|
||||
const myOptions = ref([]);
|
||||
const myOptionsOriginal = ref([]);
|
||||
const vnSelectRef = ref();
|
||||
const showForm = ref(false);
|
||||
|
||||
const value = computed({
|
||||
get() {
|
||||
return $props.modelValue;
|
||||
},
|
||||
set(value) {
|
||||
emit('update:modelValue', value);
|
||||
},
|
||||
});
|
||||
|
||||
function setOptions(data) {
|
||||
myOptions.value = JSON.parse(JSON.stringify(data));
|
||||
myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
|
||||
}
|
||||
setOptions(options.value);
|
||||
const filter = (val, options) => {
|
||||
const search = val.toString().toLowerCase();
|
||||
|
||||
if (!search) return options;
|
||||
// if (!search.length < 2) return options;
|
||||
|
||||
jsegarra marked this conversation as resolved
Outdated
|
||||
return options
|
||||
};
|
||||
|
||||
const filterHandler = (val, update) => {
|
||||
update(
|
||||
() => {
|
||||
// if (val.length>2)
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
De esta manera te estas descargando toda la tabla
Por eso decia que no veia la funcion de filtrado. Yo no haría el filtrado en el front dado que te obliga a descargar todos los registros. Y en este caso lo veo inviable. (añadir limits tambien) De esta manera te estas descargando toda la tabla
- La tabla town tiene 71.691 registros.
- La tabla postCode 271.259 registros.
- ...
Por eso decia que no veia la funcion de filtrado. Yo no haría el filtrado en el front dado que te obliga a descargar todos los registros. Y en este caso lo veo inviable. (añadir limits tambien)
jsegarra
commented
Tendré en cuenta estos datos para hacer unos cambios con respecto al código original. Tendré en cuenta estos datos para hacer unos cambios con respecto al código original.
Gracias por la aportación.
|
||||
myOptions.value = filter(val, myOptionsOriginal.value);
|
||||
},
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Si solo se usa una vez, llamar directamente a locationFilter Si solo se usa una vez, llamar directamente a locationFilter
jsegarra
commented
Por mantener el formato en el uso de este componente. Por mantener el formato en el uso de este componente.
Pero se puede borrar.
|
||||
(ref) => {
|
||||
if (val !== '' && ref.options.length > 0) {
|
||||
jsegarra marked this conversation as resolved
Outdated
jsegarra
commented
Opcion A=> limit="30" Opcion A=> limit="30"
|
||||
ref.setOptionIndex(-1);
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Sin auto-load y hacer con codigo per con limit Sin auto-load y hacer con codigo per con **limit**
O con auto-load pero con limit
jsegarra
commented
El limit lo tienes definido en la línea 71, es correcto? El limit lo tienes definido en la línea 71, es correcto?
alexm
commented
Si y no. Eso sirve para las demás peticiones. Si y no. Eso sirve para las demás peticiones.
Pero no da más abrir la sección te carga toda la tabla (sin limit) al estar puesto el auto-load
(Lo he probado entrando en las ramas)
|
||||
ref.moveOptionSelection(1, true);
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
function locationFilter(val) {
|
||||
let where = { limit: 30 };
|
||||
let params = {};
|
||||
let key = 'nickname';
|
||||
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Igual se lo pondria mas alto, ver como esta en salix Igual se lo pondria mas alto, ver como esta en salix
jsegarra
commented
Corregido Corregido 6809e9bddb37ba812c3e56a8f62f3beb255a051d
|
||||
if (new RegExp(/\d/g).test(val)) {
|
||||
key = 'id';
|
||||
}
|
||||
params = { [key]: { like: `%${val}%` } };
|
||||
where = Object.assign(where, params);
|
||||
locationsRef.value.fetch({ where });
|
||||
}
|
||||
watch(options, (newValue) => {
|
||||
setOptions(newValue);
|
||||
});
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Com veus esta part? @jgallego Com veus esta part? @jgallego
Es una reglat de filtrat que t'obliga a escriure al menys 3 caracters per a que te filtre.
No ho acabe de vore. Pq si per exemple lo que volen es filtrar per pais pq hi ha un pais q saben q soles tenen 1 codi postal o molt pocs. i fiquen per exemple "J" (pq estan buscant Japon). no els trauria res
jgallego
commented
Si la consulta no es muy lenta/pesada yo no pondria nada. Si la consulta no es muy lenta/pesada yo no pondria nada.
jsegarra
commented
Lo comentamos.
> Es una reglat de filtrat que t'obliga a escriure al menys 3 caracters per a que te filtre.
> No ho acabe de vore. Pq si per exemple lo que volen es filtrar per pais pq hi ha un pais q saben q soles tenen 1 codi postal o molt pocs. i fiquen per exemple "J" (pq estan buscant Japon). no els trauria res
>
Lo comentamos.
|
||||
|
||||
function showLabel(data){
|
||||
return `${data.code} - ${data.town.name}(${data.town.province.name}), ${data.town.province.country.country}`;
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Si al final se fa la peticio en la back li possaria un delay (input-bounce) crec que se dia Si al final se fa la peticio en la back li possaria un delay (input-bounce) crec que se dia
jsegarra
commented
Lo comentamos Lo comentamos
jsegarra
commented
corregido corregido 655c8210751f819ad4aff1480f94e57f73e22931
|
||||
}
|
||||
|
||||
</script>
|
||||
jsegarra marked this conversation as resolved
Outdated
alexm
commented
Y tampoco veo que se este usando el evento no? Y tampoco veo que se este usando el evento no?
jsegarra
commented
Por mantener el formato en el uso de este componente. Por mantener el formato en el uso de este componente.
Diría que el valor de $event no se está gestionando de ninguna manera en el resto de instancias
En este caso se reemplaza por locationFilter() porque queremos los datos con otro formato
|
||||
|
||||
<template>
|
||||
<FetchData
|
||||
ref="postcodeFetchDataRef"
|
||||
url="Postcodes/location"
|
||||
@on-fetch="(data) => (postcodesOptions = data)"
|
||||
auto-load
|
||||
/>
|
||||
<FetchData
|
||||
@on-fetch="(data) => (citiesLocationOptions = data)"
|
||||
auto-load
|
||||
url="Towns/location"
|
||||
/>
|
||||
<FetchData
|
||||
@on-fetch="(data) => (provincesLocationOptions = data)"
|
||||
auto-load
|
||||
url="Provinces/location"
|
||||
/>
|
||||
<FetchData
|
||||
@on-fetch="(data) => (countriesOptions = data)"
|
||||
auto-load
|
||||
url="Countries"
|
||||
/>
|
||||
<VnSelectCreate
|
||||
v-model="value"
|
||||
:options="postcodesOptions"
|
||||
:label="t('Location')"
|
||||
:option-label="showLabel"
|
||||
v-bind="$attrs"
|
||||
emit-value
|
||||
map-options
|
||||
use-input
|
||||
@filter="filterHandler"
|
||||
clearable
|
||||
hide-selected
|
||||
fill-input
|
||||
:class="{ required: $attrs.required }"
|
||||
>
|
||||
<template #form>
|
||||
<CreateNewPostcode @on-data-saved="onPostcodeCreated($event)" />
|
||||
</template>
|
||||
<template #option="scope">
|
||||
<QItem v-bind="scope.itemProps">
|
||||
<QItemSection v-if="scope.opt">
|
||||
<QItemLabel>{{ scope.opt.code }}</QItemLabel>
|
||||
<QItemLabel caption
|
||||
>{{ showLabel(scope.opt)}}</QItemLabel
|
||||
>
|
||||
</QItemSection>
|
||||
</QItem>
|
||||
</template>
|
||||
</VnSelectCreate>
|
||||
</template>
|
||||
<style lang="scss" scoped>
|
||||
.add-icon {
|
||||
cursor: pointer;
|
||||
background-color: $primary;
|
||||
border-radius: 50px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<i18n>
|
||||
es:
|
||||
Location: Ubicación
|
||||
</i18n>
|
No veo en que momento se hace el filtrado de datos a las tablas.