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

[table] TableHeaderColumn onClick not firing

Open johnnygames opened this issue 11 years ago • 15 comments

I'm working on implementing sorting functionality and cannot for the life of me get onClick to fire for <TableHeaderColumn>. I see that there is an onClick prop in the docs, but no luck.

<Table
        fixedHeader={true}
        >
        <TableHeader adjustForCheckbox={false} displaySelectAll={false}>
          <TableRow>
            <TableHeaderColumn style={{width: '7%'}}></TableHeaderColumn>
            <TableHeaderColumn style={{width: '12%'}} tooltip="Click to sort">Name</TableHeaderColumn>
            <TableHeaderColumn onClick={(row, col) => console.log(col)} style={{width: '17%'}} tooltip="Click to sort">Email</TableHeaderColumn>
            <TableHeaderColumn style={{width: '7%'}} tooltip="Click to sort">Role</TableHeaderColumn>
            <TableHeaderColumn style={{width: '10%'}} tooltip="Click to sort">Status</TableHeaderColumn>
            <TableHeaderColumn style={{width: '2%'}}></TableHeaderColumn>
          </TableRow>
        </TableHeader>
        <TableBody selectable={true} showRowHover={true} displayRowCheckbox={false} style={{overflow: 'visible'}}>
          {rows}
        </TableBody>
      </Table>

This was just for testing purposes, but the third <TableHeaderColumn> does nothing when clicked. It also doesn't fire when passed a this.handleClick or other callback. onCellClick being passed to <Table> works when clicked on anything other than the header cells. This is using React 0.13.2 and Material-UI 0.11.0.

johnnygames avatar Oct 29 '15 23:10 johnnygames

Same here, I'm not able to use onClick. As OP said, it doesn't work for <TableHeaderColumn>, but I can't get it to work with <TableRowColumn> either. (and yes, I know about then onCellClick event :p)

It's a bit strange that onClicks never get triggered. Maybe we're no supposed to use them at all with Material UI ? Or are we missing something ?

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

@kamek-pf Are you using the latest verion of material-ui? Have a look at https://github.com/callemall/material-ui/blob/master/src/table/table-header-column.jsx#L126. It should be working.

oliviertassinari avatar Nov 07 '15 00:11 oliviertassinari

Turns out, that the TableRow component is overiding the onClick property (https://github.com/callemall/material-ui/blob/master/src/table/table-row.jsx#L145). You have to use onCellClick. Let me know if this solve you issue.

oliviertassinari avatar Nov 07 '15 00:11 oliviertassinari

Yup, it fixed my issue, thanks @oliviertassinari.

kamek-pf avatar Nov 11 '15 21:11 kamek-pf

@kamek-pf I'm wondering if this is in the doc. Feel free to submit a PR to enhance if it's not the case.

oliviertassinari avatar Nov 11 '15 21:11 oliviertassinari

I just checked, it's not in the doc. I'll send a PR tonight.

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

Done, see https://github.com/callemall/material-ui/pull/2138.

kamek-pf avatar Nov 12 '15 01:11 kamek-pf

The format of the docs has changed considerably, and onCellClick is now documented (automatically from comments in the source code).

I'll close this issue, but if you feel anything isn't clear in the new docs, feel free to reopen this issue.

oliviertassinari avatar Feb 01 '16 08:02 oliviertassinari

This issue is exactly the same as #1783 and I've submitted PR #3492 as an attempt to fix both

ccomb avatar Feb 26 '16 21:02 ccomb

Hello, any update on how to get onClick to work for TableHeaderColumns?

Sundin avatar May 03 '16 14:05 Sundin

It's ongoing in #3812 but I've currently no time to improve QA on the PR, help appreciated until I can work again on it

ccomb avatar May 03 '16 14:05 ccomb

I'm also interested in this, I'm not getting click events to work on <TableHeaderColumn />, the onCellClick callback works for columns in the table body but I'm not getting any click events triggered for the header columns.

Ideally, I'd like to sort the table by clicking the TableHeaderColumns, I'm on the latest version of material-ui 0.15.0. Any word on when this update might get in?

sh3r4rd avatar May 26 '16 17:05 sh3r4rd

@skbailey, I had the same problem when I was trying sort my table. I found only one solution for this case — onCellClick for the header column must be set on the TableRow.

<TableHeader>
    <TableRow onCellClick={(event) => (console.log(event.target))}>
        <TableHeaderColumn>Col 1</TableHeaderColumn>
        <TableHeaderColumn>Col 2</TableHeaderColumn>
        <TableHeaderColumn>Col 3</TableHeaderColumn>
    </TableRow>
</TableHeader>

I tryed this solution on Material UI 0.16.1. It worked. :)

vyushin avatar Nov 24 '16 11:11 vyushin

@vyushin you saved my day!

zhuhan0 avatar Jun 08 '17 22:06 zhuhan0

I am not using Material UI but I have the same problem that on-click is not working on my table.

tsegaywelu avatar Feb 15 '25 13:02 tsegaywelu