Examples › Handling cell clicks

If you need to know not only which row, but also which column was clicked, pass an onCellClick handler to the DataTable. The handler receives an object with the following properties:

  • record → the clicked record;
  • index → the index of the clicked record in the records array;
  • column → the clicked column’s definition;
  • columnIndex → the index of the clicked column in display order (which can differ from its position in your columns array when columns are reordered, pinned or grouped);
  • event → the original React mouse event.

Click on a cell to see it in action:

Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records

Here is the code:

<DataTable
  textSelectionDisabled
  withTableBorder
  columns={[{ accessor: 'name' }, { accessor: 'streetAddress' }, { accessor: 'city' }, { accessor: 'state' }]}
  records={companies}
  onCellClick={({ event, record, index, column, columnIndex }) => {
    openModal({
      title: 'Cell click information',
      children: (
        <Stack>
          <Text size="sm">
            You clicked on row[{index}], column[{columnIndex}] with accessor <Code>{column.accessor}</Code>.
            <br />
            The clicked row refers to company <em>{record.name}</em>.
            <br />
            {event.shiftKey && (
              <>
                You pressed the <Code>Shift</Code> key when clicking.
                <br />
              </>
            )}
            {event.ctrlKey && (
              <>
                You pressed the <Code>Ctrl</Code> key when clicking.
                <br />
              </>
            )}
            {event.altKey && (
              <>
                You pressed the <Code>Alt</Code> key when clicking.
                <br />
              </>
            )}
            {event.metaKey && (
              <>
                You pressed the <Code>Meta</Code> key when clicking.
                <br />
              </>
            )}
          </Text>
          <Center>
            <Button fullWidth onClick={() => closeAllModals()}>
              OK
            </Button>
          </Center>
        </Stack>
      ),
    });
  }}
/>

Double-clicks

The onCellDoubleClick handler receives the same arguments. Double-click on a cell to see it in action:

Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records

Here is the code:

<DataTable
  textSelectionDisabled
  withTableBorder
  columns={[{ accessor: 'name' }, { accessor: 'streetAddress' }, { accessor: 'city' }, { accessor: 'state' }]}
  records={companies}
  onCellDoubleClick={({ event, record, index, column, columnIndex }) => {
    // process double-click event...
  }}
/>

Right-clicks

Similarly, you can use the onCellContextMenu handler to react when the user right-clicks on a cell; call event.preventDefault() if you don’t want the browser’s own context menu to show up. See how you can use it to display a cell context menu with Mantine ContextMenu.

Head over to the next example to discover more features.

Mantine DataTable is used and trusted by