2016-05-23 91 views
0

我想使用ajaxspring mvc刪除多個行,但它總是刪除一行。如何使用ajax和spring刪除多行數據庫mvc

//code controller 

@RequestMapping(value = "/rmvclientserviceajax", method = RequestMethod.POST) 

@ResponseBody 

public void rmvclintServiceajax (HttpServletRequest request, Model model) 
     { 
      for(String serviceID: request.getParameterValues("idService")) 
      { Long clientID = (long) Integer.parseInt(request.getParameter("idClient"));    
       metiersr.deleteClientToService(clientID,(long) Integer.parseInt(serviceID));}    
     } 

//code js 

function AjaxPostdelete() { 
     if ($('#idService').prop('checked')) { 
        var idService = $('#idService').val(); 
        var idClient = $('#idClient').val(); 
         $.ajax({ 
          type : "POST", 
          url : "/cp/client/rmvclientserviceajax", 
          data : "idClient=" + idClient + "&idService=" + idService, 
           success : function(response) { 
            { 
         { 
      }  
    } 

// code html 
    <form> 
     <ul class="liste_servch"> 
       <input type="hidden" id="idClient" value="${client.idPersonne}" /> 
        <c:forEach items="${listservclt}" var="servclt"> 
         <li> 
         <input id="idService" type="checkbox" value="${servclt.idService}" /> 
         <c:out value="${servclt.nomService}" /> 
         </li> 
        </c:forEach> 
     </ul> 
      <input type="button" value="supp" onclick="AjaxPostdelete() "> 
    </form> 
+0

你想發佈您的js代碼嗎? – Mippy

+0

你可以用serviceID中的值來打你的控制器方法嗎? 更好地發佈你的JS代碼和錯誤日誌。 –

+0

@Mippy我加了我的代碼js, – Raki

回答

0

嘗試以下;)

function AjaxPostdelete() { 
    var idServiceObjs = $('input[id=idService]'); 
    var idServices = []; 
    for (var i = 0; i < idServiceObjs.length; i++) { 
     if(idServiceObjs[i].prop('checked')) { 
      idServices.push(idServiceObjs[i].value); 
     } 
    } 
    var idClient = $('#idClient').val(); 
    $.ajax({ 
     type : "POST", 
     url : "/cp/client/rmvclientserviceajax", 
     data : {idClient:idClient,idService:idServices}, 
     success : function(response) { 
     } 
    } 

}

相關問題