Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Navigation and Data Display

Tables, Pagination and Data Grid

Show data with Material UI Table, TableSortLabel and TablePagination, then learn when to move to the MUI X Data Grid for sorting, filtering and virtualization.

Tables are where most business apps spend their time. Material UI gives you two levels. The core Table family is a set of styled HTML table parts: you control the data, sorting and paging yourself. The MUI X Data Grid is a separate package with all of that built in, plus editing, filtering, column resizing and virtualization for very large data sets.

This lesson shows the core table with sorting and pagination, explains what the logic looks like, and then shows when to reach for the Data Grid.

MUI is not available inside the live editors here. Real MUI code is read-only; the runnable examples are stand-ins with the same props and logic, so you can run the sorting and paging code yourself.

The Table family

A table is made of small components that map to HTML elements:

MUI HTML Notes
TableContainer div Adds horizontal scrolling when the table is wider than the screen
Table table size="small" for dense rows; stickyHeader for a fixed header
TableHead / TableBody / TableFooter thead / tbody / tfoot
TableRow tr hover, selected
TableCell td or th align="right" for numbers; renders th in a head
JSX
// Real Material UI, read-only
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';

<TableContainer component={Paper} sx={{ maxHeight: 440 }}>
  <Table stickyHeader size="small" aria-label="Orders">
    <TableHead>
      <TableRow>
        <TableCell>Customer</TableCell>
        <TableCell align="right">Total</TableCell>
        <TableCell>Status</TableCell>
      </TableRow>
    </TableHead>
    <TableBody>
      {rows.map((row) => (
        <TableRow key={row.id} hover>
          <TableCell component="th" scope="row">{row.customer}</TableCell>
          <TableCell align="right">{row.total.toFixed(2)}</TableCell>
          <TableCell>{row.status}</TableCell>
        </TableRow>
      ))}
    </TableBody>
  </Table>
</TableContainer>

Right-align numeric columns so digits line up, make the first cell of a row a row header (component="th" scope="row") so assistive tech can name the row, and always provide a key that is stable (an id, not an index, when rows can reorder).

Sorting

TableSortLabel is a button that shows an arrow. Your code does the sort. Keep order ('asc' or 'desc') and orderBy in state, and sort a copy of the rows (never rows.sort() directly, which mutates props or state).

JSX
// Real Material UI, read-only
<TableCell sortDirection={orderBy === 'total' ? order : false}>
  <TableSortLabel
    active={orderBy === 'total'}
    direction={orderBy === 'total' ? order : 'asc'}
    onClick={() => requestSort('total')}
  >
    Total
  </TableSortLabel>
</TableCell>

sortDirection on the cell sets aria-sort, which screen readers announce.

Pagination

TablePagination shows the rows-per-page selector and the page buttons. It is controlled too: you pass page, rowsPerPage, count (the total number of rows) and the two handlers. You slice the data yourself.

JSX
// Real Material UI, read-only
<TablePagination
  component="div"
  count={rows.length}
  page={page}
  onPageChange={(event, newPage) => setPage(newPage)}
  rowsPerPage={rowsPerPage}
  onRowsPerPageChange={(event) => { setRowsPerPage(parseInt(event.target.value, 10)); setPage(0); }}
  rowsPerPageOptions={[5, 10, 25]}
/>

page is zero-based. After changing the page size, reset page to 0, or you may land beyond the last page. For server-side data, count is the total on the server and you fetch only the page you need.

Try it: sorting and pagination logic

This stand-in uses the same state shape as the real components (order, orderBy, page, rowsPerPage). Click a header to sort; change the page size and watch the page reset.

A sortable, paginated table

Selection and row actions

Add a checkbox column to select rows. Keep the selected ids in a Set (or array of ids), not row objects, because the data can reload. A header checkbox uses indeterminate when some, but not all, rows are selected:

JSX
// Real Material UI, read-only
<Checkbox
  indeterminate={selected.size > 0 && selected.size < rows.length}
  checked={rows.length > 0 && selected.size === rows.length}
  onChange={(e) => setSelected(e.target.checked ? new Set(rows.map((r) => r.id)) : new Set())}
  slotProps={{ input: { 'aria-label': 'select all rows' } }}
/>

Try it: select all with an indeterminate state

Row selection with a tri-state header checkbox

When to use the Data Grid

The core table is the right choice for small, simple, read-mostly tables. As soon as you need more, the work grows quickly. @mui/x-data-grid (MIT licensed for the community edition; Pro and Premium are paid) provides:

  • sorting, filtering and column visibility out of the box,
  • pagination and a built-in server-side mode,
  • row virtualization, so 100,000 rows render only what is visible,
  • cell editing, column resizing and reordering (resizing and pinning are Pro),
  • accessible keyboard navigation (it implements the ARIA grid pattern).
JSX
// Real MUI X, read-only
import { DataGrid } from '@mui/x-data-grid';

const columns = [
  { field: 'id', headerName: 'ID', width: 70 },
  { field: 'customer', headerName: 'Customer', flex: 1 },
  { field: 'total', headerName: 'Total', type: 'number', width: 110,
    valueFormatter: (value) => `$${value.toFixed(2)}` },
  { field: 'status', headerName: 'Status', width: 130 },
];

<div style={{ height: 420 }}>
  <DataGrid
    rows={rows}
    columns={columns}
    initialState={{ pagination: { paginationModel: { pageSize: 10 } } }}
    pageSizeOptions={[10, 25, 50]}
    checkboxSelection
    disableRowSelectionOnClick
  />
</div>

The grid needs a parent with a height (or autoHeight): without it nothing is visible. Each row needs a unique id field, or a getRowId function.

Need Core Table Data Grid
20 rows, read-only Best Overkill
Sort and page small data Fine (you write the logic) Built in
10,000+ rows Poor Virtualized
Inline editing Manual Built in
Full control of markup Yes Less

For a complete project that combines a table with filters, see Project: Data Table with Filters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Is TablePagination's page prop zero-based or one-based?

    The first page is 0. Remember to reset it to 0 when rowsPerPage changes.
  2. 2. Why sort a copy of the rows ([...rows].sort(...))?

    sort changes the array in place. Never mutate React state; sort a copy.
  3. 3. Which cell attribute lets screen readers announce the sort direction?

    TableCell sortDirection sets aria-sort to ascending or descending on the column header.
  4. 4. A table must show 100,000 rows smoothly. What is the best choice?

    Virtualization renders only the visible rows. A plain table would create 100,000 DOM rows.

Summary

  • The core Table parts map to HTML table elements; right-align numbers and use th scope="row" for row headers.
  • Sorting and paging are your own state: order, orderBy, page, rowsPerPage; sort a copy and reset the page when the page size changes.
  • Track selection by id and use indeterminate for the header checkbox.
  • Use the MUI X Data Grid for large data, editing and built-in filtering; give it a height and unique row ids.

Next: Dialogs, Snackbars and Feedback