2015-08-31 61 views
1

我有有對象的表像這樣一個新的Person形式:動態列表

<table class="table table-striped table-condensed flip-content"> 
    <thead class="flip-content"> 
     <tr> 
      <th width="20%"> 
       Tipo encabezado 
      </th> 
      <th width="20%"> 
       Valor 
      </th> 
      <th width="10%"></th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr th:each="ht : ${headersType}"> 
      <td th:text="${ht.headerType.name}"></td> 
      <td th:text="${ht.value}"></td> 
      <td class="operations"> 
       <a class="delete" href="/profile/delete" th:href="@{/profile/delete/__${p.id}__}"> 
        Borrar 
       </a> 
      </td>    
     </tr> 
    </tbody> 
</table> 

此表對應於人物形式的下列財產

private Set<EntityHeader> headersType = new HashSet<EntityHeader>(); 

所以,當我按新標題鏈接時,我想用新信息更新此表而不重新加載頁面。現在,我有這樣的JSON的方法來做到這一點

function addHeader(){ 

    var headerType = $('#idHeaderType').val(); 
    var valueHeader = $('#valueHeaderType').val(); 
    var json = {"headerType.id" : headerType,"value" : valueHeader}; 
    $.ajax({ 
     url: "addHeader.json", 
     type: 'POST', 
     data: "idHeader="+headerType+"&valueHeader="+valueHeader, 
     dataType: "json", 
     success:function(){ 

     }, 
     error:function(){ 

     } 


    }); 
} 

這是控制器:

@RequestMapping(value = "person/addHeader.json", method = RequestMethod.POST, headers ="Accept=application/json") 
    public String addHeader(@RequestParam(value = "idHeader") String idHeader, 
      @RequestParam(value = "valueHeader") String valueHeader, @ModelAttribute("person") PersonForm personForm, 
      BindingResult bindingResult, Model model) { 

     EntityHeader eh = new EntityHeader(); 
     eh.setHeaderType((HeaderType) basicServ.findById(Long.parseLong(idHeader), HeaderType.class)); 
     eh.setValue(valueHeader); 
     personForm.getHeadersType().add(eh); 
     model.addAttribute("person", personForm); 


     return "person/new"; 

    } 

我試圖從模型檢索爲personForm並用新值更新列表。但是,當我嘗試保存它時,PersonForm中的headersType爲空,當然表中不顯示添加的標題。

編輯:我發佈一個臨時解決方案,但我喜歡用JSON

回答

0

做,我找到了一個臨時解決方案做到這一點。我用jQuery的所有代碼,並與隱藏字段追加行,我更喜歡JSON的解決方案,但目前...

的addHeader

function addHeader(){ 

    var count = 0; 

    var headerType = $('#idHeaderType').val(); 
    var headerTypeValue = $("#idHeaderType option:selected").text(); 
    var valueHeader = $('#valueHeaderType').val(); 

    var nuevoCount = count +1; 

    $('#cuerpoTabla').append("<tr id='encabezado_"+nuevoCount+"'><td>"+headerTypeValue+"<input type='hidden' name='headersType["+count+"].headerType.id' id=headersType["+count+"].headerType.id' value='"+headerType+"'/></td><td>"+valueHeader+"<input type='hidden' name='headersType["+count+"].value' id=headersType["+count+"].value' value='"+valueHeader+"'/></td></tr>"); 
} 

而且在爲personForm我改變Set<EntityHeader>了一個簡單的數組EntityHeader[]

仍在等待另一個答案