[英]Dynamic dropdowns using thymeleaf, spring boot
I have 3 dropdowns(one is populate with cities, one with agencies and one with services). 我有3个下拉菜单(一个是城市,一个是代理,一个是服务)。 If I select one city, the second dropdown should load the data(agencies), and if i select one agency, the 3 dropdown should load data(services). 如果我选择一个城市,第二个下拉列表应该加载数据(代理商),如果我选择一个代理商,3下拉列表应该加载数据(服务)。 I am able to populate the first dropdown(cities), but I don't know how to populate the second and third. 我可以填充第一个下拉列表(城市),但我不知道如何填充第二个和第三个。
Should i write a controller for each dropdown and return the value? 我应该为每个下拉列表写一个控制器并返回值吗? If the answer is yes, how can I return the value? 如果答案是肯定的,我该如何返回该值? I've read that Thymeleaf is not a component technology but a template technology like JSP. 我读过Thymeleaf不是组件技术,而是像JSP这样的模板技术。 So there is not components or built-in mechanism in Thymeleaf to do client-server communication. 因此,Thymeleaf中没有组件或内置机制来进行客户端 - 服务器通信。 So I need to program that communication using plain old HTML forms or using AJAX calls. 所以我需要使用普通的旧HTML表单或使用AJAX调用来编程通信。 How can I program it using plain old HTML? 如何使用普通的旧HTML编程?
I tried using forms, but I click submit only once, it is not what I need. 我尝试使用表单,但我只点击提交一次,这不是我需要的。 I read the posts about dropdowns, but couldn't find anything helpful. 我阅读了有关下拉菜单的帖子,但找不到任何有用的信息。 I saw that the easy way is to use jQuery, but I don't know jQuery. 我看到简单的方法是使用jQuery,但我不知道jQuery。 Is there a way I can do this using only thymeleaf and spring boot? 有没有办法只使用百里香和弹簧靴来做到这一点? Thanks! 谢谢! I will post my code below. 我将在下面发布我的代码。
appointment.html appointment.html
<form th:action="@{/appointment/create}" method="post" id="appointmentForm">
<input type="hidden" name="id" th:value="${appointment.id}"/>
<div class="form-group">
<label for="location">Alege orasul:</label>
<select class="form-control" required="required"
th:value="${appointment.location}" name="location" id="location">
<option disabled="disabled" selected="selected" > --
alege orasul --</option>
<option th:each="city : ${cities}" th:value="${city.id}"
th:text="${city.name}" ></option>
</select>
</div>
</form>
<form th:action="@{/appointment/agency}" method="post" id="appointmentForm">
<input type="hidden" name="id" th:value="${appointment.id}"/>
<div class="form-group">
<label for="location">Alege agentia:</label>
<select class="form-control" th:value="${appointment.agency}" name="agency" id="agency" required="required">
<option disabled="disabled" selected="selected" > -- alege agentia --</option>
<option th:each="agency : ${agencies}" th:value="${agency.id}" th:text="${agency.name}" ></option>
</select>
</div>
</form>
<form th:action="@{/appointment/service}" method="post" id="appointmentForm">
<input type="hidden" name="id" th:value="${appointment.id}"/>
<div class="form-group">
<label for="location">Alege serviciul:</label>
<select class="form-control" th:value="${appointment.service}" name="service" id="service" required="required">
<option disabled="disabled" selected="selected" > -- alege serviciul --</option>
<option th:each="service : ${services}" th:value="${service.id}" th:text="${service.name}" ></option>
</select>
</div>
</form>
AppController.java AppController.java
@Controller
@RequestMapping("/appointment")
public class AppointmentController {
@Autowired
UserService userService;
AppointmentService appointmentService;
CityService cityService;
AgencyService agencyService;
SerService serService;
private ModelAndView mav;
@RequestMapping(value="/create", method=RequestMethod.GET)
public String createAppointmentPost(Model model, @ModelAttribute("city") City
city, @ModelAttribute("agency") Agency agency){
Appointment appointment=new Appointment();
model.addAttribute("appointment", appointment);
model.addAttribute("dateString", "");
model.addAttribute("cities", cityService.findAll());
//getAllAgencies(model, city);
getAllServices(model,agency);
return "appointment";
}
@RequestMapping(value="/agency", method=RequestMethod.GET)
public String getAllAgencies(Model model, @ModelAttribute("city") City city){
model.addAttribute("agencies", agencyService.listAllAgencies(city));
return "redirect:/appointment/create";
}
public void getAllServices(Model model, @ModelAttribute("agency") Agency
agency){
if(agency==null){
return;
}
model.addAttribute("services", serService.listAllServices(agency));
}
So I was able solving this using jQuery. 所以我能用jQuery解决这个问题。
Here is a useful link: http://www.rockhoppertech.com/blog/spring-mvc-3-cascading-selects-using-jquery/ I will post my code below, maybe will help someone 这是一个有用的链接: http : //www.rockhoppertech.com/blog/spring-mvc-3-cascading-selects-using-jquery/我将在下面发布我的代码,也许会帮助某人
-mycontroller -mycontroller
@RequestMapping(value="/create", method=RequestMethod.GET)
public String createAppointmentPost(Model model, @ModelAttribute("city") City
city,
@ModelAttribute("agency") Agency agency){
Appointment appointment=new Appointment();
model.addAttribute("appointment", appointment);
model.addAttribute("dateString", "");
model.addAttribute("cities", cityService.findAll());
return "appointment";
}
@RequestMapping(value = "/agencies", method = RequestMethod.GET)
public @ResponseBody
List<Agency> findAllAgencies(
@RequestParam(value = "cityId", required = true) Long cityId) {
City city = cityService.findCity(cityId);
return agencyService.listAllAgencies(city);
}
-thymeleaf -thymeleaf
<div class="form-group">
<label for="location">Alege orasul:</label>
<select class="form-control" required="required"
th:value="${appointment.location}" name="location" id="location">
<option disabled="disabled" selected="selected" > --
alege orasul --
</option>
<option th:each="city : ${cities}" th:value="${city.id}"
th:text="${city.name}" >
</option>
</select>
</div>
<div class="form-group">
<label for="location">Alege agentia:</label>
<select class="form-control" th:value="${appointment.agency}"
name="agency" id="agency" required="required">
<option disabled="disabled" selected="selected" > --alege
agentia --</option>
</select>
</div>
jQuery- for one dropdown jQuery-一个下拉列表
$('#location').change(
function() {
$.getJSON("http://localhost:8181/appointment/agencies", {
cityId : $(this).val(),
ajax : 'true'
}, function(data) {
var html = '<option value="">--alege agentia--</option>';
var len = data.length;
for ( var i = 0; i < len; i++) {
html += '<option value="' + data[i].nume + '">'
+ data[i].nume + '</option>';
}
html += '</option>';
$('#agency').html(html);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.