Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-table: How change width of the columns?

I'm using the Material Table library that is officially recommended by Google Material UI as a data table library and having troubles with configuring the width of columns.

Column width property is working until our content fits the cell: CodeSandbox Is there any solution to fix that?

like image 443
Karen Avatar asked Jun 23 '20 16:06

Karen


People also ask

How do you increase the width of a column in a material table?

You can also change the column width or row height by selecting a cell in the respective row or column and entering the desired size in the Width or Height fields in the Table Tools > Layout contextual tab.

How do you change the column width in a table?

Change column width To change the width to a specific measurement, click a cell in the column that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Column Width box, and then specify the options you want.

How do I change the width of a column in SAP?

To change the width of the key figure columns, proceed as follows: From the initial or drilldown list, select Settings Column width Key figures . Enter the number of characters you require for the column width in the Column width field and pressENTER .

How do you control column width?

Instead we provide an option to control how wide the columns are. Column width has three settings: Default: Columns will be automatically sized based on the size of the row content and column type. Narrow: Columns will be automatically sized but will be smaller than they otherwise would be.


1 Answers

If you want to set specific width to each column, I believe that you need to specify the option tableLayout: 'fixed' . The docs refers to it like this:

tableLayout | auto or fixed | To make columns width algorithm auto or fixed

So your code could be something like this:

 const tableColumns = [
    { title: "Lorem ipsum", field: "lorem", width: "10%" }, // fixed column width
    { title: "Name", field: "name", width: "80%" },
    { title: "Custom status", field: "customStatus", width: "10%" }]


 <MaterialTable
    tableRef={tableRef}
    columns={tableColumns}
    data={tableData}
    onRowClick={(evt, selectedRow) =>
      setSelectedRow(selectedRow.tableData.id)
    }
    title="Remote Data Example"
    options={{
      rowStyle: rowData => ({
        backgroundColor:
          selectedRow === rowData.tableData.id ? "#EEE" : "#FFF"
      }),
      tableLayout: "fixed"
    }}
  />

Here is the sandbox.

Good luck!

like image 89
NicoE Avatar answered Sep 23 '22 18:09

NicoE