This commit is contained in:
parent
14f2143304
commit
b6f68321af
|
@ -1,6 +1,7 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { onMounted, ref } from 'vue';
|
import { onMounted, ref } from 'vue';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { useArrayData } from 'composables/useArrayData';
|
||||||
|
|
||||||
const $props = defineProps({
|
const $props = defineProps({
|
||||||
autoLoad: {
|
autoLoad: {
|
||||||
|
@ -35,43 +36,61 @@ const $props = defineProps({
|
||||||
|
|
||||||
const emit = defineEmits(['onFetch']);
|
const emit = defineEmits(['onFetch']);
|
||||||
defineExpose({ fetch, paginate });
|
defineExpose({ fetch, paginate });
|
||||||
|
|
||||||
|
const arrayData = useArrayData($props.url ?? $props.dataKey, {
|
||||||
|
url: $props.url,
|
||||||
|
filter: $props.filter,
|
||||||
|
where: $props.where,
|
||||||
|
limit: $props.limit,
|
||||||
|
order: $props.order,
|
||||||
|
userParams: $props.userParams,
|
||||||
|
exprBuilder: $props.exprBuilder,
|
||||||
|
});
|
||||||
|
const store = arrayData.store;
|
||||||
|
|
||||||
const pagination = ref({
|
const pagination = ref({
|
||||||
sortBy: $props.order,
|
sortBy: $props.order,
|
||||||
rowsPerPage: $props.limit,
|
rowsPerPage: +$props.limit,
|
||||||
page: 1,
|
page: 1,
|
||||||
|
hasMoreData: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if ($props.autoLoad) {
|
if ($props.autoLoad) {
|
||||||
await fetch();
|
await fetch();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
async function fetchMore(fetchFilter = {}) {
|
|
||||||
try {
|
|
||||||
const filter = await fetch(filter);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async function paginate() {
|
async function paginate() {
|
||||||
await fetch({
|
if (!pagination.value.hasMoreData) return emit('onFetch', []);
|
||||||
skip: pagination.value.page * $props.limit,
|
await fetch(
|
||||||
});
|
{
|
||||||
|
skip: pagination.value.page * $props.limit,
|
||||||
|
},
|
||||||
|
{ append: true }
|
||||||
|
);
|
||||||
pagination.value.page += 1;
|
pagination.value.page += 1;
|
||||||
}
|
}
|
||||||
async function fetch(fetchFilter = {}) {
|
|
||||||
|
async function fetch(fetchFilter = {}, options = { append: false }) {
|
||||||
try {
|
try {
|
||||||
const filter = Object.assign(fetchFilter, $props.filter); // eslint-disable-line vue/no-dupe-keys
|
const filter = Object.assign(fetchFilter, $props.filter); // eslint-disable-line vue/no-dupe-keys
|
||||||
if ($props.where && !fetchFilter.where) filter.where = $props.where;
|
if ($props.where && !fetchFilter.where) filter.where = $props.where;
|
||||||
if ($props.sortBy) filter.order = $props.sortBy;
|
if ($props.sortBy) filter.order = $props.sortBy;
|
||||||
if ($props.limit) filter.limit = $props.limit;
|
if ($props.limit) filter.limit = $props.limit;
|
||||||
if ($props.skip) filter.skip = $props.skip;
|
if ($props.skip) filter.skip = $props.skip;
|
||||||
// if (Object.keys(fetchFilter.where).length < 1) delete filter.where;
|
|
||||||
|
|
||||||
const { data } = await axios.get($props.url, {
|
const { data } = await axios.get($props.url, {
|
||||||
params: { filter: JSON.stringify(filter), ...$props.params },
|
params: { filter: JSON.stringify(filter), ...$props.params },
|
||||||
});
|
});
|
||||||
|
pagination.value.hasMoreData = data.length === pagination.value.rowsPerPage;
|
||||||
|
|
||||||
|
if (options.append) {
|
||||||
|
store.data.push(...data);
|
||||||
|
} else store.data = data;
|
||||||
|
|
||||||
emit('onFetch', data);
|
emit('onFetch', data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//
|
//
|
||||||
|
|
|
@ -83,10 +83,9 @@ const value = computed({
|
||||||
});
|
});
|
||||||
|
|
||||||
function setOptions(data) {
|
function setOptions(data) {
|
||||||
if (loading.value) {
|
if (isLoading.value) {
|
||||||
data.unshift(...myOptions.value);
|
data.unshift(...myOptions.value);
|
||||||
// myOptions.value.push(...data);
|
isLoading.value = false;
|
||||||
loading.value = false;
|
|
||||||
}
|
}
|
||||||
myOptions.value = JSON.parse(JSON.stringify(data));
|
myOptions.value = JSON.parse(JSON.stringify(data));
|
||||||
myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
|
myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
|
||||||
|
@ -155,22 +154,25 @@ watch(modelValue, (newValue) => {
|
||||||
if (!myOptions.value.some((option) => option[optionValue.value] == newValue))
|
if (!myOptions.value.some((option) => option[optionValue.value] == newValue))
|
||||||
fetchFilter(newValue);
|
fetchFilter(newValue);
|
||||||
});
|
});
|
||||||
const loading = ref(false);
|
const isLoading = ref(false);
|
||||||
|
|
||||||
async function onScroll({ to, ref }) {
|
async function onScroll(scrollEv) {
|
||||||
|
const { to, ref, direction } = scrollEv;
|
||||||
const lastIndex = myOptions.value.length - 1;
|
const lastIndex = myOptions.value.length - 1;
|
||||||
const optionIndex = ref.getOptionIndex();
|
const optionIndex = ref.getOptionIndex();
|
||||||
|
|
||||||
console.log(lastIndex, to);
|
console.log(lastIndex, to, optionIndex);
|
||||||
if (optionIndex > 0 && to === lastIndex) {
|
if (direction === 'decrease') return;
|
||||||
loading.value === false && emit('onPaginate');
|
if (optionIndex > 0 && to === lastIndex && isLoading.value === false) {
|
||||||
loading.value = true;
|
isLoading.value = true;
|
||||||
|
emit('onPaginate');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<FetchData
|
<FetchData
|
||||||
|
v-if="$props.url"
|
||||||
ref="dataRef"
|
ref="dataRef"
|
||||||
:url="$props.url"
|
:url="$props.url"
|
||||||
@on-fetch="(data) => setOptions(data)"
|
@on-fetch="(data) => setOptions(data)"
|
||||||
|
@ -180,7 +182,7 @@ async function onScroll({ to, ref }) {
|
||||||
:fields="fields"
|
:fields="fields"
|
||||||
/>
|
/>
|
||||||
<QSelect
|
<QSelect
|
||||||
:loading="loading"
|
:loading="isLoading"
|
||||||
@virtual-scroll="onScroll"
|
@virtual-scroll="onScroll"
|
||||||
v-model="value"
|
v-model="value"
|
||||||
:options="myOptions"
|
:options="myOptions"
|
||||||
|
|
Loading…
Reference in New Issue