Beautiful Open-Source Form Builder
Enhance column management in OpenTable component and related state handling
JhumanJ7ccff78a54a3
diff --git a/client/components/open/tables/OpenTable.vue b/client/components/open/tables/OpenTable.vueindex 44de913ec..1d3ec4401 100644--- a/client/components/open/tables/OpenTable.vue+++ b/client/components/open/tables/OpenTable.vue@@ -83,6 +83,7 @@v-if="form"ref="table"v-model:row-selection="rowSelection"+ v-model:column-order="columnOrder":columns="allColumns":column-visibility="columnVisibility":column-pinning="columnPinning"@@ -213,9 +214,10 @@ const tableState = useTableState(canModify.value)-const { tableColumns: allColumns, columnVisibility, columnPinning, columnSizing, columnWrapping, handleColumnResize: handleColumnResizeState } = tableState+const { tableColumns: allColumns, columnVisibility, columnPinning, columnSizing, columnWrapping, columnOrder, handleColumnResize: handleColumnResizeState } = tableStateconst tableColumns = computed(() => {+ console.log('tableColumns changed')return allColumns.value.filter(column => !['actions', 'select'].includes(column.id))})diff --git a/client/composables/components/tables/useTableColumnPreferences.js b/client/composables/components/tables/useTableColumnPreferences.jsindex d226df933..5acc8b8da 100644--- a/client/composables/components/tables/useTableColumnPreferences.js+++ b/client/composables/components/tables/useTableColumnPreferences.js@@ -71,23 +71,6 @@ export function useTableColumnPreferences(formId) {}}- const setColumnsOrder = (newOrder) => {- if (import.meta.server) return-- const newColumns = { ...preferences.value.columns }- newOrder.forEach((columnId, index) => {- newColumns[columnId] = {- ...getColumnPreference(columnId),- order: index- }- })-- preferences.value = {- ...preferences.value,- columns: newColumns- }- }-const setColumnSizing = (newSizing) => {if (import.meta.server) returnpreferences.value = {@@ -115,7 +98,6 @@ export function useTableColumnPreferences(formId) {preferences: computed(() => preferences.value),getColumnPreference,setColumnPreference,- setColumnsOrder,setColumnSizing,resetPreferences,resetColumn,diff --git a/client/composables/components/tables/useTableState.js b/client/composables/components/tables/useTableState.jsindex 9c4e6e22a..64a58a73f 100644--- a/client/composables/components/tables/useTableState.js+++ b/client/composables/components/tables/useTableState.js@@ -211,85 +211,92 @@ export function useTableState(form, withActions = false) {/* Column ordering *//* -------------------------------------------------------------------------- */- // Helper function to set a specific column to a specific index+ // Column order for TanStack Table (array of column IDs)+ const columnOrder = computed({+ get() {+ try {+ // Return all column IDs in the correct order+ const cols = tableColumns.value || []+ return cols.map(col => col.id)+ } catch (error) {+ console.error('Error in columnOrder computed:', error)+ return []+ }+ },+ set(newOrder) {+ try {+ // newOrder is an array of column IDs+ // Filter out 'select' and 'actions' columns as they're managed separately+ const dataColumnIds = newOrder.filter(id => !['select', 'actions'].includes(id))++ // Update preferences for each column based on the new order+ dataColumnIds.forEach((id, index) => {+ columnPreferences.setColumnPreference(id, { order: index })+ })++ // Give high order numbers to columns not in the new order+ const allColumnIds = columnConfigurations.value.map(c => c.id)+ allColumnIds.forEach(id => {+ if (!dataColumnIds.includes(id)) {+ columnPreferences.setColumnPreference(id, { order: 9999 })+ }+ })+ } catch (error) {+ console.error('Error in columnOrder setter:', error)+ }+ }+ })++ // Helper function to set a specific column to a specific index (used by drag-and-drop)const setColumnOrder = (columnId, newIndex) => {try {- // Get the current list of VISIBLE column IDs, already in the correct order- let visibleColumnIds = orderedColumns.value- .filter(c => columnVisibility.value[c.id] !== false)- .map(c => c.id)-- // Perform the move on this array- const currentIndex = visibleColumnIds.indexOf(columnId)- if (currentIndex > -1) {- visibleColumnIds.splice(currentIndex, 1)- }- visibleColumnIds.splice(newIndex, 0, columnId)-- // Update the preferences based on the new order- const allColumnIds = columnConfigurations.value.map(c => c.id)- allColumnIds.forEach(id => {- const visibleIndex = visibleColumnIds.indexOf(id)- if (visibleIndex !== -1) {- // It's visible, set its order- columnPreferences.setColumnPreference(id, { order: visibleIndex })- } else {- // It's hidden, give it a high order number- columnPreferences.setColumnPreference(id, { order: 9999 })- }- })+ // Get current column order+ const currentOrder = columnOrder.value++ // Find the column in the current order+ const currentIndex = currentOrder.indexOf(columnId)+ if (currentIndex === -1) return++ // Create new order by moving the column+ const newOrder = [...currentOrder]+ newOrder.splice(currentIndex, 1)+ newOrder.splice(newIndex, 0, columnId)++ // Update via columnOrder setter (triggers TanStack Table update)+ columnOrder.value = newOrder} catch (error) {console.error('Error in setColumnOrder:', error)}}- // Ordered columns for display- const orderedColumns = computed({- get() {- try {- const prefs = columnPreferences.preferences.value- const configCols = columnConfigurations.value || []- const columns = Array.isArray(configCols) ? [...configCols] : []-- // Sort by order preference, then by original position- columns.sort((a, b) => {- const prefA = prefs.columns[a.id] || {}- const prefB = prefs.columns[b.id] || {}-- const orderA = prefA.order ?? 9999- const orderB = prefB.order ?? 9999+ // Ordered columns for display (read-only getter)+ const orderedColumns = computed(() => {+ try {+ const prefs = columnPreferences.preferences.value+ const configCols = columnConfigurations.value || []+ const columns = Array.isArray(configCols) ? [...configCols] : []- if (orderA !== orderB) {- return orderA - orderB- }+ // Sort by order preference, then by original position+ columns.sort((a, b) => {+ const prefA = prefs.columns[a.id] || {}+ const prefB = prefs.columns[b.id] || {}- // Fallback to original order- return configCols.indexOf(a) - configCols.indexOf(b)- })+ const orderA = prefA.order ?? 9999+ const orderB = prefB.order ?? 9999- return columns- } catch (error) {- console.error('Error in orderedColumns computed:', error)- return []- }- },- set(newOrderColumns) {- // Logic to update column order preferences- const allColumnIds = (columnConfigurations.value || []).map(c => c.id)- const newOrderIds = newOrderColumns.map(c => c.id)-- allColumnIds.forEach(id => {- const order = newOrderIds.indexOf(id)- if (order !== -1) {- // If the column is in the new order, set its order- columnPreferences.setColumnPreference(id, { order })- } else {- // If it's not (e.g., it's a hidden column not in the ordered list),- // assign a high order number to keep it at the end if it becomes visible- columnPreferences.setColumnPreference(id, { order: 9999 })+ if (orderA !== orderB) {+ return orderA - orderB}++ // Fallback to original order+ return configCols.indexOf(a) - configCols.indexOf(b)})- },++ return columns+ } catch (error) {+ console.error('Error in orderedColumns computed:', error)+ return []+ }})// Final array of columns to be passed to the table component@@ -407,6 +414,7 @@ export function useTableState(form, withActions = false) {columnPinning,columnWrapping,columnSizing,+ columnOrder, // TanStack Table column order state// Preference helpers (for components)getColumnPreference,@@ -416,8 +424,6 @@ export function useTableState(form, withActions = false) {toggleColumnVisibility: toggleColumnVisibility,toggleColumnWrapping,toggleColumnPin: toggleColumnPin,- toggleColumnWrap: columnPreferences.toggleColumnWrap,- setColumnsOrder: columnPreferences.setColumnsOrder,handleColumnResize,}}\ No newline at end of file