0

I am not quite sure how to approach this problem. I have a table rendered by datatables populated with product information. I am trying to figure out a form where I can insert the quantity and discount values and then save either the selected fields or fields where the quantity > 0. Then bind the table data to a set or a list.

However, I am not quite sure how to write the bindings. I have read over this question here but it didn't seem quite the same in that I don't think I could use an index as there could a large amount of products and therefore potentially a large amount of nulls in the response. Any help would be appreciated.

Product.java

@Entity
@Table(name="products")
public class Product {
   @Getter
   @Setter
   @Id
   private Long productId;
   @Getter
   @Setter
   private Long productName; 
}

QuoteForm.java

public class QuoteForm {
   @Getter
   @Setter
   private Long quoteId;
   @Getter
   @Setter
   private Long contactId;
   @Getter
   @Setter
   private Set<ProductEntry> products;
 }

ProductEntry.java

public class ProductEntry {
  @Getter
  @Setter
  private TempProduct product;
  @Getter
  @Setter
  private Long quantity;
  @Getter
  @Setter
  private float discount;
}

form.html

<form id="productTable" th:object="${quoteForm}">
<input type="number" th:field="*{contactId}"></input>
<table id ="productList"
          class="table table-striped table-bordered"
          cellspacing="0"
          width="100%">
    <thead>
      <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>Discount</th>
      </tr>
   </thead>
   <tbody>
      <tr th:each="product : ${productData.getProducts()}">
      <td name="?">
          <a th:text="${product.getProductName()}"
                       th:href="@{|/product/records/${product.getProductId()}|}">
          </a>
      </td>
      <td>
          <input type="number" 
                 name="?" 
                 th:field="*{products.quantity}">
           </input>
      </td>
      <td>
         <input type="number" name="?" th:field="*{products.discount}"></input>
     </td>
    </tr>
  </tbody>

M. Deinum
  • 115,695
  • 22
  • 220
  • 224

1 Answers1

0

You can do binding using "model.addAttribute()". Something like this in your controller:

@RequestMapping("/example")
public String formParse(Model model) {
    //some logic

    Set<ProductEntry> productList = //code to get();

    model.addAttribute("productList", productList);

    //some more logic
    return "yourView";
}

Here "productList" is the name which you have assigned to "?". And as for checking the quantity you can do that in the form itself, with some thing like:

<div th:if="*{products.quantity&gt;0}" th:field="*{products.quantity}"></div>

Here &gt represents "greater than". Hope this helps!

ASR4
  • 545
  • 1
  • 8
  • 24