Matelutex
Matelutex

Reputation: 2220

React Material - MUIDataTable - how to add content in column header

I use in my React app:

import MUIDataTable from "mui-datatables";

I'd like to add some button to my last column header (instead of the column name):

 <MUIDataTable
            data={data}
            columns={columns}
            options={options}
        >
        </MUIDataTable>

where columns:

export const columns = [
    {
        name: "name",
        label: "Nazwa",
        options: {
            filter: true,
            sort: true,
        }
    },
    {
        name: "productNumber",
        label: "Numer",
        options: {
            filter: true,
            sort: true,
        }
    }, (...)

How to do that? Is it possible? I can't find anything

Upvotes: 0

Views: 2683

Answers (2)

Khabir
Khabir

Reputation: 5842

You need to use customHeadRender

const columns = [
    {
        name: "id",
        label: "Id",
        options: {
            filter: false,
        }
    },
    {
        name: "subject",
        label: "Subject",
        options: {
            filter: true,
            sort: false,
        }
    },
    {
        name: "button",
        options: {
            customHeadRender: ({index, ...column}) => {
                return (
                    <Button key={index}>
                        Click
                    </Button>
                )
            }
        }
    }
];

Upvotes: 0

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 4748

You can define a custom body for column. You can add a column like this:

{
  name: "Age",
  options: {
     filter: false,
     customBodyRender: (value, tableMeta, updateValue) => (
        <FormControlLabel
          control={<TextField value={value || ''} type='number' />}
          onChange={event => updateValue(event.target.value)}
        />
     )
  }
}

Upvotes: 1

Related Questions