0

I make table contain students names, and checkbox for every row in the table, I want so store the students names that checked in the (students) array. How can I do that?

const [data, setData] = useState([]);  
    const [students, setStudents] = useState([]);  

    
  useEffect(() => {  
    Axios  
        .get("http://localhost:3003/studentsnotinsections")  
        .then(result => setData(result.data));  
}, []);  

console.log(students)
return(
  <div>
 <table className="table" >  
                <thead className="thead-dark">  
                    <tr>  
                        <th scope="col">Name</th>  
                    </tr>  
                </thead>  
                <tbody>                      
                    {data.map((item, id) => {  
                        return <tr key={id}>
                            <td>{item.FullName}</td> 
                            <td><input type="checkbox" /></td> 
                        </tr>  
                    })}  
                </tbody>  
            </table>
  </div>
)}

2 Answers 2

3

Did you try simply adding onclick function to your checkbox?

const Compon = () => {
  const [data, setData] = useState([]);  
  const [students, setStudents] = useState([]);  
  
      
  useEffect(() => {  
    Axios  
        .get("http://localhost:3003/studentsnotinsections")  
        .then(result => setData(result.data));  
  }, []);  
  const addOrRemove = (name) => {
    const newStudents = [...students];
    const index = newStudents.indexOf(name);
    if (index === -1) {
      newStudents.push(name);
    } else {
      newStudents.splice(index, 1);
    }
    setStudents(newStudents);
    console.log(students)
  }
  
  return(
    <div>
     <table className="table" >  
        <thead className="thead-dark">  
            <tr>  
                <th scope="col">Name</th>  
            </tr>  
         </thead>  
        <tbody>                      
            {data.map((item, id) => {  
                return <tr key={id}>
                    <td>{item.FullName}</td> 
                    <td><input type="checkbox" onClick={() => addOrRemove(item.FullName)} /></td> 
                </tr>  
            })}  
        </tbody>  
      </table>
     </div>
  )
  }

Sign up to request clarification or add additional context in comments.

Comments

0

You could use onChange handler, it's more standard for input handling that onClick

import { useCallback, useState } from "react";

export default function App() {
  const [data, setData] = useState([
    {FullName: 'Alex'},
    {FullName: 'Richard'}
  ]);  
  const [students, setStudents] = useState([]);  

  let toggleValue = useCallback((event, id) => {
     if (event.target.checked) {
       setStudents(value => [...value, id])
     } else {
       setStudents(value => value.filter(it => it !== id))
     }
  }, [setStudents])

  console.log(students)
  return(
    <div>
      <table className="table" >  
        <thead className="thead-dark">  
          <tr>  
            <th scope="col">Name</th>  
          </tr>  
        </thead>  
        <tbody>                      
          {data.map((item, id) => {  
            return <tr key={id}>
                     <td>{item.FullName}</td> 
                     <td><input type="checkbox" onChange={(e) => toggleValue(e, id)} /></td> 
                   </tr>  
          })}  
        </tbody>  
      </table>
    </div>
)
}

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.