Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 10012  / 4 Years ago, tue, march 3, 2020, 12:00:00

I have a table using react-table with checkboxes in rows.
I also added a check all checkbox in the 1st cell of a row, to check/uncheck all the checkboxes in every column of that row.



The problem is when I implemented the check all checkbox, it worked as intended but when I check/uncheck a checkbox individually it doesn't check/uncheck.



Table Component



import React from 'react';
import ReactTable from 'react-table';
import setTableColumns from '../utils/setTableColumns';

const RolePermissionsTable = () => {
return (
<ReactTable
defaultPageSize={10}
data={capacity.roles}
columns={setTableColumns()}
showPagination={false}
getTrProps={(state, rowData, column, instance) => {
return {
onClick(event) {
const row = event.currentTarget;
const targetEntity = row.querySelector(
'.RoleTarget [type=checkbox]'
);
const allCheckboxes = Array.prototype.slice.call(
row.querySelectorAll('[type=checkbox]')
);
const permissionsCheckboxes = allCheckboxes.slice(
1,
allCheckboxes.length
);
const isChecked = targetEntity.checked;

permissionsCheckboxes.map(permissionsCheckbox => {
permissionsCheckbox.checked = isChecked ? isChecked : null;
});
},
};
}}
/>
);
};


Column Headers



import React from 'react';

// imported above for the table
const setTableColumns = () => {
return [
{
Header: 'Entities',
accessor: 'target',
Cell: rowData => (
<span className=RoleTarget key={rowData.original.target}>
<input
id={`CheckAll-${rowData.original.target}`}
type=checkbox
/>
<span className=RoleTarget__name>{rowData.original.target}</span>
</span>
),
},
{
Header: 'Add',
accessor: 'add',
Cell: rowData => {
return (
<input
type=checkbox
defaultChecked={rowData.original.permissions.add}
id={`${rowData.original.target}-${rowData.original.userId}`}
/>
);
},
},
{
Header: 'Modify',
accessor: 'modify',
Cell: rowData => {
return (
<input
type=checkbox
defaultChecked={rowData.original.permissions.modify}
id={`${rowData.original.target}-${rowData.original.userId}`}
/>
);
},
},
{
Header: 'Delete',
accessor: 'delete',
Cell: rowData => {
return (
<input
type=checkbox
defaultChecked={rowData.original.permissions.delete}
/>
);
},
},
];
};


I'm not sure if I'm doing this correctly or is there a better way for this?



Many thanks


More From » html

 Answers
12

I don't want to say if there is a better way, if your solution solves your problem it's already good. I do think there is a simpler way.
There is a simple package that solves this problem grouped-checkboxes.



View on codesandbox



In your case it will look like this:



import React from 'react';
import ReactTable, { ReactTableDefaults } from 'react-table';
import setTableColumns from '../utils/setTableColumns';
import { CheckboxGroup } from '@createnl/grouped-checkboxes';

const TrComponent = ({ children, ...rest }) => (
<tr {...rest}>
<CheckboxGroup>{children}</CheckboxGroup>
</tr>
);

const RolePermissionsTable = () => {
Object.assign(ReactTableDefaults, {
TrComponent: TrComponent
});

return (
<ReactTable
defaultPageSize={10}
data={capacity.roles}
columns={setTableColumns()}
showPagination={false}
/>
);


Column Headers



import React from 'react';
import { AllCheckerCheckbox, Checkbox } from '@createnl/grouped-checkboxes';

// imported above for the table
const setTableColumns = () => {
return [
{
Header: 'Entities',
accessor: 'target',
Cell: rowData => (
<span className=RoleTarget key={rowData.original.target}>
<AllCheckerCheckbox
id={`CheckAll-${rowData.original.target}`}
/>
<span className=RoleTarget__name>{rowData.original.target}</span>
</span>
),
},
{
Header: 'Add',
accessor: 'add',
Cell: rowData => {
return (
<Checkbox
checked={rowData.original.permissions.add}
id={`${rowData.original.target}-${rowData.original.userId}`}
/>
);
},
},
{
Header: 'Modify',
accessor: 'modify',
Cell: rowData => {
return (
<Checkbox
checked={rowData.original.permissions.modify}
id={`${rowData.original.target}-${rowData.original.userId}`}
/>
);
},
},
{
Header: 'Delete',
accessor: 'delete',
Cell: rowData => {
return (
<Checkbox
checked={rowData.original.permissions.delete}
/>
);
},
},
];
};

[#4581] Friday, February 28, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
pranavrorys questions
Fri, May 27, 22, 00:00, 2 Years ago
Thu, Oct 28, 21, 00:00, 3 Years ago
Sat, May 30, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 5 Years ago
Fri, Oct 11, 19, 00:00, 5 Years ago
;