0

I want directly push the selected option. But i dont find a solution. Maybe u can help me?

I have a table with a list of entities. And i will offer a select for every entity. But i want to save the selection directly. I need the entry.id and the cat.id.

This is what i have.

<form class="myForms" id="uploadCheckerForm" method="post" th:action="@{uploadStatus}">
  <table>
    <tr th:replace="fragment/form-fragments::checkerMainLine()"></tr>
    <tr th:each="entry : ${uploadList}">
      <td><select th:id="transactionCategory" th:name="transactionCategory">
        <option>Select</option>
        <option th:each="cat : ${transactionCategoryList}"
             th:text="${cat.name}" th:value="${cat.id}">
        </option></select>
      </td>
      <td th:text="${entry.Comment}">
    </tr>
  </table>
</form>

What have i to do, that it directly post this infos?

Thanks for help!

Bianca
  • 21
  • 4

1 Answers1

0

I would append some unique identifier of your entry to the name of your <select> with some sort of separator and then accept form data on backend to a Map. Kinda like this:

HTML

<form class="myForms" id="uploadCheckerForm" method="post" th:action="@{uploadStatus}">
  <table>
    <tr th:replace="fragment/form-fragments::checkerMainLine()"></tr>
    <tr th:each="entry : ${uploadList}">
      <td><select id="transactionCategory" th:name="'transactionCategory_' + ${entry.guid}">
        <option>Select</option>
        <option th:each="cat : ${transactionCategoryList}"
             th:text="${cat.name}" th:value="${cat.id}">
        </option></select>
      </td>
      <td th:text="${entry.Comment}">
    </tr>
  </table>
</form>

Controller

@PostMapping( "/uploadStatus" )
public String uploadStatus( @RequestParam Map<String, Long> params ) {
    for ( Map.Entry<String, Long> entry : params.entrySet() ) {
        if ( entry.getKey().startsWith( "transactionCategory_" ) ) continue; // skip

        Long entryId = Long.parseLong( entry.getKey().replace( "transactionCategory_", "" ) ); // get entry ID

        SomeDAO.instance().uploadStatus( entryId, entry.getValue() ); // do what you need to do with id of entry and chosen category ID
    }

    return "redirect:/";
}
Pavel Polyakoff
  • 191
  • 1
  • 13