Java Thymeleaf预选选择2个选项
我正在尝试使用select2呈现具有多选列表的编辑页面。根据select2文档,只需设置一个选项Java Thymeleaf预选选择2个选项,java,jquery-select2,thymeleaf,Java,Jquery Select2,Thymeleaf,我正在尝试使用select2呈现具有多选列表的编辑页面。根据select2文档,只需设置一个选项selected=“selected”就可以了,但我使用的是Java/Spring和Thymeleaf模板,所以有点棘手 <select id="programs" name="programs.id" style="min-width: 200px;" multiple="multiple"> <option th:each="programs :
selected=“selected”
就可以了,但我使用的是Java/Spring和Thymeleaf模板,所以有点棘手
<select id="programs" name="programs.id" style="min-width: 200px;" multiple="multiple">
<option
th:each="programs : ${programs}"
th:value="${programs.id}"
th:text="'['+${programs.category.name}+'] '+${programs.name}"
th:selected="${#lists.contains(personProg,programs.id)}">
</option>
</select>
java控制台中的输出:
69
1
2
3
66
6
37
59
58
我做错了什么?发送过来的模型属性是一个
personProg
列表,而不是您想要的ID列表。制作一个列表,并发送ID列表而不是对象列表
List<Integer> programIdList = new ArrayList<Integer>();
for (PersonPrograms x : personProgRepo.findByPersonid(person.getID())) {
programIdList.add(x.getProgramid());
}
model.addAttribute("personProg", programIdList.toString());
List programmidlist=new ArrayList();
对于(PersonX:personProgRepo.findByPersonid(person.getID())){
add(x.getProgramid());
}
addAttribute(“personProg”,programmidlist.toString());
List<Integer> programIdList = new ArrayList<Integer>();
for (PersonPrograms x : personProgRepo.findByPersonid(person.getID())) {
programIdList.add(x.getProgramid());
}
model.addAttribute("personProg", programIdList.toString());