How to Populate a Drop Down with a List Using Thymeleaf and Spring

How do I populate a drop down with a list using thymeleaf and spring

This is how I populate the drop down list.I think it may help to you get some idea about it.

Controller

List<Operator> operators =  operatorService.getAllOperaors()
model.addAttribute("operators", operators);

Model

  @Entity
@Table(name = "operator")
public class Operator {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@JsonIgnore
private Long id;

@NotBlank(message="operator Name cannot be empty")
@Column(name = "operator_name", nullable = false)
private String operatorName;

getters and setters ...

}

View

<div class="form-group blu-margin">
<select class="form-control" th:field="${operator.opeId}" id="dropOperator">
<option value="0">select operator</option>
<option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option>
</select>
</div>

Populate dropdown from an entity using Spring-mvc and thymeleaf

You should add student list in controller wherever open the form:

    ModelAndView model = new ModelAndView("studentList");
model.addObject(students) // get list from database or...
// students =studentRepository.findAll() or use your exclusive query

And <option>:

<option th:each="item : ${studentList}"
th:value=${item.id} // value you want...
th:text=${item.name}>
</option>

and change entities:

Meal.java:

@Entity
public class Meal {

@Id
@GeneratedValue
private Long id;

@OneToOne
@JoinColumn(name = "meel_cook_id")
private Student mealCook;

private String mealName;

private int mealPrice;

Student.java:

@Entity
public class Student {

@Id
@GeneratedValue
private Long id;

private String studentName;

@OneToOne(mappedBy = "mealCook")
@JsonBackReference
private Meal meal;

populating thymeleaf dropdown with data of another table

So you want to load Course combo inside subject html

You need to modify subject controller

@RequestMapping("/subject")
public String viewHomePage(Model model){
List<Subject> subjectDetails= subjectDAO.findAll();
List<Course> courseDetail= courseDAO.findAll();
model.addAttribute("subjectDetails",subjectDetails);
model.addAttribute("courses",courseDetail);
return "subject";
}

In HTML

<tr>
<td>Course:</td>
<td>
<select th:field="*{course_code}">
<option value="">Choose..</option>
<option th:each="course: ${courses}" th:value="${course.id}" th:text="${course.name}" />
</select>
</td>
</tr>
<tr>

Edit 1:

I think you are loading addSubject.html

In that case you need to modify addSubject controller

@RequestMapping("/subject/new")
public String addSubject(Model model){
Subject subject =new Subject();
model.addAttribute("subject",subject);
List<Course> courseDetail= courseDAO.findAll();
model.addAttribute("courses",courseDetail);
return "addSubject";
}

Edit 2:

As per your code in Git I can see

@Autowired
private SubjectDAO subjectDAO;
private CourseDAO courseDAO;

It should be

 @Autowired
private SubjectDAO subjectDAO;

@Autowired
private CourseDAO courseDAO;

dropdown lists with Thymeleaf and SpringBoot

You have forgot indicate the field from where it has to take the selected value:

Example: I supousse the User class has an attribute for institution.

<div class="form-group">
<label class="col-md-3 control-label">Institution</label>
<div class="col-md-5">
<div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}">
<select th:field="*{institution}">
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.name}"
th:text="${dropDownItem.name}" />
</select>
</div>
<div th:if="${institutionList == null or lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>

More info: http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dropdownlist-selectors

EDIT:
You need to indicate to your app how convert a Id of Insitution returned inside form (String type) to a Institution entity. For that you have to use a Converter.

First change the value of option to institutionId:

<div class="form-group">
<label class="col-md-3 control-label">Institution</label>
<div class="col-md-5">
<div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}">
<select th:field="*{institution}">
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.institutionId}"
th:text="${dropDownItem.name}" />
</select>
</div>
<div th:if="${institutionList == null or lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>

You have to create a class that implements the Converter interface.

@Component
public class StringToInstitution implements Converter<String, Institution> {

@Autowired
private InstitutionRepository repository; //Or the class that implments the repository.

@Override
public Institution convert(String arg0) {
Long id = new Long(arg0);
return repository.findOne(id);
}

}


Related Topics



Leave a reply



Submit