Thymeleaf地图中的相关下拉列表

时间:2016-03-10 13:30:38

标签: javascript java jquery thymeleaf

我想创建一个包含国家/地区的下拉列表和第二个包含城市的下拉列表,这取决于第一个列表中的选定值。并且应该动态更改城市列表。 在视图(Thymeleaf)中,我有一个来自控制器的Map<CountryModel, Set<RegionModel>>。 CountryModel的名称应显示在第二个下拉列表中,Set应显示在第二个(从属)下拉列表中。
在这里,我创建了第一个下拉列表:

 <tr>
   <td th:text="#{country}"/>
   <td>
      <div class="form-group">
          <select th:field="*{transferRequestModel.country}" class="form-control" id="country">
                <option th:each="country : ${transferModel.countries}"
                    th:value="${country}"
                    th:text="${country.key.countryName}">Wireframe
                </option>
          </select>
      </div>
    </td>
 </tr>

那么如何创建第二个下拉列表,该列表取决于第一个列表中的所选国家/地区?

2 个答案:

答案 0 :(得分:4)

所以我用AJAX请求和jQuery追加解决了我的问题。

  1. Map<CountryModel, Set<RegionModel>>更改为Map<String, Set<String>>

  2. AJAX请求

    function sendAjaxRequest() {
        var country = $("#country").val();
        $.get( "/regions?country=" + country, function( data ) {
            $("#region").empty();
            data.forEach(function(item, i) {
                var option = "<option value = " + item + ">" + item +  "</option>";
                $("#region").append(option);
            });
        });
    };
    
  3. 更改第一个下拉列表时使用sendAjaxRequest()

    $(document).ready(function() {
        $("#country").change(function() {
            sendAjaxRequest();
        });
    });
    
  4. Thymeleaf模板下拉列表

  5. 第一个下拉列表

    <td th:text="#{country}"/>
    <td>
        <div class="form-group">
            <select th:field="*{model.country}" class="form-control" id="country">
                <option th:each="country : ${model.countries}"
                        th:value="${country}"
                        th:text="${country}">Wireframe
                </option>
            </select>
        </div>
    </td>
    

    第二个下拉列表

    <td>
        <div class="form-group">
            <select th:field="*{requestModel.region}" class="form-control" id="region">
            </select>
        </div>
    </td>
    
    1. 控制器

      @RequestMapping(value = "/regions")
      @ResponseBody
      public Set getRegions(@RequestParam String country) {
          Map<String, Set<String>> regions = regionsService.getRegions();
          return regions.get(country);
      }
      

答案 1 :(得分:0)

在我们的项目中,我们这样做了:

<div class="form-group">
    <label class="col-sm-4 control-label" 
           th:text="#{person.edit.policy.tfoms}"></label>

    <div class="col-sm-8">
        <select class="form-control" th:field="*{tfoms}" 
                onchange="loadInsuranceCompanies()">
            <option th:each="t : ${tfomses}" 
                    th:value="${t.uidtfoms}"  
                    th:text="${t.shortName}"
                    th:selected="${personBean.tfoms != null 
                    and personBean.tfoms.equals(t)}">
            </option>
        </select>
    </div>
</div>
<div th:class="${#fields.hasErrors('insuranceCompany')} 
     ? 'form-group has-error' : 'form-group'">
    <label class="col-sm-4 control-label" 
          th:text="#{person.edit.policy.ic}">
    </label>

    <div class="col-sm-8" id="insuranceCompaniesContent">
        <select class="form-control" id="insuranceCompany" 
                name="insuranceCompany"  
                th:fragment="insuranceCompany">
            <option th:each="i : ${insuranceCompanies}" 
                    th:value="${i.uidinsurancecompany}"  
                    th:text="${i.shortName}"
                    th:selected="${personBean.insuranceCompany != null 
                    and personBean.insuranceCompany.equals(i)}">
            </option>
        </select>
        <div th:if="${#fields.hasErrors('insuranceCompany')}" 
         th:each="err : ${#fields.errors('insuranceCompany')}">
        <span class="text-danger" th:text="${err}"></span><br/>
        </div>
    </div>
</div>

保险公司加载函数loadInsuranceCompanies()

function loadInsuranceCompanies() {
    var url = /*[[@{/PersonEdit/insuranceCompanies}]]*/ "/PersonEdit/insuranceCompanies";
    if ($('#tfoms').val() !== '') {
        url = url + '/' + $('#tfoms').val();
    }
    $("#insuranceCompaniesContent").load(url);
}

最后在控制器中编写代码:

@RequestMapping(value = "/PersonEdit/insuranceCompanies/{tfoms}", method = RequestMethod.GET)
public String getInsuranceCompaniesByTfoms(@PathVariable("tfoms") Integer tfomsId,
        Model model) {
    model.addAttribute("insuranceCompanies", insuranceCompanyService
            .getInsuranceCompaniesByTfoms(new TerritorialFondOms(tfomsId)));
    return "person/PersonEdit :: insuranceCompany";
}