mui-datatables icon indicating copy to clipboard operation
mui-datatables copied to clipboard

Column CustomHeadRender reuse the sortToggle function

Open diegochavez opened this issue 6 years ago • 8 comments

Column CustomHeadRender rehuse the sort function

Right now I want to create a custom head and use the sort function provided by the table.

Current Behavior

right now is not possible,

Proposal

I noticed the sort function is passed on the customHeadRender function so I made a few changes on the MUIDataTableHead component

line 55

column.customHeadRender({ index, ...column }, (columnIndex) => this.handleToggleColumn(columnIndex))

This give us the oportunity of continue using the sortToggle using the current index or other column index. :) I changed the name to columnIndex because the name index is shared inside of the map function on columns array.

    const columns = ["Name", "Title", "Location", "Age", {
        name: "Salary", 
        sortDirection: 'asc',
        options: {
          customHeadRender: ({index, ...column}, sortColumn) => {
            return (
              <TableCell onClick={() => sortColumn(index)} key={index}>
                {column.name}
              </TableCell>
            );
          }
        }
    }];

I can create the PR to include this small functionality.

diegochavez avatar Jan 20 '19 20:01 diegochavez

Any updates on this?

sriram4493 avatar Mar 08 '19 06:03 sriram4493

Thanks @diegochavez . But if we use this as it is then we lost the sort icon which is provided by default on datatable. We can customize it as follow

const columnHeadRender = ({index, name,...column}, sortColumn) => (
  <th style={columnStyle} onClick={() => sortColumn(index)} key={index}>
    {name}
    <TableSortLabel active={column.sortDirection !== null} direction={column.sortDirection || "asc"}/>
  </th>
)

We can get this TableSortLabel component from import TableSortLabel from '@material-ui/core/TableSortLabel';

zeeshansafdar48 avatar Jun 27 '19 06:06 zeeshansafdar48

If someone wants to open a PR for this behavior, I will take a look.

gabrielliwerant avatar Jun 27 '19 06:06 gabrielliwerant

I would find this useful as well; for a tooltip I can use hint but I'd like to just override the basic Sort tooltip and skip the icon without re-implementing the rest of the header.

pcrane-zag avatar Jul 30 '19 17:07 pcrane-zag

A quick heads up for this thread: a recent update in mui-datatables changed the default value of an unsorted sortDirection to "none" (as opposed to null).

Anyone who used the above example to build their own custom TH renderer, if upgrading to versions higher than 2.10.3 (when I noticed it, but may be in an earlier build as well) should update the code accordingly:

<TableSortLabel active={column.sortDirection !== "none"} ... />

dandrei avatar Sep 11 '19 15:09 dandrei

If you continue to use null, it should still work, but you will get a console deprecation error warning you to change the behavior.

gabrielliwerant avatar Sep 11 '19 20:09 gabrielliwerant

hi guys, how about the arrow icon? his direction doesn't change if i use ss sorting from param 'column' i have this only : issuecustomheadsort where the property sortDirection?

cyc1e avatar Nov 18 '21 09:11 cyc1e

to change the styling of table header, use customHeadLabelRender instead : customHeadLabelRender: (columnMeta) => ( <span className="tableheader" > {columnMeta.name} </span> ),

RafaelaPrendi avatar Jul 26 '22 09:07 RafaelaPrendi