Resetting the column order in Mui Data Grid: A step-by-step guide

I need assistance with resetting column ordering in my React application:

import * as React from 'react';
import Box from '@mui/material/Box';
import { DataGridPro } from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function DataGridProDemo() {
    const { data } = useDemoData({
        dataSet: 'Commodity',
        rowLength: 100000,
        editable: true,
    });

    const resetOrdering = () => {
        // Looking for a way to reset the column ordering 
    }

    return (
        <Box sx={{ height: 520, width: '100%' }}>
            <button onClick={resetOrdering} > Reset </button>
            <DataGridPro
                {...data}
                loading={data.rows.length === 0}
                rowHeight={38}
                checkboxSelection
                disableSelectionOnClick
                experimentalFeatures={{ newEditingApi: true }}
            />
        </Box>
    );
}

I have attempted using separate setters provided by the Data Grid Pro API, but it did not yield the desired result.

Answer №1

Give this a try:

function DataGridPro() {
    const [sortingModel, setSortingModel] = useState<GridSortModel>([]);
    ...
    const clearOrdering = () => {
        setSortingModel([])
    }

    return (
        ...
        <DataGridPro
            ...
            sortingModel={sortingModel}
            onSortingModelChange={(newSortingModel) => setSortingModel(newSortingModel)}
        />
        ...
    );
}

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

Acquiring the selected value when the dropdown is modified in a React application

Upon selecting the year 2022, I faced an issue where the logged value did not appear in the console. Strangely, when choosing 2021 instead, '2022' was the value that got printed to the console. To remedy this, I attempted setting the initial stat ...

Struggling to Incorporate Components into my Higher Order Component (HOC)

Here is the HOC code inside SectionWrapper.jsx: import React from 'react' import { motion } from 'framer-motion' import { styles } from '../styles' import { staggerContainer } from '../utils/motion' const SectionWrap ...

What are the steps to rectify the issue of displaying data accurately on

I am working on my ReactJS project and using devextreme to create a unique circular gauge. However, I'm facing an issue where certain values are not being displayed correctly. For instance, instead of showing the value 5011480286.78, it is displaying ...

What is the best way to display JSON data in a readable format?

I received a JSON file with the following structure: { "data":{ "uuid":"123", "name":"TestData", "alias":null, "created_at":"2021-03-17T11:57:29.000000Z&q ...

Can you explain the main distinction between Material UI Popover and Popper? Are there specific situations where one is more suitable than

Material UI has been praised for its near-perfect documentation, especially for React developers. However, there are some aspects that remain unclear, such as the precise distinction between Popover and Popper. Can someone provide a brief explanation of ...

An alternative MUI option for AntD notifications

I've been utilizing antD's notification API for a while now, but in my latest project, I've switched to MUI. To streamline error handling for API requests, I'm using a shared Axios instance that includes onSuccess and onError methods. I ...

What is the best way to create an optional object parameter in Typescript?

I'm working on a function that looks like this: const func = (arg1: string, { objArg = true }:{ objArg: string }) => { // some code } Is it possible to make the second parameter (an object) optional? ...

The ternary operator is malfunctioning when it comes to the condition

I've encountered an issue while trying to integrate a custom MUI button into my project. My goal is to have the button enabled only when 2 specific objects are not empty, otherwise it should remain disabled. Despite my efforts, the code I've writ ...

Caution: The React Hook useEffect is missing a required dependency

What is the best way to eliminate the warning "React Hook useEffect has a missing dependency" while developing my code? Here is a snippet of the code that triggers the warning: useEffect(() => { if(inactive){ document.querySelect ...

The custom react hook is throwing an error stating that "Window" is not

I have encountered a challenging issue that I can't seem to resolve on my own. The dilemma arises from using a custom hook to determine the window's height and width, leading to an error as Next.js operates with SSR (Server-Side Rendering) and d ...

Encountering an Uncaught TypeError due to failure to read the property 'bool' of an undefined value

Encountering a runtime error in my code that I can't seem to pinpoint. Everything was functioning correctly before this error surfaced after updating npm. Here's the error message: Uncaught TypeError: Cannot read property 'bool' of und ...

What are some methods for utilizing the data received through this.props.history?

Whenever I need to navigate from one route to another using this.props.history.push("/"), I also want to pass along some extra data. For example: this.props.history.push('/postDetail', {data: item}). However, I am uncertain about where to define ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

The upcoming developer manages to execute the program successfully, however, it continues to load indefinitely

Executing the command yarn dev consistently runs successfully in my VS Code terminal: $ yarn dev yarn run v1.22.19 warning ..\..\..\..\package.json: No license field $ next dev ready - started server on 0.0.0.0:3000, url: http://localho ...

Determining if the state in React has been altered

State containing client information is stored here const { info, setInfo, onPostInfoChanges } = useContext(ClientInfos); Below is a function that sends new info data to the server using POST or PUT requests const onSubmitHandler = async (model) => { ...

Comparing Fetch and Axios: Which is Better?

Currently delving into the realms of axios and the fetch API, I am experimenting with sending requests using both methods. Here is an example of a POST request using the fetch API: let response = await fetch('https://online.yoco.com/v1/charges/&ap ...

Develop a custom autocomplete feature using Formik in React for selecting values from an array of objects

Looking to Add Autocomplete Functionality in Your React Application Using Formik for Seamless Selection of Single and Multiple Values from an Array of Objects? Take a Look at this Code snippet! [see image below] (https://i.stack.imgur.com/ekkAi.png) arra ...

Exploring the wonders of Next.js and its ability to incorporate

I am currently working on a Next.js (13.3.0) project and facing an issue with global styles that include animations. Here is the structure of my folders: https://i.stack.imgur.com/nM5xw.png All SCSS files are loaded through the master.scss file: @import & ...

Is there a way to convert time measurements like minutes, hours, or days into seconds in React and then pass that information to an

Currently, I am working on an application that allows users to select a frequency unit (like seconds, minutes, hours, or days) and input the corresponding value. The challenge arises when I need to convert this value into seconds before sending it to the ...

Whenever a reload occurs, I encounter the React error of an invalid hook call

Initially, the app functions correctly but after a page reload or any action that triggers a page reload, an error occurs. Upon examining the error stack, it appears that the error originates from the Redux Provider. I have meticulously searched for pote ...