【问题标题】:react-table row selection with custom field instead of index使用自定义字段而不是索引的反应表行选择
【发布时间】:2022-01-03 21:47:20
【问题描述】:

我正在使用react-table 包。我制作了一个带有行选择的简单表格。问题是:行选择结果是一个有索引的对象:

{
 0: true,
 1: true,
 ...
}

但我想成为我的数据的主键,如下所示:

{
  EXoxMjyd4leYABdpuy8m: true,
  2gXlClA38AU8sSM5jnZ7: true,
  ...
}

Code example

在文档中,我找不到可以设置选择键的配置。 问题是,我怎样才能实现第二个例子?

【问题讨论】:

    标签: reactjs react-table react-table-v7


    【解决方案1】:

    你需要覆盖table optiongetRowId

    例子:

    const getRowId = (row, relativeIndex, parent) => {
       // In row object you have access to data. 
       // You can choose parameter. In this example I used uniqueId
       return parent ? [parent.id, row.uniqueId].join('.') : row.uniqueId;
    }
    

    这是一个活生生的例子:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2019-03-30
      • 1970-01-01
      相关资源
      最近更新 更多