gregnb / gregnb/mui-datatables

Persist table: manually shown columns don't remain visible after refresh

Open
#1,924 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
2.7k
Forks
906
PR merge metrics
No merged PRs in 30d

Description

1. Use `storageKey` to persist the table state.
2. Have 1 column with `display: 'false'` (in the repo: the 'title' column)
3. use the "viewColumns" to add that column to view
4. refresh the page

## Expected Behavior
1. The column that we marked to view should remain in view after refresh

## Current Behavior
1. The column that we marked to view doesn't remain in view after refresh
2. _Hiding_ columns does work, they are not returned to view after refresh

## Steps to Reproduce (for bugs)
https://codesandbox.io/s/muidatatables-persist-table-bug-kcrqyu

```typescript
columns = [
...
{ name: 'Title', options: { display: 'false' } },
...
]

options = {
...
storageKey: 'some-key'
...
}
```

## Your Environment

| Tech | Version |
|--------------|---------|
| Material-UI | 5.x |
| MUI-datatables | 4.1.0 (also 3.8.0) |
| React | 17.0.2 |
| browser | * |

---

With very shallow reading of the source code, it seems like `setTableData` always passes `props.columns` instead of checking `this.state.columns`, which holds the stored state.

```diff
+ const columnsToUse = this.state.columns || props.columns;
let { columns, filterData, filterList, columnOrder } = this.buildColumns(
- props.columns,
+ columnsToUse,
this.state.columns,
```

and
```diff
if (!sortOrder.direction) {
- props.columns.forEach((column, colIndex) => {
+ columnsToUse.forEach((column, colIndex) => {
```

manually changing this piece of code seems making this bug fixed, but I really didn't checked more complex cases.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing setTableData and buildColumns, especially how this.state.columns and props.columns are selected after storageKey state is restored. Reproduce the issue with the linked CodeSandbox and verify that a column initially set to display:false, then enabled through viewColumns, remains visible after refresh without breaking column hiding.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.