首页 > 解决方案 > 有没有办法添加一个使用 Material Table React 到每个列标题

问题描述

嗨,我正在尝试向<label></label>表格的每个单元格添加一个。我在这里使用材料表:https ://material-table.com/#/docs/features/component-overriding

我试过这样:

<Table
  onChangePage={handleChangePage}
  page={props.currentPage}
  totalCount={props.totalCount}
  options={{
    paging:true,
    pageSizeOptions:[10, 15, 25, 50],
    pageSize: props.rowsPerPage,
    padding: "dense",
    search: false,
    toolbar:false,
    headerStyle: TableHeaderRow, <--- cssproperties
    rowStyle:TableRow <--- cssproperties
  }}
  columns={columns} <--- variable columns:TableColumn[]
  data={dataAct} <---- data generated from API
/>

实际上,我定义了headerStylein 选项,但我真正想要的是添加一个

<label>
  headerValueHere}
</label>

对于每个标题。

我在单元格中添加了标签标记,例如在定义列中使用渲染:

const getDocumentTypeForRow = rowData => {
  return (
    <Tooltip title={rowData}>
      <label style={OpenVulnerabilityDuePatchingCardDetailsElementLabel}>
        {rowData}
      </label>
    </Tooltip>
  )
};

const columns: TableColumn<>[] = [
  {
    title: 'Due Date',
    field: 'remediation_due_date',
    render: data => getDocumentTypeForRow(data.remediation_due_date)
  },
]

但是如何为标题执行此操作???

标签: javascriptreactjsmaterial-uimaterial-table

解决方案


您可以将 JSX 或 HTML 传递给标题

const columns: TableColumn<>[] = [
  {
    title: <label>Due Date</label>,
    field: 'remediation_due_date',
    render: data => getDocumentTypeForRow(data.remediation_due_date)
  },
]

您可以在此处进行实时编辑并查看结果

https://material-table.com/#/docs/features/styling

columns={[
        {
          title: <h1>Heading 1</h1>, field: 'name',
          cellStyle: {
            backgroundColor: '#039be5',
            color: '#FFF'
          },
          headerStyle: {
            backgroundColor: '#039be5',
          }
        },
        { title: 'Surname', field: 'surname' },
        { title: 'Birth Year', field: 'birthYear', type: 'numeric' },
        {
          title: 'Birth Place',
          field: 'birthCity',
          lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
        },
      ]}

推荐阅读