mercredi 19 octobre 2016

How to bind a list as Checkbox Field

I have a Class that has a Set property of type Car.

Car has a enum property "make". There are 5 available options in MakeEnum enum.

I'm trying to display all the enums as a list of checkboxes.

For Example

If the following Set "cars" (see below) contains a single element.

The form below displays this element as a checked checkbox field.

How do I add the remaining for 4 options as checkboxes?

Model

public class Machine { 
        private Long id; 
        private Set<Cars> cars = new HashSet<Cars>(); 
        private Truck truck; 
        private Boat boat; 

        //Accessors 
} 

Notice that the 'make' field is an enum (e.g. ford, mustang, jeep, volvo, volkwagen).

public class Car { 
        private Long id; 
        private MakeEnum make; 

        //Accessors 
} 

Form

Lists the cars in the set. How do I add the other options?

<form accept-charset="UTF-8" th:object="${machine}" th:action="@{/machine}" method="post">

    <ul>
        <li th:each="car, iterStat : ${machine.cars}">
            <input th:field="*{cars}" type="checkbox" th:value="${car.make}"/>
                <label th:for="'car'+${iterStat.count}" th:text="${car.make}"></label>                            
        </li 

        //If collection holds less the 5 cars then add missing options here. 
    </ul>

.... 
}




Aucun commentaire:

Enregistrer un commentaire