Home > Net >  Thymeleaf looping over list of objects with list of objects attribute problem
Thymeleaf looping over list of objects with list of objects attribute problem

Time:06-25

I'm working in Spring Boot and I have a problem rendering with Thymeleaf a table with different lines. First must be a String, and the subsequent lines must be the data saved in a list of objects.

situation of the problem:

I have a list of objects, this object has two attributes, one is a list of Strings, and the other one is a list of different objects. I don't know how to render in Thymeleaf in a table the first attribute of a string list in a line, and on the next lines of the table render the second list of attribute object.

details of the object:

public class objetosDeServiciosAD {

    private String Servicio;
    private LinkedList<usuarioAD> listaUsuariosAD;

    public String getServicio() {
        return Servicio;
    }
    public void setServicio(String servicio) {
        Servicio = servicio;
    }
    public LinkedList<usuarioAD> getListaUsuariosAD() {
        return listaUsuariosAD;
    }
    public void setListaUsuariosAD(LinkedList<usuarioAD> listaUsuariosAD) {
        this.listaUsuariosAD = listaUsuariosAD;
    }
    @Override
    public String toString() {
        return "objetosDeServiciosAD [Servicio="   Servicio   ", listaUsuariosAD="   listaUsuariosAD   "]";
    }
        
}   

objetos_Servicios is a list of objects with two atributes, one is servicio this object has a second attibute which is a list of objects, this is listaUsuariosAD.

This is my code in Thymeleaf:

<table >
    <thead >
        <tr>
            <th scope="col">Usuario</th>
            <th scope="col">Teléfono</th>
            <th scope="col">mail</th>
            <th scope="col">Descripción</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="servicio : ${objetos_Servicios}">
            <td th:text="${servicio.servicio}"></td>            
        <tr th:each=" listaeusuario : ${servicio.listaUsuariosAD}">
            <tr th:each ="usuarios : ${listaeusuario}">
                <td th:text = "${usuarios.usuario}"></td>
                <td th:text = "${usuarios.telefono}"></td>
                <td th:text = "${usuarios.mail}"></td>
                <td th:text = "${usuarios.descripion}"></td>
                  
            </tr>
        </tr>                            
    </tbody> 
      
</table>

CodePudding user response:

The code will look something like this:

<table >
  <thead >
    <tr>
      <th scope="col">Usuario</th>
      <th scope="col">Teléfono</th>
      <th scope="col">mail</th>
      <th scope="col">Descripción</th>
    </tr>
  </thead>
  
  <tbody>
    <th:block th:each="servicio : ${objetos_Servicios}">
      <tr>
        <td th:text="${servicio.servicio}" />
      </tr>          
      
      <tr th:each = "lista : ${servicio.getListaUsuariosAD()}">
        <td th:text="${lista.usuario}"></td>
        <td th:text="${lista.telefono}"></td>
        <td th:text="${lista.mail}"></td>
        <td th:text="${lista.Descripcion}"></td>
      </tr>
    </th:block>
  </tbody> 
</table>

You can use a th:block tag to loop over a larger block of code (that contains the header <tr /> and the rows <tr />).

CodePudding user response:

I recommend changing the naming standards you are using, so that all your class names begin with an upper-case letter - for example: ObjetosDeServiciosAD instead of objetosDeServiciosAD. This is standard in Java - and not doing this can be confusing for other people who read your code.

So, your class becomes:

import java.util.List;

public class ObjetosDeServiciosAD {

    private String servicio;
    private List<UsuarioAD> listaUsuariosAD;

    public String getServicio() {
        return servicio;
    }

    public void setServicio(String servicio) {
        this.servicio = servicio;
    }

    public List<UsuarioAD> getListaUsuariosAD() {
        return listaUsuariosAD;
    }

    public void setListaUsuariosAD(List<UsuarioAD> listaUsuariosAD) {
        this.listaUsuariosAD = listaUsuariosAD;
    }

}

I also replaced LinkedList with List, since you do not appear to need a linked list here (if you actually do, you can revert that change).

Then, for your Thymeleaf template, you can use Thymeleaf's table that describes what I want to construct

  • Related