From d1684b4ae6871c92c5c91cd52b3972c4d4902a59 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 25 Nov 2024 16:01:24 +0100 Subject: [PATCH] feat: refs #8113 add map-key --- src/components/ui/VnPaginate.vue | 9 ++++++--- src/composables/useArrayData.js | 3 ++- src/stores/useArrayDataStore.js | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/ui/VnPaginate.vue b/src/components/ui/VnPaginate.vue index f3f6d64f1..a7c3f77e7 100644 --- a/src/components/ui/VnPaginate.vue +++ b/src/components/ui/VnPaginate.vue @@ -74,8 +74,11 @@ const props = defineProps({ type: Boolean, default: false, }, + mapKey: { + type: String, + default: 'id', + }, }); - const emit = defineEmits(['onFetch', 'onPaginate', 'onChange']); const isLoading = ref(false); const mounted = ref(false); @@ -84,7 +87,6 @@ const pagination = ref({ rowsPerPage: props.limit, page: 1, }); - const arrayData = useArrayData(props.dataKey, { url: props.url, filter: props.filter, @@ -96,9 +98,10 @@ const arrayData = useArrayData(props.dataKey, { exprBuilder: props.exprBuilder, keepOpts: props.keepOpts, searchUrl: props.searchUrl, + mapKey: props.mapKey, }); const store = arrayData.store; - +console.log('store', store); onMounted(async () => { if (props.autoLoad && !store.data?.length) await fetch(); mounted.value = true; diff --git a/src/composables/useArrayData.js b/src/composables/useArrayData.js index e51c135b4..14d5e5b98 100644 --- a/src/composables/useArrayData.js +++ b/src/composables/useArrayData.js @@ -49,6 +49,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { 'exprBuilder', 'searchUrl', 'navigate', + 'mapKey', ]; if (typeof userOptions === 'object') { for (const option in userOptions) { @@ -294,7 +295,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { function mapData(data) { data.forEach((row) => { - const key = row.id; + const key = row[store.mapKey]; const val = { ...row, key }; if (store.map.has(key)) { diff --git a/src/stores/useArrayDataStore.js b/src/stores/useArrayDataStore.js index 8301f3a6f..d0a1c3a8f 100644 --- a/src/stores/useArrayDataStore.js +++ b/src/stores/useArrayDataStore.js @@ -17,6 +17,7 @@ export const useArrayDataStore = defineStore('arrayDataStore', () => { searchUrl: 'params', navigate: null, page: 1, + mapKey: 'id', }; function get(key) {