[英]Dynamic dropdowns using thymeleaf, spring boot
我有3個下拉菜單(一個是城市,一個是代理,一個是服務)。 如果我選擇一個城市,第二個下拉列表應該加載數據(代理商),如果我選擇一個代理商,3下拉列表應該加載數據(服務)。 我可以填充第一個下拉列表(城市),但我不知道如何填充第二個和第三個。
我應該為每個下拉列表寫一個控制器並返回值嗎? 如果答案是肯定的,我該如何返回該值? 我讀過Thymeleaf不是組件技術,而是像JSP這樣的模板技術。 因此,Thymeleaf中沒有組件或內置機制來進行客戶端 - 服務器通信。 所以我需要使用普通的舊HTML表單或使用AJAX調用來編程通信。 如何使用普通的舊HTML編程?
我嘗試使用表單,但我只點擊提交一次,這不是我需要的。 我閱讀了有關下拉菜單的帖子,但找不到任何有用的信息。 我看到簡單的方法是使用jQuery,但我不知道jQuery。 有沒有辦法只使用百里香和彈簧靴來做到這一點? 謝謝! 我將在下面發布我的代碼。
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
@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));
}
所以我能用jQuery解決這個問題。
這是一個有用的鏈接: http : //www.rockhoppertech.com/blog/spring-mvc-3-cascading-selects-using-jquery/我將在下面發布我的代碼,也許會幫助某人
-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
<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-一個下拉列表
$('#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.