MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.
In this article, we are going to discuss the React MUI TableHead API. Table Heads are used to define the header of the Table component of MUI. Tables are used to display a collection of data in an organized way. Table heads are used to define the column name or category. The API provides a lot of functionality and we will learn to implement them.
Import TableHead API
import TableHead from '@mui/material/TableHead'; // or import { TableHead } from '@mui/material';
Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.
- children (node): It is a component similar to the table row.
- classes (Object): Override the existing styles or add new styles to the component.
- component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
- sx (Array<func / object / bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles
Syntax: Create a TableHead component inside the table as follows:
<Table aria-label="simple table"> <TableHead> <TableRow> <TableCell>Sl. No.</TableCell> <TableCell align="right">Name</TableCell> <TableCell align="center">Age</TableCell> </TableRow> </TableHead> </Table>
Installing and Creating React app, and adding the MUI dependencies.
Step 1: Create a react project using the following command.
npx create-react-app gfg_tutorial
Step 2: Get into the project directory
cd gfg_tutorial
Step 3: Install the MUI dependencies as follows:
npm install @mui/material @emotion/react @emotion/styled @mui/lab
Step 4: Run the project as follows:
npm start
Example 1: In the following example, we have a table with a table head having three columns.
App.js
import "./App.css" ; import * as React from "react" ; 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" ; function createData(index = 0, tutorial = "" , link = "" ) { return { index, tutorial, link }; } const rows = [ createData( 1, "Data Structures" , ), createData( 2, "Algorithms" , ), createData( 3, "Competitive Programming" , ), ]; function App() { return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> <strong>React MUI TableHead API</strong> </div> <TableContainer component={Paper}> <Table sx={{ minWidth: 650 }} aria-label= "simple table" > <TableHead> <TableRow> <TableCell>Sl. No.</TableCell> <TableCell>Tutorial</TableCell> <TableCell>Link</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row) => ( <TableRow key={row.name}> <TableCell component= "th" scope= "row" > {row.index} </TableCell> <TableCell>{row.tutorial}</TableCell> <TableCell> <a href={row.link} target= "_blank" > {row.link} </a> </TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer> </div> ); } export default App; |
Output:
Example 2: In the following example, we have added the sx prop field to customize the borders and background of the tablehead.
App.js
import "./App.css" ; import * as React from "react" ; 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" ; function createData(index = 0, tutorial = "" , link = "" ) { return { index, tutorial, link }; } const rows = [ createData( 1, "Data Structures" , ), createData( 2, "Algorithms" , ), createData( 3, "Competitive Programming" , ), ]; function App() { return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> <strong>React MUI TableHead API</strong> </div> <TableContainer component={Paper}> <Table sx={{ minWidth: 650 }} aria-label= "simple table" > <TableHead sx={{ backgroundColor: "lightgreen" , borderColor: "black" , borderWidth: "2px" , borderStyle: "solid" , padding: "4px" , }} > <TableRow> <TableCell>Sl. No.</TableCell> <TableCell>Tutorial</TableCell> <TableCell>Link</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row) => ( <TableRow key={row.name}> <TableCell component= "th" scope= "row" > {row.index} </TableCell> <TableCell>{row.tutorial}</TableCell> <TableCell> <a href={row.link} target= "_blank" > {row.link} </a> </TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer> </div> ); } export default App; |
Output:
Reference: https://mui.com/material-ui/api/table-head/