Examples › Basic usage

In its most basic usage scenario, the DataTable component only requires the records and columns properties to be set:

import { DataTable } from 'mantine-datatable';
import companies from '~/data/companies.json';

export function BasicUsageExample() {
  return (
    <DataTable
      columns={[{ accessor: 'name' }, { accessor: 'streetAddress' }, { accessor: 'city' }, { accessor: 'state' }]}
      records={companies}
    />
  );
}

The code above will produce the following result:

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

A few things worth noting:

  • each column’s accessor tells the component which record property to display in that column;
  • when a column doesn’t have a title, one is generated by “humanizing” its accessor (i.e. streetAddress becomes Street address);
  • each record must have a unique id property, which is used as the row key; if your records are identified by a different property, have a look at the non-standard record IDs example.

Displaying nested properties

Accessors support dot-notation, so you can display nested object properties without writing a custom render function:

return (
  <DataTable
    columns={[
      { accessor: 'firstName' },
      { accessor: 'lastName' },
      // 👇 the generated title will be “Department name”
      { accessor: 'department.name' },
      // 👇 the generated title will be “Department company name”
      { accessor: 'department.company.name' },
    ]}
    records={records}
  />
);
First name
Last name
Department name
Department company name
JeraldHowellIndustrialRunte Inc
KathleenRueckerComputersShanahan, Robel and Beier
EricaVolkmanToysGoyette Inc
CliffordOberbrunnerAutomotiveRau - O'Hara
AlisonKlingJeweleryGoyette Inc
No records

However, there’s much more you can do with Mantine DataTable. Head over to the next example to learn about its basic properties.

Mantine DataTable is used and trusted by