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?
Advertisement
Answer
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:
JavaScript
x
23
23
1
const tableColumns = [
2
{ title: "Lorem ipsum", field: "lorem", width: "10%" }, // fixed column width
3
{ title: "Name", field: "name", width: "80%" },
4
{ title: "Custom status", field: "customStatus", width: "10%" }]
5
6
7
<MaterialTable
8
tableRef={tableRef}
9
columns={tableColumns}
10
data={tableData}
11
onRowClick={(evt, selectedRow) =>
12
setSelectedRow(selectedRow.tableData.id)
13
}
14
title="Remote Data Example"
15
options={{
16
rowStyle: rowData => ({
17
backgroundColor:
18
selectedRow === rowData.tableData.id ? "#EEE" : "#FFF"
19
}),
20
tableLayout: "fixed"
21
}}
22
/>
23
Here is the sandbox.
Good luck!