Improve retention of grid metadata when updating datasources to avoid resetting state

This commit is contained in:
Andrew Kingston 2023-10-06 12:02:30 +01:00
parent 3564fd5621
commit c2acac36a8
1 changed files with 28 additions and 16 deletions

View File

@ -152,14 +152,20 @@ export const initialise = context => {
return return
} }
const $definition = get(definition) const $definition = get(definition)
const $columns = get(columns)
const $stickyColumn = get(stickyColumn)
// Generate array of all columns to easily find pre-existing columns
let allColumns = $columns || []
if ($stickyColumn) {
allColumns.push($stickyColumn)
}
// Find primary display // Find primary display
let primaryDisplay let primaryDisplay
if ( const candidatePD = $definition.primaryDisplay || $stickyColumn?.name
$definition.primaryDisplay && if (candidatePD && $enrichedSchema[candidatePD]) {
$enrichedSchema[$definition.primaryDisplay] primaryDisplay = candidatePD
) {
primaryDisplay = $definition.primaryDisplay
} }
// Get field list // Get field list
@ -173,14 +179,18 @@ export const initialise = context => {
// Update columns, removing extraneous columns and adding missing ones // Update columns, removing extraneous columns and adding missing ones
columns.set( columns.set(
fields fields
.map(field => ({ .map(field => {
name: field, const fieldSchema = $enrichedSchema[field]
label: $enrichedSchema[field].displayName || field, const oldColumn = allColumns?.find(x => x.name === field)
schema: $enrichedSchema[field], return {
width: $enrichedSchema[field].width || DefaultColumnWidth, name: field,
visible: $enrichedSchema[field].visible ?? true, label: fieldSchema.displayName || field,
order: $enrichedSchema[field].order, schema: fieldSchema,
})) width: fieldSchema.width || oldColumn?.width || DefaultColumnWidth,
visible: fieldSchema.visible ?? true,
order: fieldSchema.order ?? oldColumn?.order,
}
})
.sort((a, b) => { .sort((a, b) => {
// Sort by order first // Sort by order first
const orderA = a.order const orderA = a.order
@ -208,11 +218,13 @@ export const initialise = context => {
stickyColumn.set(null) stickyColumn.set(null)
return return
} }
const stickySchema = $enrichedSchema[primaryDisplay]
const oldStickyColumn = allColumns?.find(x => x.name === primaryDisplay)
stickyColumn.set({ stickyColumn.set({
name: primaryDisplay, name: primaryDisplay,
label: $enrichedSchema[primaryDisplay].displayName || primaryDisplay, label: stickySchema.displayName || primaryDisplay,
schema: $enrichedSchema[primaryDisplay], schema: stickySchema,
width: $enrichedSchema[primaryDisplay].width || DefaultColumnWidth, width: stickySchema.width || oldStickyColumn?.width || DefaultColumnWidth,
visible: true, visible: true,
order: 0, order: 0,
left: GutterWidth, left: GutterWidth,