Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
<pre>
const [getselectedlist, setSelectedList] = React.useState([]);
const [getSelectIndex, setSelectIndex] = useState(0)
const setSelected = (getrow, index, selectIndex) => {
		if (getSelectIndex !== selectIndex) {
			setSelectedList([getrow])
			setMobileNumberCandi(getrow.mobile)
			console.log("res",getrow.mobile)

		}
		else {
			if (getselectedlist.filter(e => e === getrow).length === 1) {
				setSelectedList(getselectedlist.filter(e => e !== getrow));
			} else {
				setSelectedList(prevArray => [...prevArray, getrow]);
			}
		}
		setSelectIndex(selectIndex)
	}; 



JavaScript
<pre>
{props.row.candidateDetails ? props.row.candidateDetails.length > 0 ? props.row.candidateDetails.map((items, index) => (
                    <TableRow key={items.candguid} style={items.isCommunicated === true ? { background: '#e0e0e0' } : {}}>
                      <StyledTableCell style={items.isCommunicated === true ? { padding: "1px", textAlign: "center", fontFamily: "Titillium Web Light", fontWeight: 'bold' } : { padding: "5px", textAlign: "center", fontFamily: "Titillium Web Light" }}>
                        <Checkbox
                          checked={props.getselectedlist.filter(e => e.candguid === items.candguid).length === 1 ? true : false}
                          onChange={() => { props.setSelected(items, props.row, props.index) }}
                          name="checkedA"
                        />
                      </StyledTableCell>
                     
                    </TableRow>
                  )) : "There is no data" : ""}


What I have tried:

JavaScript
<pre>
const [getselectedlist, setSelectedList] = React.useState([]);
const [getSelectIndex, setSelectIndex] = useState(0)
const setSelected = (getrow, index, selectIndex) => {
  var updatedList = [...getselectedlist];
  if (getrow) {
    updatedList = [...getselectedlist, getrow];
  } else {
    updatedList.splice(getselectedlist.indexOf(getrow), 1);
  }
  setSelectedList(updatedList);
  setSelectIndex(selectIndex)
};
Posted
Updated 15-Oct-22 2:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900