1

i have a requirement like i need to show/hide datatable upon button click . I tried implementing it , but its not working . below is the code .

please let me know if we can do with ajax . It is possible only if i set ajax to false .

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>hello world</title>
</h:head>


 <h:body>
<h:form>

    <p:outputPanel id="panel" rendered="#{bye1.showtable}">
        <p:dataTable value="#{bye1.carmodel}" var="cartypes">

            <p:column headerText="Model">
                <h:outputText value="#{cartypes.carname}">
                </h:outputText>
            </p:column>

            <p:column headerText="Location">
                <h:outputText value="#{cartypes.location}"></h:outputText>
            </p:column>

            <p:column headerText="Price">
                <h:outputText value="#{cartypes.rate}"></h:outputText>
            </p:column>

        </p:dataTable>
    </p:outputPanel>
    <p:commandButton value="show" action="#{bye1.enabletable}" update="panel">
        </p:commandButton>
</h:form>
</h:body>
</html>


package lifecycle;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;


public class bye {

private String output;

private Boolean showtable;

private List<cars> carmodel;

public List<cars> getCarmodel() {
    System.out.println("cars populated...........");
    return carmodel;
}

@PostConstruct
public void bye1() {
    System.out.println("constructor called");
    carmodel = new ArrayList<cars>();
    output = "hai";
    carmodel.add(new cars("ford","chennai","4 laks"));
    carmodel.add(new cars("AUDI","chennai","44 laks"));
}

public String getOutput() {
    return output;
}

public Boolean getShowtable() {
    return showtable;
}

public String enabletable() {
    showtable = true;
    return "";
}

}

Any help ?

Thanks in Advance

murthi
  • 175
  • 1
  • 4
  • 18

3 Answers3

0

Use an actionListener on your commandbutton instead of action.

"A return value of an empty string or the same view ID will also return to the same page, but recreate the view scope and thus destroy any currently active view scoped beans and, if applicable, recreate them:" Differences between action and actionListener

Also consider the use of Boolean vs boolean.

It also seems like your class is missing the @ManagedBean and scope annotations?

Community
  • 1
  • 1
Emil Kaminski
  • 1,886
  • 2
  • 16
  • 26
  • Thanks for ur comment Emil . I tried as you said , then again the same problem , its not getting displayed . changed Boolean to boolean , used actionlistener – murthi Nov 11 '14 at 09:24
  • is the method getting called? also what is bye1? isin't your bean called bye? – Emil Kaminski Nov 11 '14 at 09:26
  • ya Emil , its getting called , i tried putting a sysout in the datatable getters and in showtable boolean variable . they are getting printed , once when i click the button . – murthi Nov 11 '14 at 09:34
0

I think the update="panel" is not working properly, and the panel component is not getting updated. When you disable ajax, the whole page gets update, and maybe this is why the update works only with ajax="false".

Can you try this and tell me if it works now ? :

<p:commandButton value="show" actionListener="#{bye1.enabletable}" ajax="true" update=":#{p:component('panel')}">
</p:commandButton>
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • thanks for ur response yanni, update="panel" is working and the component's getter gets called also (i checked with a sysout). i tried the solution that you gave too . the probelm is , in both the cases the getter of the datatable list gets called but the datatable is not getting displayed – murthi Nov 11 '14 at 09:44
  • What is the scope of your 'bye1' Class ? can you also provide the code from method `enabletable` and the declaration + getter+ setter of 'showtable' ? Have you tried debuggin on getShowtable() and check if it's returning the right value? – yannicuLar Nov 11 '14 at 10:16
  • bye1 bean is session scoped . bean is in the question itself , you can scroll it down . the values are getting printed yanni upon the button click . but the table is not visible – murthi Nov 11 '14 at 10:24
  • again, can you check what 'getShowtable()' is a)called from 'rendered=..' b) what is it returning? Could it be it's always returning false? – yannicuLar Nov 11 '14 at 12:20
  • hi yanni , im getting the following output upon clicking the commandbutton : showtable = true showtable = true showtable = true showtable = true carname =ford location :chennai rate :4 laks carname =AUDI location :chennai rate :44 laks – murthi Nov 11 '14 at 12:26
  • looks like it's returning the correct value. If you hit the button, then refresh your page (from browser), does the panel get rendered ? – yannicuLar Nov 11 '14 at 12:45
0

i found this solution .

i did a change like this

<p:outputPanel id="panel" >
        <p:dataTable value="#{bye1.carmodel}" var="cartypes"
            rendered="#{bye1.showtable}">
     ..............
       </p:dataatble>
 </p:outputPanel>

set the rendered attribute to the datatable rather to the outputpanel .

murthi
  • 175
  • 1
  • 4
  • 18