Hi,
I am having issue with multiple checkboxes on liveview. Am running phoenix 1.6.6 with alpinejs 3.10.4.
I have a search form with mutiple checkboxes to enable user select categories, problem is, if the liveview re-renders the checkboxes dont check themseves eventhough the x-data has then right values.
In the above markup, of liveview re-renders the page, the span will still show the right colors selected but the checkboxes will not be checked appropriately.
You have not given those checkboxes name attribute.
Checkboxes that belongs together must have same name attribute!!
<form method="get">
<input id="vehicle1" type="checkbox" name="vehicle" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input id="vehicle2" type="checkbox" name="vehicle" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle" value="Boat" checked>
<label for="vehicle3"> I have a boat</label><br><br>
<input type="submit" value="Submit">
</form>
I have managed to temporarily fix it by deleteing the array holding the checkboxdata and reassigning it on liveview update.
I Created a phx-hook, and on update i ran this by calling a function on the alpine store:
LiveView is in charge of the DOM. If you want something to be true in the DOM, like whether a checkbox is checked, you need to control that from an assign. In this particular case it’d be something like: