【问题标题】:How to bind a dynamic child list using Thymeleaf and JQuery?如何使用 Thymeleaf 和 JQuery 绑定动态子列表?
【发布时间】:2021-12-22 19:33:28
【问题描述】:

我是一个完全的新手,我真的希望你能帮助我!

我有两个实体:配方和产品。 recipe 对象包含一个产品列表,大小为 这是由用户“控制”的——它会根据用户想要购买的产品数量而变化 包括。

这是我的配方实体:

@Entity
@Table(name = "recipes")
public class RecipeEntity extends BaseEntity {

    private String name;
    private String shortDescription;
    private String cookingInstructions;
    private Integer cookingTime;
    private Integer portions;
    private List<ProductEntity> products;
    
    //GETTERS, SETTERS

这是产品实体:

@Entity
@Table(name = "products")
public class ProductEntity extends BaseEntity {

    private String name;
    private Double quantity;
    private RecipeEntity recipe;
    
    //GETTERS, SETTERS

这是 Thymeleaf 模板:

 <h4>Products</h4>

 <div class="products" th:each="product, iterStat : *{products}">
   <div class="product row">


     <div class="col-lg-6 mb-3">
       Name
       <input th:field="*{products[__${iterStat.index}__].name}" id="product" class="form-control form-control-lg" />
     </div>

     <div class="col-lg-6 mb-3">
       Quantity(grams)
       <input th:field="*{products[__${iterStat.index}__].quantity}" type="number" id="quantity" step="0.1" min="0" class="form-control form-control-lg" />
     </div>

   </div>
 </div>

 <button type="button" class="add-product btn btn-outline-secondary col-lg-12">+ Add another
   product
 </button>

最后我们有了我编写的 JQuery 函数,用于动态添加产品行:

$(document).ready(function () {
    $('.add-product').click(function () {
        let newRow = jQuery('<div class="product row">' +
            ' <div class="col-lg-6 mb-3">' +
            ' Name' +
            ' <input type="text" class="form-control form-control-lg"/>' +
            ' </div>' +
            ' <div class="col-lg-6 mb-3">' +
            '  Quantity(grams)' +
            ' <input type="number" step="0.1" min="0"' +
            ' class="form-control form-control-lg"/>' +
            '</div>' +
            '</div>');

        $('.products').append(newRow)

现在的问题是我想在发布请求中获取所有产品以及配方 将它们一起保存到数据库中。 JQuery 函数工作正常,它确实附加产品行, 但是,当我将信息发布到我的 RecipeController 时,产品列表为空。

如果有人能告诉我正确的方向,我将非常感激,因为我完全迷失了这个方向!提前谢谢大家!

【问题讨论】:

    标签: jquery spring thymeleaf


    【解决方案1】:

    我已经设法通过更多研究解决了我自己的问题,所以我将在此处发布答案,以防将来有人发现它有用。

    发现这篇文章很有帮助 -How to get dynamic input box data using Thymeleaf?

    thymeleaf 模板并不是我最初认为的问题。 我编写的 JQuery 函数存在问题。

    这是工作的 JQuery 函数,它不仅在客户端追加行,而且还将父对象 Recipe 与子列表 Products 一起传输到我的 RecipeController。

    $(document).ready(function () {
        let max_fields = 30;
        let index = 1;
    
    
        $('.add-product').click(function () {
    
            if (index < max_fields) {
                let newRow = jQuery('<div class="product row">' +
                    ' <div class="col-lg-6 mb-3">' +
                    ' Name' +
                    ' <input name="products[' + index + '].name" type="text" class="form-control form-control-lg"/>' +
                    ' </div>' +
                    ' <div class="col-lg-6 mb-3">' +
                    '  Quantity(grams)\n' +
                    ' <input name="products[' + index + '].quantity" type="number" step="0.1" min="0"' +
                    ' class="form-control form-control-lg"/>' +
                    '</div>' +
                    '</div>');
    
                index++;
    
                $('.products').append(newRow)
            }
    
        });
    });
    

    我要注意的一件事是,包含名称和数量的标签标签可能会更好,但是即使没有它们,代码仍然可以按预期工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2019-04-14
      • 2019-09-23
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多