我有一个新的 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>
该表对应于Person表单的以下属性
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(){
}
});
}
这是 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";
}
我正在尝试从模型中检索 PersonForm 并使用新值更新列表。但是当我尝试保存它时,PersonForm 中的 headersType 为空,当然表格不会显示添加的标题。
编辑:我发布了一个临时解决方案,但我更喜欢使用 JSON
最佳答案
我找到了一个临时解决方案来做到这一点。我使用 JQuery 编写了所有代码,并附加带有隐藏字段的行,我更喜欢 JSON 解决方案,但目前...
添加标题
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>");
}
我亲自更改Set<EntityHeader>
对于一个简单的数组 EntityHeader[]
仍在等待其他答案
关于java - Spring MVC 中的 JSON 动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304971/