Thymeleaf/Spring - 将项目从组合框添加到列表到表格 [英] Thymeleaf/Spring - Add items to a list from a combobox to a table

查看:29
本文介绍了Thymeleaf/Spring - 将项目从组合框添加到列表到表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

有一个类叫做工具".这个工具类有一个分布点"列表.


在用户界面上:

用户从组合框又名选项 (HTML) 中选择一个项目(分发位置)并将其添加到表格中.之后,用户点击表单上的提交",表格中的所有内容都绑定到一个列表中.


问题

Spring 不会将表 '' 项作为列表发送.它不起作用.尝试了几件事,但都没有奏效.


代码片段

 ....<表单方法=POST"th:object="${tool}";th:action="@{/tools/add}">....<!--(分布点)--><div class="w-100 col-md-6"><table id="tabledistributionPoints";class="w-100 tab";边界=2"th:field=${tool.distributionPoints}"><tr id="head"><th>分发点</th></tr><tr th:each="ponto, index: ${tool.distributionPoints}"><td th:value=${ponto.id}";th:field="${tool.distributionPoints[index]}";th:text=${ponto.distributionPoint}"></td></tr><div class="row col-md-6"><select id="distributionPoint";th:object="${distributionPoints}";class =表单控件"><选项></选项><option th:id="${ponto.id}";th:each="ponto : ${distributionPoints}";th:value="${ponto.id}";th:text="${ponto.distributionPoint}"></option></选择><input id="AddPonto";类型=按钮"值=添加"名称=添加"onclick="AdddistributionPoint()";/><input id="RemovePonto";类型=按钮"值=删除"名称=删除"onclick="RemoveddistributionPoint()";/>

....</表单><脚本>函数添加分布点(){var ponto = $("#distributionPoint option:selected").text();var pontoId = $("#distributionPoint option:selected").attr("id");var pontoClean = cleanString(ponto);var rows = $("#tabledistributionPoints tr").length - 1;如果(ponto.toString()!=="){//if ($("#tabledistributionPoints").find("#" + pontoClean).length === 0) {var k = '<input th:field="${tool.distributionPoints[' + rows + '].id}";th:value="'+ ponto + '"th:name="${ponto.distributionPoint}";th:text="'+ ponto + '"值="'+ ponto + '"类型=文本"只读=只读"></>';$("#tabledistributionPoints > tBody:last>).append(<tr><td id="pontoClean +">>>k+</td></tr>");}}$("#tabledistributionPoints").on("click", "tr", function () {$(this).addClass("clicked").siblings().removeClass("clicked");});}函数移除分布点(){var rowCount = $("#tabledistributionPoints tr").length;如果(行数> 1){var selected = $(#tabledistributionPoints > tbody > tr.clicked");if (selected !== null && selected.length > 0) {选择.remove();}其他{$("#tabledistributionPoints tr:last").remove();}}}


@GetMapping("/tools/add")公共 ModelAndView formadd() {ModelAndView mv = new ModelAndView(addtool");工具工具=新工具();mv.addObject(doc);mv.addObject(distributionPoints",pontoDistribuicaoDAO.findAll());返回 mv;}@RequestMapping(value = "/tools/add", method = RequestMethod.POST)公共字符串salvar(工具工具){System.out.println(tool.getdistributionPoints());this.toolDAO.save(工具);返回重定向:/工具";}

我已经研究了很多,但我仍然很困惑.任何提示表示赞赏.

阅读的参考文献/链接

https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

如何使用 thymeleaf 绑定对象列表?


1- 用户从组合框中选择一项.2- 用户点击添加3- 从组合框中选择的项目进入表格.4- 用户从组合框中选择另一个随机项目.5- 用户点击添加6- 从组合框中选择的项目进入表格.

表格现在有 2 个项目.

用户点击保存,这两个项目被保存为列表'Distribution Points'的项目

解决方案

首先,你的标签缺少 name 属性.


                
            
发送“验证码”获取 | 15天全站免登陆