My item is not updated

0

I have a "panelGroup" with a render that depends on a boolean variable, and a button that changes the state of that boolean, but when you click on the button, the panelgroup is not updated until you reload the page despite that I DO have an update.

xhtml:

<h:form >
    <p:commandButton class="btn" value="mostrar foo"
        action="#{developerController.foo}" update="foo"/>

    <h:panelGroup id="foo" rendered="#{developerController.bar}">
        foo
    </h:panelGroup>
</h:form>

My bean:

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named(value = "developerController")
@SessionScoped
public class DeveloperController implements Serializable {

private boolean bar;


public DeveloperController() {

}

public void foo() {
    bar=!bar;
}

public boolean isBar() {
    return bar;
}

public void setBar(boolean bar) {
    this.bar = bar;
}
}

What should I do to update it?

    
asked by gibran alexis moreno zuñiga 24.01.2017 в 20:09
source

4 answers

3

As Lithorel indicates, the rendered = false is at fault for not updating. Having said that, other options besides the one he proposes:

Option 1:

update="@form"

The handicap is that the whole form is updated.

Option 2:

<h:form>
    <p:commandButton class="btn" value="mostrar foo"
        action="#{developerController.foo}" update="miPanel"/>

    <p:outputPanel id="miPanel">
        <h:panelGroup id="foo" rendered="#{developerController.bar}">
            foo
        </h:panelGroup>
    </p:outputPanel>
</h:form>

The miPanel element is always painted and therefore you can update it.

The CSS option is also good, it's a matter of taste.

    
answered by 25.01.2017 / 09:57
source
2

The renderer makes your panelGroup not go in the html so the update does not work since the element id = foo does not exist.

One solution is to use display: none / display: block in the style and make the selection with the bean conditional.

<h:form >
    <p:commandButton class="btn" value="mostrar foo"
        action="#{developerController.foo}" update="foo"/>

    <h:panelGroup id="foo" style="#{developerController.bar ? 'display:block;' : 'display:none'}">
        foo
    </h:panelGroup>
</h:form>
    
answered by 25.01.2017 в 09:28
1

When you want to update the attribute rendered of an element, the update must be made to the parent element:

<h:form id="frmFoo">
    <p:commandButton class="btn" value="mostrar foo"
        action="#{developerController.foo}" update="frmFoo"/>

    <h:panelGroup id="foo" rendered="#{developerController.bar}">
        foo
    </h:panelGroup>
</h:form>

With the rendered in false , the element does not exist, so you can not find it when doing update .

    
answered by 25.01.2017 в 16:20
1

Very good options already mentioned, I guess that what you are developing goes beyond a simple foo.

You can also use styleClass to update and assign it only to the items you want to update to avoid using as many containers and longer references; When your code grows considerably you will see the practicality of this technique.

Look at the following example how we only update an item, no matter how nested it is.

<h:form >

    <p:commandButton class="btn" value="Calcular Neto" action="#{beanController.calcular}" 
      update="@(.neto)"/>

    <h:panelGroup id="contenedorPrincipal" >


         <h:panelGroup....

               <h:panelGroup......

                    <h:outputText value="Neto: "/>
                    <h:outputText styleClass="neto" value="#{bean.neto}" />

               </h:panelGroup>

          </h:panelGroup>

    </h:panelGroup>

</h:form>

Here you can find a guide with this and other techniques that will make it easier to locate your components:

link

Greetings!

    
answered by 26.01.2017 в 00:18