From 5b448e5bb37593aa0ac6f581b336b694404314ef Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 24 Jan 2025 08:24:12 +0100 Subject: [PATCH] fix: order when is string --- src/composables/useArrayData.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/composables/useArrayData.js b/src/composables/useArrayData.js index 412e17598..3c3a283f1 100644 --- a/src/composables/useArrayData.js +++ b/src/composables/useArrayData.js @@ -93,8 +93,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { Object.assign(params, userParams); if (params.filter) params.filter.skip = store.skip; - if (store?.order && typeof store?.order == 'string') store.order = [store.order]; - if (store.order?.length) params.filter.order = [...store.order]; + if (store.order?.length) params.filter.order = [...toArray(store.order)]; else delete params.filter.order; store.currentFilter = JSON.parse(JSON.stringify(params)); @@ -191,10 +190,9 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { async function addOrder(field, direction = 'ASC') { const newOrder = field + ' ' + direction; - let order = store.order || []; - if (typeof order == 'string') order = [order]; + const order = toArray(store.order); - let index = order.findIndex((o) => o.split(' ')[0] === field); + let index = getOrderIndex(order, field); if (index > -1) { order[index] = newOrder; } else { @@ -211,16 +209,23 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { } async function deleteOrder(field) { - let order = store.order ?? []; - if (typeof order == 'string') order = [order]; - - const index = order.findIndex((o) => o.split(' ')[0] === field); + const order = toArray(store.order); + const index = getOrderIndex(order, field); if (index > -1) order.splice(index, 1); store.order = order; fetch({}); } + function getOrderIndex(order, field) { + return order.findIndex((o) => o.split(' ')[0] === field); + } + + function toArray(str = []) { + if (Array.isArray(str)) return str; + if (typeof str === 'string') return str.split(',').map((item) => item.trim()); + } + function sanitizerParams(params, exprBuilder) { for (const param in params) { if (params[param] === '' || params[param] === null) { -- 2.40.1