Dependent dropdown list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Dependent dropdown list

malvitor
Hi everyone,

I want to know if is possible create dependent a dropdown using only thymeleaf and spring, if it's possible, how can i do this?
It's the code that i have for while:
   <label for="trabalhador">trabalhador:</label>
   <select th:field="*{trabalhador}">
   <option th:each="trabalhador : ${trabalhadorList}"
    th:value="${trabalhador.getId()}"
    th:text="${trabalhador.getNmTrab()}"></option>
   </select>
   
   trabalhador is required
 </div>
 <div class="form-group">
   <label for="cargo">Cargo:</label>
   <select th:field="*{cargo}">
   <option th:each="cargo : ${cargoList}"
    th:value="${cargo.getId()}"
    th:text="${cargo.getNmCargo()}"></option>
   </select>
   
   cargo is required
 </div>
  <div class="form-group">
   <label for="lotacao">lotacao:</label>
   <select th:field="*{lotacao}">
   <option th:each="lotacao : ${lotacaoList}"
    th:value="${lotacao.getId()}"
    th:text="${lotacao.getCodLotacao()}"></option>
   </select>
   
   lotacao is required
 </div>
 <div class="form-group">
   <label for="empregador">empregador:</label>
   <select th:field="*{empregador}">
      <option th:each="empregador : ${empregadorList}"
    th:value="${empregador.getId()}"
    th:text="${empregador.getNmRazao()}"></option>
   </select>
   
   empregador is required
 </div>

So trabalhador dropdown is dependent of empregador dropdown and cargo and lotacao are dependent of trabalhador and empregador. Sorry for the bad english.
Reply | Threaded
Open this post in threaded view
|

Re: Dependent dropdown list

Zemi
Administrator
Hello,

being Thymeleaf a server-side technology, you can achieve that behaviour as long as you submit the data to the server every time the user changes the value of a select.

If you are not thinking on that possibility, maybe you should aim for a Javascript-based solution.

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Dependent dropdown list

malvitor
Hi Zemi,

It's a possibility, and how can I send the data to the server when the data is changed? Exist some tag like th:change, th:valuechanged or it must to be done with html like action="somefuncion"? I have some doubts about this components and how to make a function or event, and I'm lost where I can find this information because the documentation is a little confuse for me.Thanks for reply.

João Victor
Reply | Threaded
Open this post in threaded view
|

Re: Dependent dropdown list

Zemi
Administrator
Hi,

I don't know if you have previous experience with some component-based technology, like JSF. Thymeleaf is not a component technology but a template technology like JSP. So there is not components or built-in mechanism in Thymeleaf to do client-server communication.

You will have to program that communication using plain old HTML forms or using AJAX calls.

The full application examples
   https://github.com/thymeleaf/thymeleafexamples-gtvg
   https://github.com/thymeleaf/thymeleafexamples-petclinic
could be a good start to see how to do it.

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Dependent dropdown list

João Victor
I don't have expereince with this technologies, I was working with desktop development. Ok, I will take a look, thanks for the help.

João Victor