|
| 1 | +# DataTable Column Visibility Customization Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The new `@medusajs/ui` DataTable component provides built-in column visibility functionality through `Primitive.ColumnVisibilityMenu`. However, this component doesn't support custom implementations out of the box. This guide explains how to integrate custom column visibility with the DataTable component. |
| 6 | + |
| 7 | +## Current Architecture |
| 8 | + |
| 9 | +### Default DataTable Component |
| 10 | +The default DataTable from `@medusajs/ui` uses: |
| 11 | +- `Primitive.ColumnVisibilityMenu` - A built-in component that cannot be customized |
| 12 | +- `columnVisibility` state managed through `useDataTable` hook |
| 13 | +- No props to pass custom column visibility components |
| 14 | + |
| 15 | +### Custom Column Visibility Requirements |
| 16 | +To integrate with the column visibility API, we need: |
| 17 | +1. Access to API column metadata (names, IDs, etc.) |
| 18 | +2. Ability to save/load column preferences |
| 19 | +3. Custom UI for column selection |
| 20 | +4. Integration with the table's visibility state |
| 21 | + |
| 22 | +## Solution Approaches |
| 23 | + |
| 24 | +### Approach 1: Custom DataTable Wrapper (Recommended) |
| 25 | + |
| 26 | +Create a wrapper component that replaces the built-in column visibility menu with a custom implementation: |
| 27 | + |
| 28 | +```tsx |
| 29 | +// data-table-with-custom-columns.tsx |
| 30 | +import { DataTable as Primitive, useDataTable } from "@medusajs/ui" |
| 31 | +import { CustomColumnVisibility } from "./custom-column-visibility" |
| 32 | + |
| 33 | +export const DataTableWithCustomColumns = (props) => { |
| 34 | + const instance = useDataTable({ |
| 35 | + // ... table configuration |
| 36 | + columnVisibility: { |
| 37 | + state: columnVisibility, |
| 38 | + onColumnVisibilityChange: setColumnVisibility, |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + return ( |
| 43 | + <Primitive instance={instance}> |
| 44 | + <Primitive.Toolbar> |
| 45 | + {/* Replace Primitive.ColumnVisibilityMenu with custom component */} |
| 46 | + {enableColumnVisibility && ( |
| 47 | + <CustomColumnVisibility |
| 48 | + table={instance} |
| 49 | + apiColumns={apiColumns} |
| 50 | + onSaveColumns={onSaveColumns} |
| 51 | + /> |
| 52 | + )} |
| 53 | + </Primitive.Toolbar> |
| 54 | + <Primitive.Table /> |
| 55 | + </Primitive> |
| 56 | + ) |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Approach 2: Extend Existing DataTable |
| 61 | + |
| 62 | +Extend the existing DataTable component with additional props: |
| 63 | + |
| 64 | +```tsx |
| 65 | +// Extended DataTable usage |
| 66 | +<DataTable |
| 67 | + // ... standard props |
| 68 | + enableColumnVisibility={true} |
| 69 | + apiColumns={apiColumns} |
| 70 | + onSaveColumns={handleSaveColumns} |
| 71 | +/> |
| 72 | +``` |
| 73 | + |
| 74 | +### Approach 3: Hook-based Column Management |
| 75 | + |
| 76 | +Create a hook that manages column visibility state: |
| 77 | + |
| 78 | +```tsx |
| 79 | +const useColumnVisibilityManager = (entity: string) => { |
| 80 | + // Load/save column preferences |
| 81 | + // Sync with API |
| 82 | + // Return visibility state and handlers |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Implementation Details |
| 87 | + |
| 88 | +### Custom Column Visibility Component |
| 89 | + |
| 90 | +The custom component should: |
| 91 | +1. Display column names from the API |
| 92 | +2. Allow toggling individual columns |
| 93 | +3. Support "toggle all" functionality |
| 94 | +4. Save preferences when changed |
| 95 | +5. Show loading state while fetching column metadata |
| 96 | + |
| 97 | +```tsx |
| 98 | +export const CustomColumnVisibility = ({ |
| 99 | + table, |
| 100 | + apiColumns, |
| 101 | + isLoading, |
| 102 | + onSaveColumns, |
| 103 | +}) => { |
| 104 | + // Get hideable columns from table |
| 105 | + const columns = table.getAllColumns().filter(col => col.getCanHide()) |
| 106 | + |
| 107 | + // Handle column toggle |
| 108 | + const handleToggle = (columnId) => { |
| 109 | + table.getColumn(columnId)?.toggleVisibility() |
| 110 | + // Save preferences |
| 111 | + const visibleColumns = getVisibleColumns() |
| 112 | + onSaveColumns(visibleColumns) |
| 113 | + } |
| 114 | + |
| 115 | + // Render dropdown with column list |
| 116 | + return ( |
| 117 | + <DropdownMenu> |
| 118 | + {/* Column checkboxes */} |
| 119 | + </DropdownMenu> |
| 120 | + ) |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Integration with Column API |
| 125 | + |
| 126 | +Use the views API to get column metadata: |
| 127 | + |
| 128 | +```tsx |
| 129 | +import { useOrderColumns } from "../../hooks/api/views" |
| 130 | + |
| 131 | +const OrderTable = () => { |
| 132 | + const { columns: apiColumns, isLoading } = useOrderColumns() |
| 133 | + |
| 134 | + return ( |
| 135 | + <DataTableWithCustomColumns |
| 136 | + apiColumns={apiColumns} |
| 137 | + apiColumnsLoading={isLoading} |
| 138 | + onSaveColumns={saveColumnPreferences} |
| 139 | + /> |
| 140 | + ) |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +## Usage Example |
| 145 | + |
| 146 | +```tsx |
| 147 | +import { DataTableWithCustomColumns } from "./data-table-with-custom-columns" |
| 148 | +import { useOrderColumns } from "../../hooks/api/views" |
| 149 | + |
| 150 | +export const OrderListTable = () => { |
| 151 | + const { columns: apiColumns } = useOrderColumns() |
| 152 | + |
| 153 | + const handleSaveColumns = async (columns) => { |
| 154 | + // Save to local storage |
| 155 | + localStorage.setItem('order-columns', JSON.stringify(columns)) |
| 156 | + |
| 157 | + // Or save to API |
| 158 | + await sdk.admin.views.updateColumns('orders', columns) |
| 159 | + } |
| 160 | + |
| 161 | + return ( |
| 162 | + <DataTableWithCustomColumns |
| 163 | + data={orders} |
| 164 | + columns={tableColumns} |
| 165 | + enableColumnVisibility |
| 166 | + apiColumns={apiColumns} |
| 167 | + onSaveColumns={handleSaveColumns} |
| 168 | + /> |
| 169 | + ) |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Benefits |
| 174 | + |
| 175 | +1. **Full control** over column visibility UI |
| 176 | +2. **Integration** with backend column metadata API |
| 177 | +3. **Persistence** of user preferences |
| 178 | +4. **Consistent** with existing DataTable API |
| 179 | +5. **Reusable** across different table implementations |
| 180 | + |
| 181 | +## Limitations |
| 182 | + |
| 183 | +1. Requires maintaining a custom DataTable wrapper |
| 184 | +2. Must keep in sync with @medusajs/ui updates |
| 185 | +3. Cannot use the built-in Primitive.ColumnVisibilityMenu |
| 186 | + |
| 187 | +## Future Considerations |
| 188 | + |
| 189 | +Ideally, the `@medusajs/ui` library would support: |
| 190 | +- Custom render props for column visibility |
| 191 | +- Column metadata integration |
| 192 | +- Persistence callbacks |
| 193 | +- Custom component injection |
| 194 | + |
| 195 | +Until then, the wrapper approach provides the most flexibility while maintaining compatibility with the existing DataTable API. |
0 commit comments