Swap button after JSF click

Asked

Viewed 263 times

0

Talk personal I have the following problem, I have these two buttons that make:

X : Removes the value of a chart + : Adds the value of a chart

Foto1

I need it to look like this: When I click the X button

inserir a descrição da imagem aqui

it switches to the +button as they have different functions as you can notice.

inserir a descrição da imagem aqui

My view is like this currently:

<p:column width="30">
                <p:commandButton id="valorSelect" value="x"
                    actionListener="#{statusView.selecionarView}" update=":novoform:grafico" >
                    <f:attribute name="projetoSelecionadoNaView" value="#{projeto}" />
                </p:commandButton>
                <p:commandButton value="+"
                    actionListener="#{statusView.addView}" update=":novoform:grafico" >
                    <f:attribute name="projetoSelecionadoNaView" value="#{projeto}" />
                </p:commandButton>
</p:column>

1 answer

0


If I understand correctly, you want when the user clicks on a button it disappears and the other one is in place. You can use the 'rendered JSF attribute joined with a boolean expression or variable that changes at the time you wish. I took more or less your code and made an example already inside a table:

 <h:form id="novoform">

        <p:dataTable id="grafico" value="#{trocaBotaoBean.listaTeste}" var="nome" >
            <p:column headerText="Action" >
                <p:commandButton rendered="#{!trocaBotaoBean.adicionandoValor}"
                                 id="valorSelect" value="x" actionListener="#{trocaBotaoBean.adicionar()}"
                                 update=":novoform:grafico" />
                <p:commandButton rendered="#{trocaBotaoBean.adicionandoValor}"
                                 actionListener="#{trocaBotaoBean.retirar()}"
                                 value="+"
                                 update=":novoform:grafico" />
            </p:column>
            <p:column headerText="VALORES">
                <h:outputText value="#{nome}" />
            </p:column>
        </p:dataTable>

    </h:form>

Managedbean:

public class TrocaBotaoBean {

private boolean adicionandoValor;

public TrocaBotaoBean() {

}

public void adicionar() {
    this.adicionandoValor = true;
}

public void retirar() {
    this.adicionandoValor = false;
}

public boolean isAdicionandoValor() {
    return adicionandoValor;
}

public List<String> getListaTeste() {
    List<String> nomes = new ArrayList<>();
    nomes.add("ZEUS");
    nomes.add("THOR");
    nomes.add("RÁ");
    return nomes;
}
}

I hope I’ve helped.

  • For example: If I add a value there will disappear the other button only of that line ? I think he’s gonna disappear all the buttons from all the lines... ?

  • yes, if you want to do this individually, you will need a little more code, for example, adjust the datable, Selection, Selection mode, rowKey, besides, you will need to use a <p:ajax that will capture the line with rowSelect event. these buttons, will not need to repeat themselves, just play them on the footer or header ja q they change only the q was selected. I believe q is that way.

Browser other questions tagged

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