material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[Table] Set table column width

Open celiao opened this issue 11 years ago • 55 comments

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

celiao avatar Oct 19 '15 03:10 celiao

Nothing quite special about this :

<TableCell style={{ width: '2rem' }}>

kamek-pf avatar Nov 06 '15 23:11 kamek-pf

<TableRowColumn style={{ width: 100 }}>

doesn't behave too well with TableHeaderColumn

max-ch9i avatar Nov 11 '15 17:11 max-ch9i

Is there any way to dynamically assign the width of col based on content ?

zikangyao avatar Mar 03 '16 22:03 zikangyao

In my vanilla HTML table, the columns width are automatically set depending on content. material-ui seems to enforce equal width on all columns. Is there a way to set width of column to be dynamic instead, like vanilla HTML tables?

sanfilippopablo avatar Jun 03 '16 05:06 sanfilippopablo

@sanfilippopablo, <Table> uses the table-layout: 'fixed' css prop, leading to all columns having the same width, combined with white-space: 'nowrap' on every table cell to handle the overflow.

You can't safely switch to table-layout: 'auto' since the table then grows infinitely with its content unless you remove the above-mentioned white-space css prop. And if you do this, you can't use the fixedHeader={true} prop anymore since headers are then contained in a separate table :cry:.

Some feedback would be appreciated.

qettnqs avatar Jul 08 '16 08:07 qettnqs

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..

theseancronin avatar Jul 19 '16 10:07 theseancronin

You can't safely switch to table-layout: 'auto' since the table then grows infinitely with its content

What about adjusting table header width when the tableRowColumn width changes with content? I can't seem to set it up so those 2 are aligned.

HeWasntOffByMuch avatar Sep 06 '16 16:09 HeWasntOffByMuch

I guess you either use fixedHeader={true} and can't set a custom width or you don't and have to keep your headers inside your <TableBody>.

qettnqs avatar Sep 06 '16 16:09 qettnqs

Okay, putting header in tbody works! Now I need to figure out how to do multi-line header columns. Any tips?

HeWasntOffByMuch avatar Sep 06 '16 20:09 HeWasntOffByMuch

Are you trying to achieve something like this? (if not, you can probably find the right example in this website)

I think you can do pretty much everything you want with the attributes colSpan on TableHeaderColumn and TableRowColumn and rowSpan on TableRow.

qettnqs avatar Sep 07 '16 07:09 qettnqs

I don't understand - why is this closed?

NZhuravlev avatar Dec 07 '16 17:12 NZhuravlev

Putting TableHeader in TableBody doesn't work for me.

henrycity avatar Dec 12 '16 12:12 henrycity

@tvtri96 What we suggested was putting tableRow acting as tableheader in tableBody, but it looks to me like it is now working fine.

this is how it looks now in my code:

<Table fixedHeader={true}>
          <TableHeader>
            <TableHeaderComponent schema={schema}/>
          </TableHeader>
          <TableBody displayRowCheckbox={false} showRowHover={true}>
            {this.props.data.map((item, index) => (
              <TableRowComponent key={index} schema={this.props.schema}
                item={item} onRemoveClick={this.handleRemoveItem}
                onEditClick={this.handleEditItem}
              />
            ))}
          </TableBody>
</Table>

and header columns are aligned with body columns correctly.

HeWasntOffByMuch avatar Dec 12 '16 12:12 HeWasntOffByMuch

Tks for answering me. What is inside TableHeaderComponent? I guess it is not like a normal TableHeader right?

<TableHeader fixedHeader={true} >
     <TableRow>
          <TableHeaderColumn>Timestamp</TableHeaderColumn>
     </TableRow>
 </TableHeader>

henrycity avatar Dec 12 '16 12:12 henrycity

Yes, its just wrapped for reasons.

HeWasntOffByMuch avatar Dec 12 '16 13:12 HeWasntOffByMuch

Any news on this? How can I let the columns auto-adjust to content?

damianobarbati avatar Feb 10 '17 15:02 damianobarbati

This works for me, auto-sizing:

      <Table style={{tableLayout: 'auto'}}>

arjan avatar Apr 25 '17 11:04 arjan

@arjan

 <Table style={{tableLayout: 'auto'}}>

This has no effect on header.

musray avatar Jul 30 '17 05:07 musray

<Table fixedHeader={false} style={{ tableLayout: 'auto' }}> works (dynamic sizing based on content).

sonaye avatar Oct 09 '17 16:10 sonaye

I needed to add width: "auto", which results in:

<Table fixedHeader={false} style={{ width: "auto", tableLayout: "auto" }}>

devuxer avatar Dec 03 '17 23:12 devuxer