New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Table] Set table column width #1911
Comments
Nothing quite special about this : <TableCell style={{ width: '2rem' }}> |
This comment has been minimized.
This comment has been minimized.
Is there any way to dynamically assign the width of col based on content ? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
What I've been doing to set the widths of columns is for the style, set table-layout to auto, as mentioned above, then I give widths in percentages to my columns. i.e. width 30% and width 70%. This would be a two-column layout example, with table header columns in first first column rather than in the header.. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is good to go in v1 or above const styles = {
narrowCell: {
'width': '150px',
}
};
<Table>
<TableHead>
<TableRow>
<TableCell>Company Name</TableCell>
<TableCell style={classes.narrowCell} numeric>Amount</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell> name 1 </TableCell>
<TableCell style={classes.narrowCell}> $100 </TableCell>
</TableRow>
</TableBody>
</Table> Can also give percentage widths, rest of the cells span equally |
None of the solutions posted above work. Why is this so difficult to achieve? |
<Table fixedHeader={false} style={{tableLayout: "auto" }}>
<colgroup>
<col width="250px" />
<col />
<col width="40px" />
<col width="40px" />
</colgroup>
<TableHead>
<TableRow> the second collumn is flex |
Somehow i recognize why it's not work, because we only set
As you can see, we can use both value for style by integer and string. But for string, it's only work if you defined it by Why do i use both of Note : Both of I hope this code help you guys, cheers |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@devenovil Instead of <TableCell style={{ width: '35%' }}>
Table Data
</TableCell>
<TableCell>
Another Table Data
</TableCell> |
For me it worked to set the width only for the cells in the header: |
Posted my solution to setting the column width here: mui/material-ui#1911 (comment)
I set the column widths with the percentage and it sort of works, but for some reason, it adds a phantom column on the far right. Via inspecting the element, it appears to add a "TableStubCell" element to the table. I have no idea where that came from. I've had similar issues with other MUI tables, and my work-arounds are never that great. Would be cool if y'all opened up this issue and fixed it...clearly other folks are struggling with it too. edit: the TableStubCell came from the dx-react-grid-material-ui npm package, so sorry for pointing fingers! |
My solution is to do <Table>
<TableHeader>
<TableRow>
<TableCell>
<Box width="200px">
User Fullname
</Box>
</TableCell>
{/* ...rest of the columns */}
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>
April Mintac Pineda
</TableCell>
{/* ...rest of the columns */}
</TableRow>
{/* ...rest of the rows */}
</TableBody>
</Table> But would be nice if we could just do <Table>
<TableHeader>
<TableRow>
<TableCell width="200px">
User Fullname
</TableCell>
{/* ...rest of the columns */}
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>
April Mintac Pineda
</TableCell>
{/* ...rest of the columns */}
</TableRow>
{/* ...rest of the rows */}
</TableBody>
</Table> |
This works for me with the version <TableContainer className={classes.container}>
<Table className={classes.table} stickyHeader size="small">
<TableHead>
<TableRow>
<TableCell width="30%">User Name</TableCell>
<TableCell width="20%">Designation</TableCell>
<TableCell width="10%">Nid</TableCell>
<TableCell width="20%">Email</TableCell>
<TableCell width="10%">Mobile</TableCell>
<TableCell width="10%">Gender</TableCell>
</TableRow>
</TableHead>
<TableBody>
{props.isGridLoading && (
<TableRow>
<TableCell colSpan={6}>
<LoadingGridSpinner open={props.isGridLoading} />
</TableCell>
</TableRow>
)}
{props.profileObj.lists &&
props.profileObj.lists.map(row => (
<TableRow key={row.userProfileId} hover={true}>
<TableCell width="30%">
{row.userName}
</TableCell>
<TableCell width="20%">{row.designation}</TableCell>
<TableCell width="10%">{row.nid}</TableCell>
<TableCell width="20%">{row.email}</TableCell>
<TableCell width="10%">{row.mobile}</TableCell>
<TableCell width="10%">{row.gender}</TableCell>
</TableRow>
))}
</TableBody>
</Table> |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Does anyone know a way to let the table scroll in x direction if there are too many columns? I'm only able to make this happen by giving minWidth=something hardcoded (which is not great for unknown number of columns) It seems like Any ideas? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@oliviertassinari I did not convert the existing code to native This example works by setting width of all columns |
This comment has been minimized.
This comment has been minimized.
You can try wrapping the whole table in a container element with |
thanks this works for material v5 too. |
Is it possible to set the width of Table columns?
StackOverflow equivalent question: https://stackoverflow.com/questions/51216285/material-ui-v1-set-table-column-widths
The text was updated successfully, but these errors were encountered: