Column Instance APIs
Each column has a
column
instance object associated with it that can be accessed in callback props and other places in the table.You can access and use a
column
instance in quite a few places, but here are some of the most common:const columns = [{accessorKey: 'username',header: 'Username',//you can access a column instance in many callback functions in a column definition like thismantineTableHeadCellProps: ({ column }) => ({sx: {color: column.getIsSorted() ? 'red' : 'black',},}),//or in the component override callbacksHeader: ({ column }) => <div>{column.columnDef.header}</div>,Cell: ({ cell, column }) => (<Boxsx={{backgroundColor: column.getIsGrouped() ? 'green' : 'white',}}>{cell.getValue()}</Box>),},];const table = useMantineReactTable({columns,data,mantineTableBodyCellProps: ({ column }) => ({sx: { boxShadow: column.getIsPinned() ? '0 0 0 2px red' : 'none' },}),});return <MantineReactTable table={table} />;
NOTE: These are NOT column options for Mantine React Table. These are just static methods on a column instance that you can use.
Wanna see the source code for this table? Check it out down below!
1import { useEffect, useMemo, useState } from 'react';2import Link from 'next/link';3import {4MantineReactTable,5type MRT_ColumnDef,6type MRT_Column,7} from 'mantine-react-table';8import { Anchor, Text } from '@mantine/core';9import { useMediaQuery } from '@mantine/hooks';10import { SampleCodeSnippet } from '../mdx/SampleCodeSnippet';11import {12type ColumnInstanceAPI,13columnInstanceAPIs,14} from './columnInstanceAPIs';1516interface Props {17onlyOptions?: Set<keyof MRT_Column<ColumnInstanceAPI>>;18}1920const ColumnInstanceAPIsTable = ({ onlyOptions }: Props) => {21const isDesktop = useMediaQuery('(min-width: 1200px)');2223const columns = useMemo(24() =>25[26{27accessorKey: 'columnInstanceAPI',28enableClickToCopy: true,29header: 'State Option',30mantineCopyButtonProps: ({ cell }) => ({31className: 'column-instance-api',32id: `${cell.getValue<string>()}-column-instance-api`,33}),34},35{36accessorKey: 'type',37header: 'Type',38enableGlobalFilter: false,39Cell: ({ cell }) => (40<SampleCodeSnippet language="typescript" noCopy>41{cell.getValue<string>()}42</SampleCodeSnippet>43),44},45{46accessorKey: 'link',47disableFilters: true,48enableGlobalFilter: false,49header: 'More Info Links',50Cell: ({ cell, row }) => (51<Link href={cell.getValue() as string} passHref legacyBehavior>52<Anchor53target={54(cell.getValue() as string).startsWith('http')55? '_blank'56: undefined57}58rel="noopener"59>60{row.original?.linkText}61</Anchor>62</Link>63),64},65] as MRT_ColumnDef<ColumnInstanceAPI>[],66[],67);6869const [columnPinning, setColumnPinning] = useState({});7071useEffect(() => {72if (typeof window !== 'undefined') {73if (isDesktop) {74setColumnPinning({75left: ['mrt-row-expand', 'mrt-row-numbers', 'columnInstanceAPI'],76right: ['link'],77});78} else {79setColumnPinning({});80}81}82}, [isDesktop]);8384const data = useMemo(() => {85if (onlyOptions) {86return columnInstanceAPIs.filter(({ columnInstanceAPI }) =>87onlyOptions.has(columnInstanceAPI),88);89}90return columnInstanceAPIs;91}, [onlyOptions]);9293return (94<MantineReactTable95columns={columns}96data={data}97displayColumnDefOptions={{98'mrt-row-numbers': {99size: 10,100},101'mrt-row-expand': {102size: 10,103},104}}105enableColumnActions={!onlyOptions}106enableColumnFilterModes107enablePagination={false}108enablePinning109enableRowNumbers110enableBottomToolbar={false}111enableTopToolbar={!onlyOptions}112initialState={{113columnVisibility: { description: false },114density: 'xs',115showGlobalFilter: true,116sorting: [{ id: 'columnInstanceAPI', desc: false }],117}}118mantineSearchTextInputProps={{119placeholder: 'Search Column APIs',120sx: { minWidth: '18rem' },121variant: 'filled',122}}123mantinePaperProps={{124sx: { marginBottom: '24px' },125id: onlyOptions126? 'relevant-column-instance-apis-table'127: 'column-instance-apis-table',128}}129positionGlobalFilter="left"130renderDetailPanel={({ row }) => (131<Text color={row.original.description ? 'teal' : 'gray'}>132{row.original.description || 'No Description Provided... Yet...'}133</Text>134)}135rowNumberMode="static"136onColumnPinningChange={setColumnPinning}137state={{ columnPinning }}138/>139);140};141142export default ColumnInstanceAPIsTable;