Tree Table Primefaces does not work by selecting a Node from Java

Asked

Viewed 217 times

1

I have a p:Tree being automatically loaded, so I need to do a search inside the existing nodes on Tree, I do the search and return the first Node I find and I want to arrow it like Selected, but when I do this via code to Tree to work, I can’t de-select and expand it anymore, everything stops working.

Fazendo a pesquisa

Retorno da pesquisa

My code:

 <m:linha>
                <m:autocomplete size="4" bean="#{controller}" itemLabel="descricaoCompleta" dropdown="true"  minQueryLength="1"
                    value="#{controller.objetoFiltro.tipoObjeto}" method="listarTipoObjeto" label="Tipo de Objeto" maxResults="100"/>
                <m:input value="#{controller.objetoFiltro.nome}" label="Descrição" size="6" />
                <div class="ui-grid-col-2">
                    <br/>
                    <p:commandButton action="#{controller.pesquisar()}" styleClass="btn btn-primary float-left" value="Pesquisar" update="@form"/>
                </div>
            </m:linha>
            <m:linha>
                <div class="ui-grid-col-12">
                    <p:tree id="treeObjetos" value="#{controller.mapeamentos}" var="objeto" style="width: 100%" 
                    selectionMode="single" selection="#{controller.objetoSelecionado}" animate="true">
                        <p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed">
                            <h:outputText value="#{objeto.nome}"/>
                        </p:treeNode>
                    </p:tree>
                </div>
            </m:linha>

Managedbean:

private Objeto objetoFiltro;

private TreeNode mapeamentos;

private TreeNode objetoSelecionado;

@PostConstruct
private void init() {
    List<Objeto> objetosRoot = objetoDAO.pesquisarObjetosPais();
    mapeamentos = new DefaultTreeNode();
    for (Objeto objeto : objetosRoot) {
        TreeNode filho = new DefaultTreeNode(objeto, mapeamentos);
        criarNode(objeto, filho);
    }
}

private void criarNode(Objeto objeto, TreeNode parentNode) {
    List<Objeto> objetos = objetoDAO.recuperarObjetosFilhos(objeto);
    for (Objeto item : objetos) {
        TreeNode filho = new DefaultTreeNode(item, parentNode);
        criarNode(item, filho);
    }
}

public void pesquisar() {
    TreeNode node = objetoPesquisado();
    node.setSelectable(true);
    node.setSelected(true);
}

public TreeNode objetoPesquisado() {
    TreeNode retorno = null;

    for (TreeNode item : mapeamentos.getChildren()) {
        if (objetoFiltro.getNome() != null && !objetoFiltro.getNome().isEmpty()) {
            if (((Objeto) item.getData()).getNome().contains(objetoFiltro.getNome())) {
                return item;
            }
        }
    }

    return retorno;
}
  • Have you verified the suggestions of this question? http://stackoverflow.com/questions/10597134/primefaces-tree-component-setting-selected-node-from-managed-bean

  • Dude, I managed to solve, taking out the ajax and recharging the screen again, thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.