Hello everyone, I am new to Ionic Framework. So far I have been able to develop the project and it has been amazing so far.
I am just stuck on the following code, unable to keep the checkboxes ticked/checked and pass the values to Laravel back-end.
<Modal.Header>
<Modal.Title><h6>Cuisines</h6></Modal.Title>
</Modal.Header>
<Modal.Body>
{items.map((val: any, index: any) => <div key={val.id}>
<IonCheckbox id={`cuisines-${val.id}`} name="cuisine" value={val.id} onIonChange={handleCheck} /> {val.title}
</div>)}</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={handleClose}>
Submit
</Button>
</Modal.Footer>
</Modal>
This is the dynamic cuisine checkbox.
const [checked, setChecked] = useState<any[]>([]);
// Add/Remove checked item from list
const handleCheck = (event:any) => {
var cuisineCheck = [...checked];
if (event.target.checked) {
cuisineCheck = [...checked, event.target.value];
} else {
cuisineCheck.splice(checked.indexOf(event.target.value), 1);
}
setChecked(cuisineCheck);
console.log(cuisineCheck);
};
And this is the checkbox to get the list of checked cuisines.
- Console log shows that this works however the checkbox doesn’t stay checked.
- And I am not sure how to pass the data to Laravel’s back-end.