Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

material-ui - change rows' height and padding in table

I am using the Table component from the react library material-ui.
For some reason, each row, including the header, has a 24px padding, top and bottom, which I can't override.
I already tried changing the style on all the underlying components with no success. Here is the code:

<Table>
    <TableHeader adjustForCheckbox={false} displaySelectAll={false} fixedHeader={true}>
        <TableRow>
            <TableHeaderColumn>id</TableHeaderColumn>
            <TableHeaderColumn>name</TableHeaderColumn>
            <TableHeaderColumn>number</TableHeaderColumn>
        </TableRow>
    </TableHeader>
    <TableBody showRowHover={true} displayRowCheckbox={false}>
        {data.map(item => {
            return (
                <TableRow key={item.id}>
                    <TableRowColumn>{item.id}</TableRowColumn>
                    <TableRowColumn>{item.name}</TableRowColumn>
                    <TableRowColumn>{item.number}</TableRowColumn>
                </TableRow>
            );
        })}
    </TableBody>
</Table>

Any idea how which component's style needs to be changed in order to override this styling?

like image 828
David Tzoor Avatar asked Aug 29 '16 16:08

David Tzoor


People also ask

How do I change the row height in a material UI table?

Set Material-UI Table Row Height with Class Override. I will override the TableRow in the TableHead using class based overrides. There are two steps to this for the Table component: set a height in the TableRow, and remove the vertical padding in the TableCell.

How do you set the width of a TableCell in material UI?

As shown in the previous section, simply use the style or classes prop on the TableCell to set the width, i.e. style={{ width: "25%" }} .

How do you use a TableRow in flutter?

Show activity on this post. Create the list beforehand in the build method. final rows = <TableRow>[]; for (var rowData in myRowDataList) { rows. add(TableRow( ... // Generate a TableRow using rowData )); } ... Table( columnWidths: { 0: FlexColumnWidth(1.0), 1: FlexColumnWidth(2.0), }, border: TableBorder.

What is stack in material UI?

The Stack component manages layout of immediate children along the vertical or horizontal axis with optional spacing and/or dividers between each child.


1 Answers

This kind of requirements can be handled with overrides in Material UI as per below example:

Step 1: include following dependencies

import { ThemeProvider } from '@material-ui/core'
import { createMuiTheme } from '@material-ui/core/styles';

Step 2: Define custom css related changes as

const theme = createMuiTheme({
    overrides: {
        MuiTableCell: {
            root: {  //This can be referred from Material UI API documentation. 
                padding: '4px 8px',
                backgroundColor: "#eaeaea",
            },
        },
    },
});

Step 3: Wrap your component or your code block with

<ThemeProvider theme={theme}>
    <Table>
        <TableRow>
             <TableCell component="td" scope="row">
             </TableCell>
        </TableRow>
    </Table>
</ThemeProvider>

This is how we can override the Material UI style from our custom style. Happy Coding :)

like image 92
Ashok Jangid Avatar answered Oct 26 '22 07:10

Ashok Jangid