diff --git a/src/store/data.js b/src/store/data.js index 470bd9dc6c..ff5b41ccbd 100644 --- a/src/store/data.js +++ b/src/store/data.js @@ -47,6 +47,10 @@ export const useDataStore = defineStore('data', { const stateId = typeof elementId === 'string' && elementId.startsWith('public-') ? elementId : genStateKey(isView, elementId) return state.rows[stateId] ?? [] }, + hasRows: (state) => (isView, elementId) => { + const stateId = typeof elementId === 'string' && elementId.startsWith('public-') ? elementId : genStateKey(isView, elementId) + return Object.hasOwn(state.rows, stateId) + }, getRelations: (state) => (columnId) => { if (state.relations[columnId] === undefined) { state.relations[columnId] = {} @@ -104,27 +108,50 @@ export const useDataStore = defineStore('data', { return columns }, + async loadViewWithColumnSettings(view) { + if (!view || Array.isArray(view.columnSettings)) { + return view + } + + const cachedView = useTablesStore().getView(parseInt(view.id)) + if (cachedView && Array.isArray(cachedView.columnSettings)) { + return { ...view, ...cachedView } + } + + try { + const res = await axios.get(generateUrl('/apps/tables/view/' + view.id)) + return { ...view, ...res.data } + } catch (e) { + displayError(e, t('tables', 'Could not load view.')) + return view + } + }, + async loadColumnsFromBE({ view, tableId }) { - let allColumns = await this.getColumnsFromBE({ tableId, viewId: view?.id }) - if (view) { - // Transform array to object for faster access - const columnSettingsMap = view.columnSettings?.reduce((acc, item) => { + const viewWithColumnSettings = await this.loadViewWithColumnSettings(view) + let allColumns = await this.getColumnsFromBE({ tableId, viewId: viewWithColumnSettings?.id }) + if (viewWithColumnSettings) { + // Meta columns aren't real DB columns, so they never come back + // from the fetch above -- append any this view has settings for. + const columnSettingsMap = viewWithColumnSettings.columnSettings?.reduce((acc, item) => { acc[item.columnId] = item return acc }, {}) ?? {} - allColumns = allColumns.concat(MetaColumns.filter(col => columnSettingsMap[col.id])) - if (view.columnSettings) { - allColumns = allColumns.sort((a, b) => { - const orderA = columnSettingsMap[a.id]?.order ?? Number.MAX_SAFE_INTEGER - const orderB = columnSettingsMap[b.id]?.order ?? Number.MAX_SAFE_INTEGER - return orderA - orderB - }) - } + + // Real columns carry their own order via viewColumnInformation; + // meta columns fall back to columnSettingsMap since they were + // just concatenated above and never went through server-side + // enhancement. + allColumns = allColumns.sort((a, b) => { + const orderA = a.viewColumnInformation?.order ?? columnSettingsMap[a.id]?.order ?? Number.MAX_SAFE_INTEGER + const orderB = b.viewColumnInformation?.order ?? columnSettingsMap[b.id]?.order ?? Number.MAX_SAFE_INTEGER + return orderA - orderB + }) } else { // no view: keep the backend-ordered result (ColumnService::findAllByTable already applies columnOrder) } - const stateId = genStateKey(!!(view?.id), view?.id ?? tableId) + const stateId = genStateKey(!!(viewWithColumnSettings?.id), viewWithColumnSettings?.id ?? tableId) this.columns[stateId] = allColumns return true }, diff --git a/src/views/ContentReferenceWidget.vue b/src/views/ContentReferenceWidget.vue index f4749867d1..661c68990d 100644 --- a/src/views/ContentReferenceWidget.vue +++ b/src/views/ContentReferenceWidget.vue @@ -18,18 +18,19 @@