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?
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.
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.
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 .
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With