I have two string collection.
One is to generate all check boxes.
The other one is to select the element to check.
For example, I have two string collections ["A", "B", "C", "D"] ["A", "C"]
I want to make 4 check boxes A, B, C, D and want to check A and C
I tried as below.
<span th:each="interestName : ${allInterest}">
<span th:each="interest : ${userInfo.interestName}">
<input type="checkbox"
name="eachUserInterest"
th:value="${interestName.name}"
th:checked="${interestName.name.equals(interest)}"/>
<label th:text="${interestName.name}"></label>
</span>
</span>
However, above code generates a check box repeatedly as many times as it is checked. Execution Results
Help me, please.
You should just loop over the allInterest
array, and use contains
to determine whether or not the checkbox is checked. Something like this for example:
<th:block th:each="interest: ${allInterest}">
<input type="checkbox"
name="eachUserInterest"
th:value="${interest}"
th:checked="${userInfo.interestName.contains(interest)}" />
<label th:text="${interest}"></label>
</th:block>