【问题标题】:How to make just 1 row editable in material-table?如何使材料表中只有 1 行可编辑?
【发布时间】:2023-03-04 08:57:01
【问题描述】:

目前我有一张使用material-table 的员工表。有一些带有某些标志的员工使背景行变红。这是它的样子:

我想要做的是能够编辑具有红色背景的行并且这些行。我不想将editable 道具放在整个表格上,因为这会占用每一行的图标空间,但我只想能够编辑这些特殊的行标志。

这是我的 React 代码。请注意,forgotClockOut 是我拥有的特殊标志。

<MaterialTable
    icons={icons}
    title="Daily Report"
    columns={[
        { title: 'Name', field: 'name' },
        {
            title: 'Time In',
            field: 'started',
            render: ({ started }) =>
                started
                    ? moment(started).format('h:mm A')
                    : 'Not yet',
            cellStyle: (value, { started, clockedOut }) => {
                if (!started) {
                    return null;
                }

                if (clockedOut) {
                    return { color: 'red' };
                }

                return { color: '#06c92d' };
            },
        },
        { title: 'Time Out', field: 'clockedOut', render: ({clockedOut}) => clockedOut ? moment(clockedOut).format('h:mm A') : 'Not yet'},
        { title: 'Time Worked', field: 'minutesWorked', render: ({minutesWorked}) => `${Math.floor(minutesWorked / 60)}h ${minutesWorked % 60}m`},
    ]}
    options={{
        rowStyle: ({forgotClockOut}) => {
            if(forgotClockOut) {
                return { backgroundColor: '#ffaaaa' };
            }
        }
    }}
    onRowClick={(event, rowData) => {
        const {forgotClockOut} = rowData;
        // ?? What to do here ??
    }}
    data={employees}
/>

有没有办法只编辑使用material-table 制作的表格中的某些行?

【问题讨论】:

  • 你能分享一下你的代码如何显示employees吗?
  • @bertdida 当然!我已将其添加到问题中。

标签: javascript reactjs material-ui material-table


【解决方案1】:

您可以使用 edit 属性来定义:

<MaterialTable
    editable={{
        isEditable: rowData => rowData.name === 'a', // only name(a) rows would be editable
}}/>

您可以在文档中看到:https://material-table.com/#/docs/features/editable

【讨论】:

  • 我收到错误警告:未知事件处理程序属性onBulkEditRowChanged。它将被忽略。有什么办法可以解决吗?
【解决方案2】:

即使使用 prop isEditable 和 isDeletable,您也无法隐藏这 2 个图标。 如果您想完全隐藏它们,请尝试使用Conditionals Actions

如果您需要快速解决此问题,您可以创建一个在隐藏道具条件下出现的动作。

const actions={
  [
    rowData => ({
      icon: 'delete',
      isFreeAction: false,
      tooltip: 'Delete User',
      hidden: !rowData.deletable
      onClick:() => {...}
      })
  ]} 

【讨论】:

  • 感谢您的回答!这是一个不错的解决方案,但是我需要知道单击按钮时如何触发行编辑状态。
  • 要使表格行可编辑,您应该设置具有 onRowAdd、onRowUpdate、onRowDelete 函数的可编辑属性来操作数据。 material-table.com/#/docs/features/editable
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 2022-08-24
  • 1970-01-01
  • 2019-12-12
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多