Hide Columns by Default with ProTable | ATLASSC.NET
Skip to main content
/// .DATA.FRAGMENT_20230223

Hide Columns by Default with ProTable

1 minAuthor: Z.SHINCHVEN

ProComponents is a set of enterprise-class UI components based on Ant Design and React, it provides a lot of useful components and features out of the box.

I've enjoyed building data table UI with ProTable which provides a lot of useful features like Controlled table settings columns for you to implement hide some columns by default feature within a few lines of codes.

Here's the code snippet:

Generate Columns Control States with Given Array of Column Names

import type {ColumnsState} from '@ant-design/pro-components';

export const makeDefaultColumnsState = (columnsToHide: string[]) => {
  const columnsState: Record<string, ColumnsState> = {};
  columnsToHide.forEach((column) => {
    // Hide the column by default
    columnsState[column] = {show: false};
  });
  return columnsState;
}

Use the Generated Columns States

const columnsToHide = ['column1', 'column2'];

const MyTable = () => {
  // initialize and store the columns states for future changes
  const [columnsStateMap, setColumnsStateMap] = useState<ColumnsState>(makeDefaultColumnsState(columnsToHide));

  return <ProTable
    columnsState={{
      value: columnsStateMap,
      onChange: setColumnsStateMap, // handle the columns states changes
    }}
  />
}
RackNerd Billboard Banner
Share node:
/// .RELATED.DATA_STREAMSShared tags

Related posts

Scanning_database_for_correlations…

/// .UPLINK.SUBSCRIBE

Keep your deck synced with every new transmission from ATLASSC.NET. Subscribe over RSS or Atom, or wire the whole archive straight into your AI assistant with the MCP server.