【问题标题】:Thymeleaf: how to update existing database rows without creating new set of rows?Thymeleaf:如何在不创建新行集的情况下更新现有数据库行?
【发布时间】:2018-02-19 14:40:21
【问题描述】:

在我使用 spring 和 thyme-leaf 执行的 Web 应用程序中,我的数据库中有一个表,我需要由用户更新它。在我的表格的行下方找到。

<table class="table table-responsive">
    <thead>
    <tr>
        <th>From <a href="#"><i class="fa fa-sort-amount-asc"
                                aria-hidden="true"></i></a></th>
        <th>To <a href="#"><i class="fa fa-sort-amount-asc" aria-hidden="true"></i></a>
        </th>
        <th>th1(.min)</th>
        <th>th2</th>
        <th>th3</th>
        <th>th4 &#10095;</th>
        <th>th5</th>
        <th>th6</th>
        <th>th7</th>
        <th class="action">&nbsp;</th>
    </tr>
    </thead>
    <tbody>

    <tr th:each="ir, rowStat : *{invRows}">
        <input type="hidden"
               th:field="*{invRows[__${rowStat.index}__].fromTime}"/>
        <input type="hidden" th:field="*{invRows[__${rowStat.index}__].toTime}"/>
        <td><p th:text="${#dates.format(ir.fromTime,  'HH:mm')}"/></td>
        <td><p th:text="${#dates.format(ir.toTime,  'HH:mm')}"/></td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].tvcDur}"/></td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].priceR[0].price}"/>
        </td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].priceR[1].price}"/>
        </td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].priceR[2].price}"/>
        </td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].lgPrice}"/></td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].crPrice}"/></td>
        <td><input type="text"
                   th:field="*{invRows[__${rowStat.index}__].crSpots}"/></td>
        <td class="actions"></td>
    </tr>
    </tbody>
</table>  

参数与mysql数据库绑定成功。但问题是每次保存表时都会一次又一次地添加整组行,而不是更新行的现有值。如何更新表格?

编辑: 我有以下下拉列表。对于每个选择,我都想要一张桌子。即我必须维护 3 张桌子。如何做到这一点?

<select class="btn btn-default dropdown-toggle" data-toggle="dropdown"
        th:value = "${inventory.id}" id="channel" name="channel">
    <option th:value="Channel">Channel</option>
    <option th:value="ch1">ch1</option>
    <option th:value="ch2">ch2</option>
    <option th:value="ch3">ch3</option>
</select>

【问题讨论】:

  • 您应该在表格中添加元素“ir”的 id 作为隐藏字段。像这样
  • 这里的“id”是什么?

标签: java html mysql spring thymeleaf


【解决方案1】:

在您的表中,您需要添加对象的唯一标识符,然后当列表进入您的控制器时,存储库将知道是更新而不是创建。

<input type="hidden" th:field="*{invRows[__${rowStat.index}__].id}"/>

【讨论】:

  • 我应该使用什么广告'id'?
  • 对于列表 invRows 的每个元素,您应该有一个唯一的标识符,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2018-06-20
  • 2019-12-20
相关资源
最近更新 更多