java - Dynamic list with JSON in Spring MVC -
i have new person form have table of objects this:
<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>
this table corresponds following property of person form
private set<entityheader> headerstype = new hashset<entityheader>();
so, when press new header link want update table new info without reloading page. now, have json method it
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(){ } }); }
this controller:
@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"; }
i'm trying retrieve personform model , update list new values. when try save it, headerstype in personform empty , of course table doesn't show added headers.
edit: post provisional solution prefer json
i found provisional solution this. made code jquery , append rows hidden fields, prefer json solution, @ moment...
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>"); }
and in personform change set<entityheader>
simple array entityheader[]
still waiting answers
Comments
Post a Comment