How to make mui table inside table in react js?
March 05, 2024Hi Friends π,
Welcome To aGuideHub!
To make mui table inside table in React js, you can use Table
. It will make mui table inside table in react js.
Today, I am going to show you, how to make mui table inside table in react js.
Table of contents
- Install Material-UI
- Import the required Material-UI components.
- Define the table body.
Letβs start with the first step.
Step 1: Install Material-UI
Install the following packages to use make mui table inside table in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Step 2: Import the required Material-UI components.
To make mui table inside table in React, first, you have to import the table. We have imported the table
, tableBody
, tableCell
, tableContainer
, tableHead
, tableRow
, and Paper
components from the @mui/material
library.
import React from 'react';
import { Table, TableBody, TableCell, TableHead, TableRow } from '@mui/material';
Step 3: Define the table body.
Table body is needed after table head, This code creates a table body with one row for each object in the rows array.
<TableBody>
<TableRow>
<TableCell>Nested Data 1</TableCell>
<TableCell>Nested Data 2</TableCell>
<TableCell>Nested Data 3</TableCell>
<TableCell>Nested Data 4</TableCell>
</TableRow>
</TableBody>
MUI material make mui table inside table example.
The below code is an example of a Material UI table. You have to import @mui material
table. In the Material Table function when you create a table component, it consists of a table head and a table body. In this code, we create our TableContainer component with a Paper component as its child and use Table
to make mui table inside table in react js.
App.js
import React from 'react';
import { Table, TableBody, TableCell, TableHead, TableRow } from '@mui/material';
const NestedTableExample = () => {
return (
<Table>
<TableHead>
<TableRow>
<TableCell>Column 1</TableCell>
<TableCell>Column 2</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>Data 1</TableCell>
<TableCell>Data 2</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Table>
<TableHead>
<TableRow>
<TableCell>Nested Column 1</TableCell>
<TableCell>Nested Column 2</TableCell>
<TableCell>Nested Column 3</TableCell>
<TableCell>Nested Column 4</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>Nested Data 1</TableCell>
<TableCell>Nested Data 2</TableCell>
<TableCell>Nested Data 3</TableCell>
<TableCell>Nested Data 4</TableCell>
</TableRow>
</TableBody>
</Table>
</TableCell>
</TableRow>
</TableBody>
</Table>
);
};
export default NestedTableExample;
In the above code example, I have used the @mui/material
component and made mui table inside table in react js.
Check the output of the above code example.
All the best π