thymeleaf / thymeleaf-spring

Thymeleaf integration module for Spring
http://www.thymeleaf.org
Apache License 2.0
435 stars 157 forks source link

Thymeleaf + Springboot + Bootstrap - Checkbox #326

Open eventoul opened 4 months ago

eventoul commented 4 months ago

Hi!

I have an issue with the code below:

`

                        <tr th:each="course : ${department.courses}">

                            <th th:text="${course.id}"></th>
                            <th th:text="${course.title}"></th>
                            <th th:text="${course.mandatory}"></th>
                            <th th:text="${course.duration}"></th>
                            <th th:text="${course.credits}"></th>
                            <th>
                                <div class="form-check">
                                    <label class="form-check-label" for="checkboxCourses"></label>
                                    <input id="checkboxCourses"  type="checkbox"
                                           class="form-check-input"
                                           th:value="${course.id}"
                                           th:field="*{courses}"
                                           th:checked="${course.mandatory}"
                                    />
                                </div>
                            </th>
                        </tr>
                    </tbody>`

I want the checkbox to be checked when a course is mandatory.

The th:field="*{courses}" refers to a Student model, which has a many-to-many relationship with the Course model.

I noticed that the th:checked="${course.mandatory}" works when I didn't have the th:field, but then I can't bind the course.id to save it in DB.

Any ideas?

PS: I want to be checked and disabled.

LalithK90 commented 2 months ago

yap @eventoul

I have the same issue but I manage it using the following

<input id="checkboxCourses"  
        type="checkbox" 
        class="form-check-input" 
        th:value="${course.id}" 
        name="courses"                
        th:checked="${course.mandatory}"
          />

There is an assumption that course.mandatory variable value is "selected"