Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values for th:field attributes in checkbox

I have table with datas from database (insert dynamically). In one column I insert checkbox. Now I want to select one of them and send to next form (I select one product and send properties to another form. In this form should be displayed properties only the select product). But I don't know what kind of value insert in th:field="*{}". I tried many solutions but doesn't work. My html form with all products table:

<form action="/oferta/zamow" th:action="@{/oferta/zamow}"
      th:object="${oferta}" method="post">

    <table border="1" id="display-data">
        <tr>
            <td>#</td>
            <td>title</td>
            <td>author</td>
            <td>rok</td>
            <td>cena</td>
            <td></td>
        </tr>
        <tr th:each="produkt, pozycja : ${oferta}">
            <td th:text="${pozycja.count}"></td>
            <td><span th:text="${produkt.tytul}"></span></td>
            <td><span th:text="${produkt.autor}"></span></td>
            <td><span th:text="${produkt.rok}"></span></td>
            <td><span th:text="${produkt.cena}"></span></td>
            <td>
                <input type="submit" value="zamow"/>
                <!-- <a th:href="@{/zamowienie}">zamow</a> -->
            </td>
            <td>
                <label>zamow</label>
                <input type="checkbox" th:field="*{produkt}" th:value="${produkt}"/>
            </td>
        </tr>
    </table>
</form>

Form to display select product:

<form action="/zamowienie/zam" th:action="@{/zamowienie/zam}"
      th:object="${zamowienie}" method="post">

    <table border="1" id="display-data">
        <tr align="center">
            <td colspan="2">twoje zamowienie</td>
        </tr>
        <tr>
            <td>tytul</td>
            <td><span th:text="${produkt.tytul}"></span></td>
        </tr>
        <tr>
            <td>autor</td>
            <td><span th:text="${produkt.autor}"></span></td>
        </tr>
        <tr>
            <td>rok</td>
            <td><span th:text="${produkt.rok}"></span></td>
        </tr>
        <tr>
            <td>cena</td>
            <td><span th:text="${produkt.cena}"></span></td>
        </tr>
        <tr>
            <td>data zlozenia zamowienia</td>
            <td><span th:text="${datazam}"></span></td>
        </tr>
    </table>
</form>

Thanks for help.

like image 740
user978758 Avatar asked Jul 17 '13 06:07

user978758


People also ask

Does checkbox have value attribute?

The value attribute on the checkbox is used when the form is interacting with a server. So when you set the value as lettuce, that means when the form is submitted and that box is checked, the value it sends back to the server is topping=lettuce .


1 Answers

I am not sure if this is the answer you seek, but you can find an example at http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields.

Here is a simple example to illustrate how to use a checkbox in Thymeleaf with Spring MVC.

Controller:

@RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
  List<String> allItems = new ArrayList<String>();
  allItems.add("value1");
  allItems.add("value2");
  allItems.add("value3");
  model.addAttribute("allItems", allItems);

  Foo foo = new Foo();
  List<String> checkedItems = new ArrayList<String>();
  // value1 will be checked by default.
  checkedItems.add("value1");
  foo.setCheckedItems(checkedItems);
  model.addAttribute("foo", foo);

  ...
}

@RequestMapping(value = "/processForm", method=RequestMethod.POST)
public String processForm(@ModelAttribute(value="foo") Foo foo) {
  // Get value of checked item.
  List<String> checkedItems = foo.getCheckedItems();
  ...
}

html:

<form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
  <div th:each="item : ${allItems}">
    <input type="checkbox" th:field="*{checkedItems}" th:value="${item}" />
    <label th:text="${item}">example</label>
  </div>
  <input type="submit" />
</form>

Foo.java:

public class Foo {
  private List<String> checkedItems;

  public List<String> getCheckedItems() {
    return checkedItems;
  }

  public void setCheckedItems(List<String> checkedItems) {
    this.checkedItems = checkedItems;
  }
}

Hope this helps.

like image 170
Shinichi Kai Avatar answered Nov 15 '22 07:11

Shinichi Kai