Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make material data grid width to fill the parent component in react js

I'm trying to display the data grid with all columns should take equal width to fit the parent component. but there is a blank space at the end which I'm not able to remove and cannot make columns to take auto width to fit page width.

can anyone please help me on this

https://codesandbox.io/s/musing-montalcini-tn04y

enter image description here

like image 698
Vikas Acharya Avatar asked Jun 14 '21 12:06

Vikas Acharya


People also ask

How do you change size of grid in react?

React Data Grid: Column Sizing. All columns can be resized by dragging the top right portion of the column.

What is data grid in react?

The Ignite UI for React Data Table / Data Grid is a tabular React grid component that allows you to quickly bind and display your data with little coding or configuration. Features of the React data grid include filtering, sorting, templates, row selection, row grouping, row pinning and movable columns.


Video Answer


2 Answers

Include flex value for each column level like below,

const columns = [
  { field: "id", headerName: "ID", flex: 1 },
  { field: "firstName", headerName: "First name", flex: 1 },
  { field: "lastName", headerName: "Last name", flex: 1 },
  {
    field: "age",
    headerName: "Age",
    type: "number",
    flex: 1
  },
  {
    field: "fullName",
    headerName: "Full name",
    description: "This column has a value getter and is not sortable.",
    sortable: false,
    flex: 1,
    valueGetter: (params) =>
      `${params.getValue(params.id, "firstName") || ""} ${
        params.getValue(params.id, "lastName") || ""
      }`
  }
];

codesandbox - https://codesandbox.io/s/dry-https-7pp93?file=/src/App.js:266-850

like image 89
Sarun UK Avatar answered Oct 21 '22 20:10

Sarun UK


in my case, I include flex as well as minWidth property which helps the table show full data in SM (small) and (XS) extra small devices.

const columns = [
    {field: "id", hide: true},
    {field: "transaction", headerName: "Transactions", minWidth: 330, flex: 1},
    {field: "date", headerName: "Date", minWidth: 100, flex: 1},
    {field: "type", headerName: "Type", minWidth: 100, flex: 1},
    {field: "price", headerName: "Price", minWidth: 110, flex: 1},
    {field: "gas", headerName: "Gas", minWidth: 110, flex: 1},
    {field: "total", headerName: "Total", minWidth: 110, flex: 1} ]
like image 38
Junaid Sikander Hakro Avatar answered Oct 21 '22 19:10

Junaid Sikander Hakro