根据不同下拉菜单的选择填充下拉菜单

时间:2020-05-01 22:49:20

标签: jquery ajax spring-boot

我的Spring Boot Java thymeleaf项目有一个带有从SQL表填充的下拉菜单的表单。该表单具有第二个下拉列表,我要根据第一个下拉列表中选择的项目来填充该列表,为此,我需要传递第一个下拉列表的值的ID。我知道我必须使用JQuery来完成此操作,但是我需要执行两个Ajax调用吗?一个传递选定的ID,一个传递新列表的第二个下拉列表? get ajax应该在发布ajax之后还是在其内部? 有人可以帮助我更好地理解实现我想要做的事情的最佳方法吗?

这是我到目前为止所拥有的:

<td> <select  class="form-control" id="parentCo" th:field="*{ams360Policies[__${stat.index}__].parentCompany}" required="true" >
      <option th:value="select">-Select-</option>
      <th:block th:each="carrier : ${carriers}">
               <option th:value="${carrier.parentCompanyCarrier}" th:text="${carrier.parentCompanyCarrier}" th:data="${carrier.id}"></option>
       </th:block>
        </select>
</td>
<td><select class="form-control" id="writeCos" th:field="*{ams360Policies[__${stat.index}__].writingCompany}" required="true" >

     </select>
</td>

//grab value of selected carrier
$('#parentCo').on('change', function(event){
    $('#parentCo').find('option:selected');
    var id = $(this).attr('data');
    $.ajax({
        type: "POST",
        url: "/carrierAjax",
        data: id,
        processData: false,
        contentType: false,
        cache: false,
        success: function(data, jqXHR){
            console.log('sent ID successfully')
        }
    });
    
     $.ajax({
        type: "GET",
        url: "/carrierAjax",
        data: carrierUmbrellas,
        processData: false,
        contentType: false,
        cache: false,
        success: function(data, jqXHR){
            //do afor each here to append each writeCo
            data.each(function(writeCo) {
            $('#writeCos').append('<option th:value="${writingCo.id}" th:text="${writingCo.companyName}"></option>')
        }
});

或者如果我只需要打一个AJAX电话,会是这样吗?

$('#parentCo').on('change', function(event) {
  $('#parentCo').find('option:selected');
  var id = $('#parentCo').find('option:selected').attr('data');
  console.log(jQuery.type(id));
  var token = $("meta[name='_csrf']").attr("content");
  var header = $("meta[name='_csrf_header']").attr("content");
  $.ajax({
    type: "POST",
    url: "/carrierAjax",
    data: {
      data: $('#parentCo').find('option:selected').attr('data')
    },
    cache: false,
    timeout: 600000,
    beforeSend: function(xhr) {
      xhr.setRequestHeader(header, token);
      console.log(header + ", " + token);
    },
    success: function(data) {
      //need to pass the value back from the spring controller here and append it below in an option tag...
      $('#writeCos').append('<option value=""></option>');
      console.log('successful');

    }
  });
});

在我的控制器中:

@RequestMapping(value="/carrierAjax", method=RequestMethod.POST)
    public @ResponseBody List <CarrierUmbrella>  getcarrierswriteco (@RequestParam("data") String data, DirectBind directBind, Model model) throws Exception{
        List <CarrierUmbrella> carrierUmbrellas = carrierUmbrellaRepository.findByCarrier(caRepository.findById(Long.parseLong(data)));
        model.addAttribute("carrierUmbrellas", carrierUmbrellas);
        return carrierUmbrellas;
    }

0 个答案:

没有答案