How to make mui table inside card in react js?
March 05, 2024Hi Friends π,
Welcome To aGuideHub!
To make mui table inside card in React js, you can use TableContainer
in Card
component. It will make mui table inside card in react js
Today, I am going to show you, how to make mui table inside card 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 card 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 card 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 { Card, CardContent, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } 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>Data 1</TableCell>
<TableCell>Data 2</TableCell>
<TableCell>Data 3</TableCell>
<TableCell>Data 4</TableCell>
</TableRow>
</TableBody>
MUI material make mui table inside card 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 TableContainer
in Card
component to make mui table inside card in react js.
App.js
import React from 'react';
import { Card, CardContent, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } from '@mui/material';
const TableInsideCard = () => {
return (
<Card>
<CardContent>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Column 1</TableCell>
<TableCell>Column 2</TableCell>
<TableCell>Column 3</TableCell>
<TableCell>Column 4</TableCell>
{/* Add more TableCells for additional columns */}
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>Data 1</TableCell>
<TableCell>Data 2</TableCell>
<TableCell>Data 3</TableCell>
<TableCell>Data 4</TableCell>
{/* Add more TableCell with data for additional columns */}
</TableRow>
{/* Add more TableRows for additional rows */}
</TableBody>
</Table>
</TableContainer>
</CardContent>
</Card>
);
};
export default TableInsideCard;
In the above code example, I have used the @mui/material
component and made mui table inside card in react js.
Check the output of the above code example.
All the best π