0

I'm using dynamic columns in PrimeReact Datatable component:

const columns = [
  {
    field: 'code',
    header: 'Code',
    body: bodyTemplate,
    headerStyle: {{minWidth: '15rem'}},
    sortable: 'sortable',
  },
  ...
]

I defined bodyTemplate as:

const bodyTemplate = (rowData) => {
  return (
    <>
      <span className='p-column-title'>Code</span>
      {rowData.code}
    </>
  )
}

But I don't know how to define headerStyle. Is it possible and how can I do that in dynamic columns?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31

1 Answers1

0

I did it as follow:

change

headerStyle: {{minWidth: '15rem'}}

to

headerStyle: {minWidth: '15rem'}

in columns array as:

const columns = [
  {
    field: 'code',
    header: 'Code',
    body: bodyTemplate,
    headerStyle: {minWidth: '15rem'},
    sortable: 'sortable',
  },
  ...
]

and then in Datatable map columns define headerStyle={col.headerStyle} as:

{columns.map((col) => (
  <Column
    field={col.field}
    header={col.header}
    body={col.body}
    sortable={col.sortable}
    headerStyle={col.headerStyle}
  />
))}
Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31