Search code examples
javascripthtmlreactjsreact-table

Adding checkboxes column for each row in table


Hello how can I add a new column with checkboxes for each row on the left side of my id column.

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
    />
  );
}

Here is my codeSandbox


Solution

  • You can add new column to your data :

    const data = useMemo(
        () => [
          {
            id: "check",
            accessor: (row) => row.title,
            Cell: ({ value }) => <input type="checkbox" value={value} />
          },
          {
            Header: "Id",
            // accessor: "id"
            accessor: (row) => row.id
          },
          {
            Header: "Title",
            // accessor: "title"
            accessor: (row) => ({ title: row.title, id: row.id }),
            // accessor: (row) => (
            //   <Link to={{ pathname: `/menu/${row.id}` }}>{row.title}</Link>
            // )
            // Link the content of my cell. NOT WORKING
            //Cell: ({ row }) => (
            //<Link to={{ pathname: `/menu/${row.id}` }}>{row.title}</Link>
            //)
            Cell: ({ value }) => (
              <Link to={{ pathname: `/menu/${value.id}` }}>{value.title}</Link>
            )
          }
        ],
        []
      );
    

    Edit filters (forked)