lundi 6 février 2023

select user with checkbox

I want to create a checkbox for every user in the table, this my tableBody :

              <TableBody>
                {users.map((user) => {                
                  return (
                    <TableRow
                      key={user.id}
                    >
                      <TableCell padding="checkbox">
                        <Checkbox
                          checked={selectedUserIds.indexOf(user.id) !== -1} // check if the user.id is in selectedUserIds 
                          onChange={() => handleSelectUser(user.id)} // evry time I try to find the logic for this function I fail
                          value="true"
                        />
                      </TableCell>
                      <TableCell>{user.email}</TableCell>
                      <TableCell>{user.roles}</TableCell>
                      <TableCell>{user.status}</TableCell>
                    </TableRow>
                  );
                })}
              </TableBody>

I created this variable for storing the selectedUserIds as shown in the example above

const [selectedUserIds, setSelectedUserIds] = useState([]);

the problem is that I can't create the function handleSelectUser that add/remove the user checked/unchecked in/from selectedUserIds I tried so many times and I failed. if someone can help me I will be very thankfull




Aucun commentaire:

Enregistrer un commentaire