如何获取反应表中单击单元格的行索引?

tom*_*123 1 javascript reactjs react-table

你好,我正在使用 React Table。这是我的演示沙箱 - https://codesandbox.io/embed/vq60okkz20?codemirror=1

我想获取单击的单元格 div 的行索引。我有两列,其中的单元格有一个可点击的 div。我想获取这些单元格的行索引

Mur*_*ati 5

直接来自 React-table 文档:

// When any Td element is clicked, we'll log out some information
<ReactTable
  getTdProps={(state, rowInfo, column, instance) => {
    return {
      onClick: (e, handleOriginal) => {
        console.log("A Td Element was clicked!");
        console.log("It was in this row:", rowInfo);

        // IMPORTANT! React-Table uses onClick internally to trigger
        // events like expanding SubComponents and pivots.
        // By default a custom 'onClick' handler will override this functionality.
        // If you want to fire the original onClick handler, call the
        // 'handleOriginal' function.
        if (handleOriginal) {
          handleOriginal();
        }
      }
    };
  }}
/>  
Run Code Online (Sandbox Code Playgroud)

rowInfo物体将具有以下形状:

  row: Object, // the materialized row of data
  original: , // the original row of data
  index: '', // the index of the row in the original array
  viewIndex: '', // the index of the row relative to the current view
  level: '', // the nesting level of this row
  nestingPath: '', // the nesting path of this row
  aggregated: '', // true if this row's values were aggregated
  groupedByPivot: '', // true if this row was produced by a pivot
  subRows: '', // any sub rows defined by the `subRowKey` prop
Run Code Online (Sandbox Code Playgroud)

通过访问rowInfo.index您将获得单元格的行索引。
工作示例:https://codesandbox.io/s/nr8w9q6z2m