0

Example Table multiple columns of checkboxes

Imagine that I have a table with multiple columns. Some columns are made up entirely of checkboxes. How would you manage the state for that table?

When I click a checkbox, I imagine that I would want to dispatch an action to update the redux state with some unique identifier for that column, and then the index of the row. For instance:

column1: [0,1,5,10] column2: [2,3,6]

If I click column2, row 1, column2 would update to:

column2: [0,2,3,6]

Where column# is the key for that column, and each number in the array is the row.

2
  • 1
    welcome to stack overflow! do you have a specific question? this is kind of vague. Commented Oct 27, 2017 at 15:45
  • And it's not related to react or redux... it's just a question of how to organize data Commented Aug 24, 2018 at 7:21

2 Answers 2

1

Rows are usually used to represent instances of objects. You have turned this on it's head by using columns to represent your instances. Even if your table is static, I would not try to represent the columns, but instead represent the row:

var data = [
  [false, false, false, true],
  [false, false, true, true]
];

This translates much more cleanly for use with React:

{data.map(row => {
  return (
    <tr>
     <td><input type="checkbox" checked="{row[0] ? 'checked' : ''}"/></td>
     <td><input type="checkbox" checked="{row[1] ? 'checked' : ''}"/></td>
     <td><input type="checkbox" checked="{row[2] ? 'checked' : ''}"/></td>
     <td><input type="checkbox" checked="{row[3] ? 'checked' : ''}"/></td>
   </tr>);
})}
Sign up to request clarification or add additional context in comments.

Comments

0

You can organize your data also as an object: let data = [ { columnOne: true, columnTwo: false, columnThree: true, columnHundred: 42 }, { columnOne: true, columnTwo: false, columnThree: true, columnHundred: 0 }, { columnOne: false, columnTwo: false, columnThree: true, columnHundred: 42 }, ]

And then you can update data array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.