Material-UI Grid Component: Handling Row Click Events in React

I am brand new to React and Material UI. I followed a tutorial on YouTube to create a DataGrid, but now I want to add a feature where clicking on a row in the DataGrid will take me to the detailed page of that specific row.

Thank you in advance!

`

import { Box } from "@mui/material";
import { DataGrid, GridToolbar } from "@mui/x-data-grid";
import { tokens } from "../../theme";
import { mockDatareworks } from "../../data/mockData";
import Header from "../../components/Header";
import { useTheme } from "@mui/material";

const Reworks = () => {
  const theme = useTheme();
  const colors = tokens(theme.palette.mode);

  const columns = [
    { field: "id", headerName: "ID", flex: 0.5 },
    { field: "return_date", headerName: "Date" },
    {
      field: "customer_name",
      headerName: "Customer",
      flex: 1,
      cellClassName: "name-column--cell",
    },
    {
      field: "product_name",
      headerName: 'Product',
      flex: 1,
    },
    {
      field: "rework_quantity",
      headerName: "Quantity",
      type: "number",
      headerAlign: "left",
      align: "left",
    },
  ];

  return (
    <Box m="20px">
      <Header
        title="Rework"
        subtitle="List of reworks for Future Reference"
      />
      <Box
        m="40px 0 0 0"
        height="75vh"
        sx={{
          "& .MuiDataGrid-root": {
            border: "none",
          },
          "& .MuiDataGrid-cell": {
            borderBottom: "none",
          },
          "& .name-column--cell": {
            color: colors.greenAccent[300],
          },
          "& .MuiDataGrid-columnHeaders": {
            backgroundColor: colors.blueAccent[700],
            borderBottom: "none",
          },
          "& .MuiDataGrid-virtualScroller": {
            backgroundColor: colors.primary[400],
          },
          "& .MuiDataGrid-footerContainer": {
            borderTop: "none",
            backgroundColor: colors.blueAccent[700],
          },
          "& .MuiCheckbox-root": {
            color: `${colors.greenAccent[200]} !important`,
          },
          "& .MuiDataGrid-toolbarContainer .MuiButton-text": {
            color: `${colors.grey[100]} !important`,
          },
        }}
      >
        <DataGrid
          rows={mockDatareworks}
          columns={columns}
          components={{ Toolbar: GridToolbar }}
        />
      </Box>
    </Box>
  );
};

export default Reworks;

`

This code shows the current setup of my DataGrid page. How can I implement the functionality to click on a row and navigate to its detail page?

Answer №1

Kindly utilize the onRowClick prop

Visit this link for more information on events related to React Data Grid

        <DataGrid
          rows={mockDatareworks}
          columns={columns}
          components={{ Toolbar: GridToolbar }}
          onRowClick={handleRowClick} // make sure to include it here
        />

Below are the parameters for handleRowClick function

const handleRowClick = (
  params, // GridRowParams
  event, // MuiEvent<React.MouseEvent<HTMLElement>>
  details, // GridCallbackDetails
) => {
  setMessage(params);
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose properties in the Textfield component of material-ui

My Textfield has a bug, how can I ensure that my Maps label is in the center of the dropdown every time and not elevated at the top left corner? Thank you! Check out this screenshot for reference <TextField select ...

Eliminate Specific Days from Material Calendar Display

After struggling to remove selected days from the material calendar and their decorators without success, I eventually found a workaround. Instead of using removeDecorator(), I created an additional XML file to decorate the removed days. I will share my s ...

Implementing a Scalable Application with React Redux, Thunk, and Saga

What sets Redux-Saga apart from Redux-Thunk? Can you explain the primary use of redux saga? What exactly is the objective of redux thunk? ...

Having trouble aligning text and images in React with Material-UI?

I am currently working on designing my portfolio website using reactJs and material-ui. I am facing an issue with aligning the image with text and also a problem with non-responsive text. Essentially, I am trying to achieve text on the left side and image ...

An issue has been encountered in the following module federation: `TypeError: g.useSyncExternalStore is not a function`

I encountered an error after deploying my next app with module federation: TypeError: g.useSyncExternalStore is not a function The same app works fine as a standalone application when run with yarn dev or yarn start, but it fails when using module federat ...

What is the process to create a sticky Material UI grid element?

Currently, I am in the process of developing the front-end for a shopping cart. To organize the content, I have split the container into two columns using Grid container and Grid item. In each column, various components are placed - the left side containin ...

What is the best way to integrate Material UI date picker with a standard CSS element?

https://codesandbox.io/s/busy-glitter-butkvc?file=/src/App.js:0-977 So I have this neat table in my code: <div className="table"> <div className="headers"> <span>column 1</span> <span& ...

The essential guide to creating a top-notch design system with Material UI

Our company is currently focusing on developing our design system as a package that can be easily installed in multiple projects. While the process of building the package is successful, we are facing an issue once it is installed and something is imported ...

Unreachable prevState when utilizing the useState hook

I am currently working on a component where I need to capture the previousState of an element. However, no matter what I try, it keeps returning the initial value. This suggests that there is some re-rendering happening, causing it to constantly default to ...

Issue with card grid layout not displaying correct spacing. See sandbox for example

(adjust the output to trigger the md breakpoint and display cards in a row) Check out this link for reference. I'm attempting to add space between each card, but it doesn't seem to be working as expected. ...

The useSelector from @reduxjs/toolkit in Next.js is returning an undefined value

Utilizing js and @reduxjs/toolkit in my current project has resulted in an issue where the useSelector method is returning undefined values when trying to access data from the store. Below is a snippet of my reducer file: import { createSlice } from "@red ...

Learn how to set default values for UI TextField, Select, and checkbox material fields in React using react-hook-form

Currently, I am facing an issue with assigning default values to a form that utilizes material UI and react-hook-form for managing the form. The problem lies in the fact that select fields do not get assigned values, and even when they do, the labels of Te ...

Utilizing Redux State to Update Local State in Functional Components

I am currently working on a contact manager application. My goal is to have the ContactForm fields populated with the name and number of the "hotContact" when the user clicks the update button for a specific contact. Even though the hotContact object is su ...

What is the best way to display changing data in a React component?

Having some trouble with printing data based on the length of an array. Here is my attempted solution, but unfortunately, it's not working as expected. I believe the issue lies in the cb function within the forEach loop. Below is the code snippet: fun ...

React Native: Issue with componentWillUpdate not being triggered

In my current project, I am developing a React Native app that utilizes both React Navigation and Redux. class LoginScreen extends Component { state = { email: '', password: '', errors: { email: '', ...

Issue with Material UI select component not displaying the label text

I've been struggling with Material UI's "Select" for quite some time now - spent about 10 hours trying to make it work the way I want. Any help would be greatly appreciated. This question is connected to a previous one: Select MenuItem doesn' ...

how can I apply a Linear Gradient color to the borderColor within expo react native?

Learn how to apply a linear gradient color on borders I am looking for guidance on using a linear gradient in stylesheet for border color. borderColor:['color1',color2] https://i.stack.imgur.com/oBCeu.png ...

What could be causing the TypeError I encounter when trying to import @wordpress/element?

Encountering a similar issue as discussed in this related question. This time, I've switched to using '@wordpress/element' instead of 'react-dom/client' based on the recommendation that it also leverages React functionalities. Ho ...

"Encountering an Invalid hook call error with React-Leaflet v4 and Next.js 13

I am facing an issue following my update of Next.js from version 12 to 13, which also involved updating React from 17 to 18 and react-leaflet from 3 to 4. Within this component: ` function ChangeView({ center }) { const map = useMap(); map.setView( ...

Sharing libraries among different web components can be achieved by following these steps

Embarking on a micro frontends project using custom elements has sparked the need to find a way to share dependencies across all parts of the application. I am particularly interested in integrating the Material-ui library into this structure. One idea is ...