0

I use material-table, react, and material-ui.

I've really struggled to get material-table column widths set properly. I've googled a number of stack and git posts (e.g. one example, another example), all of which have made various suggestions.. none have worked for me.

Currently I am creating my table as follows (notice all the attempts to set width, max-width, min-width. I've tried many combinations of these).

I've also tried many combinations of the options parameters in an attempt to customize the table in a fashion consistent with the two questions at the bottom of this post.

return (
        <div className={classes.root} >
            <MaterialTable
                data={data}
                title={title}
                icons={tableIcons}
                onSelectionChange={(rows) => setSelectedRows(rows)}
                options={{
                    headerStyle: {
                        backgroundColor: "#01579b",
                        color: "#FFF"
                    },
                    tableLayout: "fixed",
                    rowStyle: {
                        overflowWrap: 'break-word'
                    },
                    selection: true,
                }}
                columns={[
                    {
                        title: "Id",
                        field: "id",
                        type: "numeric",
                        headerStyle: {
                            backgroundColor: "purple",
                            width: "5%"
                        },
                        cellStyle: {width: "5%", maxWidth: "5%", overflow: "hidden"},
                        width: "5%"
                    },
                    {
                        title: "Label",
                        field: "label",
                        headerStyle: {
                            backgroundColor: "blue",
                            width: "75%"
                        },
                        cellStyle: {width: "75%", maxWidth: "75%", overflow: "hidden"},
                        width: "75%"
                    },
                    {
                        title: "Time",
                        field: "time",
                        type: "string",
                        headerStyle: {
                            backgroundColor: "red",
                            width: "20%"
                        },
                        cellStyle: {width: "20%", maxWidth: "20%", overflow: "hidden"},
                        width: "20%"
                    }
                ]}
                actions={[
                    {
                        icon: () => <SaveAlt />,
                        tooltip: "Save User",
                        onClick: (event, rowData) => alert("You saved " + rowData.name)
                    }
                ]}
                components={{
                    Action: (props) => (
                        <Button
                            onClick={(event) => props.action.onClick(event, props.data)}
                            color="primary"
                            variant="text"
                            style={{ textTransform: "none" }}
                            size="small"
                        >
                            Save
                        </Button>
                    )
                }}

            />
        </div>
    );

where

const useStyles = makeStyles((theme) => ({
    root: {
        maxWidth: "600px",
        paddingTop: "2px"
    },
    margin: {
        margin: theme.spacing(1),
    },
    extendedIcon: {
        marginRight: theme.spacing(1),
    },
}));

My table looks like the following:

enter image description here

The following shows the CSS for the "td" element. enter image description here

Oddly the width is set to 200px, and nothing I seem to do modifies this. Even when I change the value in the dev tools to something like 40px, this does not change the actual column width.

I am able to change the header width (in developer tools) for that column and then things look ok, but nothing I do in the code has allowed me to change the header width in this same fashion.

enter image description here

If I do the following for the ID column, one would think that it would set the width for the header, but it doesnt. It still shows up as 200px.

{
                        title: "Id",
                        field: "id",
                        type: "numeric",
                        headerStyle: {
                            backgroundColor: "red",
                            width: "80px"
                        },
                    },

enter image description here

Per the suggestion made in one of the comments, I've also tried this code.. to no avail:

export default function MyCustomTable(props) {
    const classes = useStyles();
    const tableRef = React.createRef();

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

    const tableData = [
        {
            lorem: "lorem",
            name: "name",
            customStatus: "customStatus"
        }
    ];

    return (
        <div className={classes.root} >
        <MaterialTable
            tableRef={tableRef}
            columns={tableColumns}
            data={tableData}
            title="Change Column Width Example"
            options={{
                tableLayout: "fixed"
            }}
        />
        </div>
    );
}

enter image description here

I've also tried to strip out all of my application except for the materialtable.. same styling problem.

enter image description here

Two questions:

  1. How do I make the ID column narrower?
  2. How do I configure the date so that it does not wrap onto a second row?
David Sargrad
  • 121
  • 1
  • 9
  • did you check out this thread? https://stackoverflow.com/questions/62539462/material-table-how-change-width-of-the-columns – urirot Sep 16 '21 at 16:05
  • @urirot I did.. it was one of the many that gave me clues.. but I dont see anything in that thread that I havent already tried. – David Sargrad Sep 16 '21 at 16:35
  • In fact I've tried pretty much exactly what you see here: https://codesandbox.io/s/material-table-change-width-of-column-kty8g?file=/src/App.js In fact I was able to pull exactly that implementation into my component, and I see the same problem: – David Sargrad Sep 16 '21 at 16:52
  • @urirot I added the attempt at the code from the codesandbox in the link you referenced.. I also added the image that shows the exact same problem. Even though it works in codesandbox, it does not work in my app – David Sargrad Sep 16 '21 at 17:02
  • in your code, did you remove all of your custom styling when you tried using the codesandbox example? if not, try having only what the example show and then gradually adding your styles, to see what is overriding the width percentage – urirot Sep 16 '21 at 17:12
  • @urirot Yes.. I've eliminated all the styling that is different. In fact I also have used the developer tools to compare and contrast every style element. They are the same. – David Sargrad Sep 16 '21 at 17:13
  • @urirot In fact ive taken that suggestion to the extreme.. I've stripped down the entire app.. and I am only rendering the sandbox code.. same exact problem. the columns are not sizing properly. – David Sargrad Sep 16 '21 at 17:29
  • Interesting. did you compare the package.json as well? – urirot Sep 16 '21 at 17:31
  • These are his dependencies: "dependencies": { "@material-ui/core": "4.10.2", "@material-ui/icons": "4.9.1", "material-table": "1.60.0", "react": "16.12.0", "react-dom": "16.12.0", "react-scripts": "3.0.1" }, – David Sargrad Sep 16 '21 at 17:36
  • And mine are all greater than those versions: "@material-ui/core": "^4.12.3", "@material-ui/icons": "^4.11.2", "material-table": "^1.69.3", "react": "^17.0.2", "react-dom": "^17.0.2", "react-scripts": "4.0.3", – David Sargrad Sep 16 '21 at 17:36
  • I'm out of ideas. I see you opened an issue in their github repo. hope that works out. I don't know what your context is, but I would try using just the material-ui tables or some other package – urirot Sep 16 '21 at 17:45
  • @urirot I appreciate all your help. The fact that I stripped it down to a barebones app and still see the problem.. was motivated by your help.. Ty much – David Sargrad Sep 16 '21 at 17:47

0 Answers0