Display checkbox only if checked

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Display checkbox only if checked

john_stephens
Hello,

In my spring project, I am displaying a list of checkboxes from database and the checked boxes are displaying correctly. But along with checked boxes, unchecked boxes also getting shown.

<div class="form-group col-xs-12 col-sm-12 col-md-6">
<label for="deptLoc">Dept Location</label>                                                               
<div class="input-group" th:each="deptLoc: ${DevApproach}" >                                                                                                                                                                       
<input type="checkbox" th:value="${deptLoc.id}"  th:field="*{deptLocations}" disabled/>
                                                                                                                                                                                                                               
</div>                                                                               
</div> 

How can I show only the checked checkboxes ?

Thanks for help.
Reply | Threaded
Open this post in threaded view
|

Re: Display checkbox only if checked

Reiju
<input type="checkbox" th:if="${condition}"...

Or show ur model classes please
Reply | Threaded
Open this post in threaded view
|

Re: Display checkbox only if checked

john_stephens
What should be the condition? The model classes have some basic data as location id, location name etc. When i checked the source of the html page in browser, i saw checked boxes as checked="checked". How to capture it in thymeleaf?
Reply | Threaded
Open this post in threaded view
|

Re: Display checkbox only if checked

Reiju
Sorry, I don't quite understand what you want to do. But I hope this example will be useful.

public class SomeObject {
    private Long id;
    private String name;
    private Boolean condition;
}

public class SomeDto {
    private List<SomeObject> someObjects;
}

<form th:action="${'/someAction'}" method="post" th:object="${someDto}">
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>CONDITION</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="o, status : ${someDto.someObjects}">
            <div th:if="${!o.condition}">
                <td><input type="text" th:field="*{someObjects[__${status.index}__].id}"/></td>
                <td><input type="text" th:field="*{someObjects[__${status.index}__].name}"/></td>
                <td><input type="checkbox" th:field="*{someObjects[__${status.index}__].condition}"/></td>
            </div>
        </tr>
        </tbody>
    </table>
    <button type="submit">Click</button>
</form>
Reply | Threaded
Open this post in threaded view
|

Re: Display checkbox only if checked

john_stephens
Thanks for the excellent reply. But i dont have any condition as such. I just need to filter out the unchecked boxes. Does thymeleaf have any in built function for this?