Friday, November 29, 2024
Google search engine
HomeLanguagesReact MUI TableFooter API

React MUI TableFooter API

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 TableFooter API. Table Footer is used to displaying different information such as the number of columns selected or the column names. Tables are used to display a collection of data in an organized way. The API provides a lot of functionality and we will learn to implement them.

Import TableFooter API:

import TableFooter from '@mui/material/TableFooter';
// or
import { TableFooter } 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 TableFooter component inside the table as follows:

<Table sx={{ minWidth: 650 }}>
  <TableFooter>
    <TableCell>Total Number of Rows is 3</TableCell>
  </TableFooter>
</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 footer showing the number of 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";
import { TableFooter } from "@mui/material";
  
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 TableFooter API</strong>
      </div>
  
      <TableContainer component={Paper}>
        <Table sx={{ minWidth: 650 }}>
          <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>
          <TableFooter>
            <TableCell>Total Number of Rows is 3</TableCell>
          </TableFooter>
        </Table>
      </TableContainer>
    </div>
  );
}
  
export default App;


Step to run the application: Open the terminal and type the following command.

npm start

Output:

 

Example 2: In this example, we have the rows selectable and the selected index is shown in the footer.

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";
import { TableFooter } from "@mui/material";
import { useState } from "react";
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() {
  const [selectedRow, setSelectedRow] = useState();
  
  return (
    <div className="App">
      <div
        className="head"
        style={{
          width: "fit-content",
          margin: "auto",
        }}
      >
        <h1
          style={{
            color: "green",
          }}
        >
          neveropen
        </h1>
        <strong>React MUI TableFooter API</strong>
      </div>
  
      <TableContainer component={Paper}>
        <Table sx={{ minWidth: 650 }}>
          <TableHead>
            <TableRow>
              <TableCell>Sl. No.</TableCell>
              <TableCell>Tutorial</TableCell>
              <TableCell>Link</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {rows.map((row, index) => (
              <TableRow
                sx={
                  index == selectedRow ? { backgroundColor: "lightcoral" } : {}
                }
                onClick={() => {
                  setSelectedRow(index);
                }}
                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>
          <TableFooter>
            <TableCell>Total Number of Rows is 3</TableCell>
            <TableCell>Number of Selected Rows is {selectedRow}</TableCell>
          </TableFooter>
        </Table>
      </TableContainer>
    </div>
  );
}
  
export default App;


Output:

 

Reference: https://mui.com/material-ui/api/table-footer/

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments