Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

anyone knows how to add resizable columns to my react data grid?

I am using DataGrid from material UI React. In the documentation the columns are resizable but it is not for me. This is the component displayed

return (
    <Box m="1.5rem 2.5rem">
      <Typography variant="h4">Reservation List</Typography>
      <Box style={{ height: 400, width: "100%" }}>
        <DataGrid
          rows={rows}
          columns={columns}
          pageSize={5}
          onRowClick={handleRowClick}
          rowSelected={selectedRow !== null}
          disableSelectionOnClick
          selectionModel={selectedRow !== null ? [selectedRow] : []}
          disableColumnResize={false}
        />
      </Box>
    </Box>
  )
  }

below are my rows and columns asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf

     const columns = [
        { field: "id", headerName: "#", width: 90 },
        { field: "reservationId", headerName: "Reservation ID", width: 150 },
        { field: "customerId", headerName: "Customer ID", width: 150 },
        { field: "numAdults", headerName: "Adults", width: 120 },
        { field: "numChildren", headerName: "Children", width: 120 },
        { field: "daysOfStay", headerName: "Days of Stay", width: 150 },
        { field: "roomType", headerName: "Room Type", width: 150 },
        { field: "arrivalDate", headerName: "Arrival Date", width: 150 },
        { field: "leadTime", headerName: "Lead Time", width: 120 },
        { field: "cancelled", headerName: "Cancelled", width: 120 },
        { field: "bookingChannel", headerName: "Booking Channel", width: 180 },
      ];
    
      const rows = [
        {
          id: 1,
          reservationId: "R001",
          customerId: "C001",
          numAdults: 2,
          numChildren: 1,
          daysOfStay: 4,
          roomType: "Deluxe",
          arrivalDate: "2023-05-01",
          leadTime: 14,
          cancelled: false,
          bookingChannel: "Expedia",
        },

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To resize the columns, DataGridPro can be used.

To prevent the resizing of a column, set resizable: false in the GridColDef. Alternatively, to disable all columns resize, set the prop disableColumnResize={true}.

References:
https://mui.com/x/react-data-grid/column-dimensions/, https://codesandbox.io/s/gjh0ln?file=/demo.tsx

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading