Saturday, 15 January 2011

primefaces - JSF Reset a property of ViewScoped bean back to initial value after render -



primefaces - JSF Reset a property of ViewScoped bean back to initial value after render -

i have viewscoped managedbean. bean has boolean attribute controls whether datatable should displayed. see below:

<p:datatable value="#{loc.locationrows}" var="obj" ... rendered="#{loc.renderlocationtable}"> <p:column> ... </p:column> ... </p:datatable>

my managedbean looks this:

@managedbean(name = "loc") @viewscoped public class locationcontroller implements serializable { private boolean renderlocationtable = false; // jsf actionlistener. public void methoda() { if(somecondition) { renderlocationtable = true; // time should render location table } } }

as methoda() gets called , status met, table should rendered; , works fine. but, problem this, each , every other jsf actionlistener method gets called, have explicitly set rendered boolean false. see below:

@managedbean(name = "loc") @viewscoped public class locationcontroller implements serializable { private boolean renderlocationtable = false; // jsf actionlistener. public void methoda() { if(somecondition) { renderlocationtable = true; // time should render location table } } // jsf actionlistener. public void methodb() { renderlocationtable = false; } // jsf actionlistener. public void methodc() { renderlocationtable = false; } }

i've given little snippet of actual managedbean , xhtml file. in-reality, these files huge , lot's of stuff happening several other boolean "rendered" flags. becoming increasingly hard maintain these flags accurate. plus, each actionlistener method has know boolean flags if not related business at-hand.

this i'd love able do:

<f:event type="postrenderview" listener="#{loc.resetrenderlocationtable}" /> <p:datatable value="#{loc.locationrows}" var="obj" ... rendered="#{loc.renderlocationtable}"> <p:column> ... </p:column> ... </p:datatable>

then, in managedbean have method:

public void resetrenderlocationtable(componentsystemevent event) { renderlocationtable = false; }

wouldn't nice? no more playing games resetting boolean variables. no more test cases need create sure table doesn't displayed when shouldn't be. rendered flag can set true when appropriate jsf actionlistener method sets true , "post-back" phone call reset flag false...perfect. but, apparently there's no way of doing out-of-the-box jsf.

so, have solution issue?

thanks!

by way, situation happens lot more think. anytime have form several commandbuttons using actionlisteners, situation happen you. if you've ever had jsf managedbean , find setting boolean flags true or false scattered through-out class, situation applies you.

you didn't added primefaces tag, according code see using primefaces. suppose methoda() called from, illustration p:commandbutton. suggest first create primefaces remote command:

<p:remotecommand name="resetrenderlocationtable"> <f:setpropertyactionlistener value="#{false}" target="#{loc.renderlocationtable}"/> </p:remotecommand>

this create javascript function named resetrenderlocationtable phone call generate ajax request set renderlocationtable property false. add together phone call function in oncomplete of commandbutton (or other ajax source):

<p:commandbutton action="#{loc.methoda()}" update="mydatatable" oncomplete="resetrenderlocationtable()"/>

in next request don't have worry resetting property, update datatable.

jsf primefaces reset

No comments:

Post a Comment