[英]How to display input values in a datatable with dynamic columns using thymeleaf
I am trying to create a form list with a Data Table where I place some input values from a collection.我正在尝试使用数据表创建一个表单列表,在其中放置一些来自集合的输入值。 The fact is that I don't know how to display the values with connection with the Controller to display and register the values from the input correctly.
事实是,我不知道如何显示与 Controller 连接的值以正确显示和注册来自输入的值。
This is the DTO I want to display as a Form List:这是我要显示为表单列表的 DTO:
@Data
public class ComponentPeriodValues {
private int idComponent;
private String description;
private List<String> periods;
private List<BigDecimal> values;
}
All records have equal numbers of periods and I am trying to build a data table like below:所有记录都有相同数量的周期,我正在尝试构建一个如下所示的数据表:
My HTML is like below:我的 HTML 如下所示:
<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
<form action="#" th:action="@{/planning-components/save}"
th:object="${cpv}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>Description</th>
<th:block th:each="p : ${cpv[0].periods}">
<th th:text="${p}"></th>
</th:block>
</tr>
</thead>
<tbody>
<tr th:each="c : ${cpv}">
<td th:text="${c.description}" />
<th:block th:each="v : ${c.values}">
<td>
<input type="text" th:field="*{v}"
class="form-control mb-4 col-4"/>
</td>
</th:block>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
</div>
But, when I use this input field, I got this error:但是,当我使用这个输入字段时,我得到了这个错误:
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'v' of bean class [java.util.ArrayList]: Bean property 'v' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?原因:org.springframework.beans.NotReadablePropertyException:bean class [java.util.ArrayList] 的无效属性“v”:bean 属性“v”不可读或具有无效的 getter 方法:getter 的返回类型是否匹配设置器的参数类型?
When I use an plain text field it works..当我使用纯文本字段时,它可以工作..
So, my question is: How can I use input fields here to reflect changes in the DTO and then work with my DTO updated in order to process the update into the database?所以,我的问题是:如何在此处使用输入字段来反映 DTO 中的更改,然后使用更新的 DTO 以将更新处理到数据库中?
I don't know much about thyme leaf syntax but I understand how spring-mvc data bind works.我不太了解百里香叶语法,但我了解 spring-mvc 数据绑定的工作原理。 You may need to change few thyme leaf syntax but here is what you need to do -
您可能需要更改一些百里香叶语法,但这是您需要做的 -
Wrap your list of ComponentPeriodValues
in another class like this像这样将您的
ComponentPeriodValues
列表包装在另一个 class 中
@Data
public class ComponentPeriodCommand implements Serializable {
private static final long serialVersionUID = 1L;
private List<ComponentPeriodValues> cpvs;
}
Now to bind values to your list your html needs to look something like this for fist row second column and so on现在要将值绑定到您的列表,您的 html 需要在第一行第二列看起来像这样,依此类推
<input type="text" name="cpvs[0].values[1]" class="form-control mb-4 col-4"/>
According to this baeldung post you can get index in thymeleaf like根据this baeldung post ,您可以在 thymeleaf 中获得索引,例如
<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
<form action="#" th:action="@{/planning-components/save}"
th:object="${command}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>Description</th>
<th:block th:each="p : ${cpv[0].periods}">
<th th:text="${p}"></th>
</th:block>
</tr>
</thead>
<tbody>
<tr th:each="c, iterC : ${command.cpvs}">
<td th:text="${c.description}" />
<th:block th:each="v, iter : ${c.values}">
<td>
<input type="text" th:field="*{cpvs[__${iterC.index}__].values[__${iter.index}__]}" class="form-control mb-4 col-4"/>
</td>
</th:block>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.