【问题标题】:Adding checkboxes column for each row in table为表格中的每一行添加复选框列
【发布时间】:2022-07-20 16:58:41
【问题描述】:

您好,如何在id 列的左侧为每一行添加一个带有复选框的新列。

export default function Display() {
  const { menus } = JsonData;

  const data = useMemo(
    () => [
      {
        Header: "Id",
        // accessor: "id"
        accessor: (row) => row.id
      },
      {
        Header: "Title",
        accessor: (row) => ({ title: row.title, id: row.id }),        
        Cell: ({ value }) => (
          <Link to={{ pathname: `/menu/${value.id}` }}>{value.title}</Link>
        )
      }
    ],
    []
  );

   return (
    <Table
      data={menus}
      columns={data}
      withCellBorder
      withRowBorder
      withSorting
      withPagination
    />
  );
}

这是我的codeSandbox

【问题讨论】:

    标签: javascript html reactjs react-table


    【解决方案1】:

    只需在数组中添加一个新条目:

    export default function Display() {
      const { menus } = JsonData;
    
      const data = useMemo(
        () => [
          {
            Header: "Ceckbox",
            // accessor: "id"
            accessor: (row) => row.id,
            Cell: ({ value }) => (
              <input type='checkbox' />
            )
          },
          {
            Header: "Id",
            // accessor: "id"
            accessor: (row) => row.id
          },
          {
            Header: "Title",
            accessor: (row) => ({ title: row.title, id: row.id }),        
            Cell: ({ value }) => (
              <Link to={{ pathname: `/menu/${value.id}` }}>{value.title}</Link>
            )
          }
        ],
        []
      );
    
       return (
        <Table
          data={menus}
          columns={data}
          withCellBorder
          withRowBorder
          withSorting
          withPagination
        />
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多