How to make mui table with multiple sticky columns in react js?
March 13, 2024Hi Friends π,
Welcome To aGuideHub!
To make mui table with multiple sticky columns in React js, you can use style={{ position: 'sticky', left: 0, zIndex: 1, background: '#fff' }}
. It will make mui table with multiple sticky columns in react js.
Today, I am going to show you, how to make mui table with multiple sticky columns 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 with multiple sticky columns 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 with multiple sticky columns 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, 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>
{[...Array(50)].map((_, index) => (
<TableRow key={index}>
<TableCell style={{ position: 'sticky', left: 0, zIndex: 1, background: '#fff' }}>Row {index + 1}</TableCell>
<TableCell style={{ position: 'sticky', left: '100px', zIndex: 1, background: '#fff' }}>Data 1</TableCell>
<TableCell>Data 2</TableCell>
<TableCell>Data 3</TableCell>
<TableCell>Data 4</TableCell>
<TableCell>Data 5</TableCell>
<TableCell>Data 6</TableCell>
{/* Add more TableCells for additional columns */}
</TableRow>
))}
</TableBody>
MUI material make mui table with multiple sticky columns 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 style={{ position: 'sticky', left: 0, zIndex: 1, background: '#fff' }}
to make mui table with multiple sticky columns in react js.
App.js
import React from 'react';
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } from '@mui/material';
const MultiStickyColumnsTable = () => {
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell style={{ position: 'sticky', left: 0, zIndex: 1, background: '#fff' }}>Sticky Column 1</TableCell>
<TableCell style={{ position: 'sticky', left: '100px', zIndex: 1, background: '#fff' }}>Sticky Column 2</TableCell>
<TableCell>Column 3</TableCell>
<TableCell>Column 4</TableCell>
<TableCell>Column 5</TableCell>
<TableCell>Column 6</TableCell>
<TableCell>Column 7</TableCell>
{/* Add more TableCells for additional columns */}
</TableRow>
</TableHead>
<TableBody>
{[...Array(50)].map((_, index) => (
<TableRow key={index}>
<TableCell style={{ position: 'sticky', left: 0, zIndex: 1, background: '#fff' }}>Row {index + 1}</TableCell>
<TableCell style={{ position: 'sticky', left: '100px', zIndex: 1, background: '#fff' }}>Data 1</TableCell>
<TableCell>Data 2</TableCell>
<TableCell>Data 3</TableCell>
<TableCell>Data 4</TableCell>
<TableCell>Data 5</TableCell>
<TableCell>Data 6</TableCell>
{/* Add more TableCells for additional columns */}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
};
export default MultiStickyColumnsTable;
In the above code example, I have used the @mui/material
component and made mui table with multiple sticky columns in react js.
Check the output of the above code example.
All the best π