ajax - h:dataTable content displayed on .jsf page load,instead of commandLink click only -
looks simple,but doesn't work)
when .jsf page loaded ,values db shown.
dao impl chunk:
public list<product> getproducts() { return gethibernatetemplate().find("from product"); }
managed bean chunk:
public list<product> getproducts() { return this.getproductmanager().getproducts(); }
i intented,that f:ajax
work click only:
<h:form> <h:commandlink value="show" action="nothing"> <f:ajax render="pr"/> </h:commandlink> <h:datatable var="product" id="pr" value="#{showproducts.products}"> <h:column>#{product.name}</h:column> </h:datatable> </h:form>
and data visible on page,when it's loaded. firebug can see,that data refreshed click, ajax it's work.
do need additional attributes h:datatable
element table content displayed on click only?
thank you.
you need hide datatable on initial request , let commandlink toggle boolean datatable's rendered
attribute depending on.
facelets:
<h:form> <h:commandlink value="show" action="#{showproducts.toggleshow}"> <f:ajax render="products"/> </h:commandlink> <h:panelgroup id="products"> <h:datatable var="product" value="#{showproducts.products}" rendered="#{!showproducts.show}"> <h:column>#{product.name}</h:column> </h:datatable> </h:panelgroup> </h:form>
bean:
private boolean show; public void toggleshow() { show = !show; // or show = true; } public boolean isshow() { return show; }
that said, it's not best practice run expensive business/database logic inside getter. getter can called more once in bean's life. rather job in bean's constructor or @postconstruct
method.
private list<product> products; @postconstruct public void init() { products = this.getproductmanager().getproducts(); } public list<product> getproducts() { return products; }
or if needs lazily loaded, rather so:
private list<product> products; public list<product> getproducts() { if (products == null) { products = this.getproductmanager().getproducts(); } return products; }
Comments
Post a Comment