Kleber Mota Kleber Mota - 1 month ago 10
Javascript Question

Iterating multiple items of diferent sizes in JSTL

My JSP page is receiving two lists: One is the list of all autorizations available and the other is the autorizations availables for one specific user. I want create a list of checkbox where all the autorizations are listed, but only the autorizations of the user are checked. Someone can point me one direction for do this?

UPDATE 1

this is the html/jstl code to display ALL the autorizations:

<div class="row" id="autorizacoes ${item.id}">
<c:forEach var="item_auth" items="${autorizacoes}">
<input type="checkbox" name="${item_auth.nome}" onclick="mudaAutorizacao(${item_auth.nome})">${item_auth.nome}
</c:forEach>
</div>


the page where this code is included is invoked by this method (placed in my controller):

@RequestMapping("/listagem_usuario")
public ModelAndView listagem_usuario()
{
UsuarioDAO lista = new UsuarioDAO();
DadosDAO dados = new DadosDAO();
TipoDAO tipo = new TipoDAO();
AutorizacaoDAO auth = new AutorizacaoDAO();

ModelAndView mav = new ModelAndView();
mav.setViewName("listagem_usuario");
mav.addObject("usuarios", lista.lista());
mav.addObject("campos", dados.getListaCampos());
mav.addObject("tipos", tipo.getListaTipos());
mav.addObject("autorizacoes", auth.getListaAutorizacoes());

return mav;
}


that is the method from AutorizationDAO which read the autorizations from the database:

public List<Autorizacao_usuario> getListaAutorizacoes()
{
List<Autorizacao_usuario> lista = new ArrayList<Autorizacao_usuario>();

conecta();

if(executa("SELECT id, nome, descricao FROM autorizacoes;")) {
do{
Autorizacao_usuario auth = new Autorizacao_usuario(getInt(1), getString(2), getString(3));
lista.add(auth);
}while(proximo());
}

return lista;
}


the autorizations of each user are available from this
List<Autorizacao_usuario> getAutorizacoes()
(of the class com.HorarioLivre.core.Usuario):

and, finally, this is the POJO class that keeps the autorization data in memory:

package com.HorarioLivre.core;

public class Autorizacao_usuario {
private int Id;

private String nome;

private String descricao;

public Autorizacao_usuario()
{
this.setId(0);
this.setNome(null);
this.setDescricao(null);
}

public Autorizacao_usuario(int id, String nome, String descricao)
{
this.setId(id);
this.setNome(nome);
this.setDescricao(descricao);
}

public int getId() {
return Id;
}

public void setId(int id) {
Id = id;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getDescricao() {
return descricao;
}

public String setDescricao(String descricao) {
this.descricao = descricao;
return descricao;
}
}

Answer

try something like this:

<div class="row" id="autorizacoes ${item.id}">
        <c:forEach var="item_auth" items="${autorizacoes}">
            <c:set var="isChecked" value="${false}"/>
            <c:forEach var="user_auth" items="${userAutorizacoes}">
                <c:if test="${user_auth.id == item_auth.id}">  <!-- here compare according ID or any unique attribute -->
                    <c:set var="isChecked" value="${true}"/>
                </c:if>
            </c:forEach>
            <input type="checkbox" name="${item_auth.nome}" onclick="mudaAutorizacao(${item_auth.nome})"  <c:if test="${isChecked}">checked="checked"</c:if> />${item_auth.nome}
        </c:forEach>
    </div>

It checks for each item_auth if it equals to autorizations of the user and if so it sets checked attribute in the checkbox element.