I have a model which includes a boolean 'deleted'. I generate a list and each element has this variable within it.
I print it out in a list and I'm trying to set it using a checkbox, so it saves whatever is set.
Model
public class entry{
private boolean deleted;
public entry(boolean deleted){
this.deleted = deleted;
}
//setters & getters for deleted
}
I then have my thymeleaf form read in this list using a wrapper class. Which works fine.
I can get the checkbox to display checked/unchecked depending on the deleted state for each element, but it if I change the checkbox from checked to unchecked...it doesn't then save the variable to that state.
HTML
<div th:each="entry, stat: *{entryList}">
<td>
<label class="switch">
<input name="index" th:type="checkbox" th:checked="*{entryList[__${stat.index}__].deleted}">
</input> <span class="delete round">
</span>
</label>
</td>
</div>
TH:Object
<form action="#" th:action="@{submitData}" class="saveForm"
method="post" th:object="${entryWrapper}">
...
...
</form>
Wrapper Class
public class EntryWrapper {
@Autowired
private List<Entry> entryList;
public EntryWrapper(){
}
public EntryWrapper(List<Entry> entryList){
this.entryList = entryList;
}
public List<Entry> getEntryList(){
return entryList;
}
public void add(final Entry entry){
this.entryList.add(entry);
}
public void setEntryList(List<Entry> entryList){
this.entryList = entryList;
}
}
Instead of th:checked
, you should use th:field
<input type="checkbox" th:field="*{entryList[__${stat.index}__].deleted}">
th:field
will generate the name, id and checked attributes, so when you post the form it should populate your model object.