如何获取react-table中单击的单元格的行索引?

时间:2018-12-27 13:52:56

标签: javascript reactjs react-table

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

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

3 个答案:

答案 0 :(得分:2)

直接来自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();
        }
      }
    };
  }}
/>  

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

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

答案 1 :(得分:1)

如果您正在执行功能渲染,这可能会有所帮助

<tbody {...getTableBodyProps()}>
    rows.slice(0, 10).map((row, i) => {
      prepareRow(row);
      return (
        <tr {...row.getRowProps()}>
          {row.cells.map((cell, i) => {
            return (
              <td {...row.getRowProps({onClick: (e)=>handleTableCellClick(e,row)})}>{cell.render("Cell")}</td>
            );
          })}
        </tr>
      );
    })
</tbody>

答案 2 :(得分:0)

我会建议使用onClick处理程序来获取值。您可以广告一个唯一的ID,以获取确切的元素。

这是一个非常简单的示例

        buttonStateLeft = digitalRead(left);

  if(buttonStateLeft == HIGH)
  {
    clockShown = false;
    menuShown = true;
    lcd.clear();
  }

id可以随机生成