You need to sign in to do that
Don't have an account?
Franco
Selecting Accounts with inputcheckbox
I'm trying to create a page where a list of all accounts are presented to the user so he/she can select accounts to be displayed on the bottom part of the page. I'm trying to display inputcheckboxes next to each account to to capture the user's request. For some reason, I can't get the initial list of accounts to display.
My code is below -
Controller -
My code is below -
Code:
<apex:page Controller="AccountSelectController"> <apex:form > <apex:pageBlock title="Available Accounts"> <apex:pageBlockTable title="Accounts" id="PickAccts" value="{!MyAcct}" var="aAcct" > <apex:column> <apex:inputCheckBox value="{!aAcct.selected}" Id="selectLine"/> </apex:column> <apex:column headerValue="Account Id" value="{!aAcct.theacct.Id}" /> <apex:column headerValue="Account Name" value="{!aAcct.theacct.Name}" /> </apex:PageblockTable> <apex:commandButton action="{!gensAcct}" value="Select" rerender="MyPanel" status="status"/> <br /> <apex:pageMessages ></apex:pageMessages> </apex:pageBlock> <apex:actionStatus startText="Updating..." id="status"/> <apex:outputpanel title="Results" id="MyPanel"> <apex:outputpanel title="Wrapper" id="Wrapper" rendered="{!NOT(ISNULL(SAcct))}"> <apex:pageBlock > <apex:pageBlockTable title="Accounts" id="PickAccts" value="{!SAcct}" var="aAcct" > <apex:column headerValue="Account Id" value="{!aAcct.theacct.Id}" /> <apex:column headerValue="Account Name" value="{!aAcct.theacct.Name}" /> </apex:PageblockTable> </apex:Pageblock> </apex:outputpanel> </apex:outputpanel> </apex:form> </apex:page>
Controller -
Code:
public class AccountSelectController { public PageReference genSAcct() { SAcct = getSAcct(); return null; } public class cAcct{ public Account theacct {get; set;} public Boolean selected{get; set;} public cAcct(Account a,Boolean s){ theacct = a; selected = s; } } //A collection of the class/wrapper objects cAcct //The fist list includes all Accounts public List<cAcct> MyAcct {get; set;} //The second list only inclues Accouts selected public List<cAcct> SAcct {get; set;} //This method uses a SOQL query to return a list of all Accounts public List<cAcct> getMyAcct(){ if(MyAcct == null){ MyAcct = new List<cAcct>(); for(Account acct : [Select Name FROM Account Limit 10 ]){ /* As each opportunity is processed I create a new cOpportunity object and add it to MyList */ cAcct myadd = new cAcct(acct,FALSE); MyAcct.add(myadd); } } return MyAcct; } //This method uses looks at the full account list and adds only those with "selected" = true public List<cAcct> getSAcct(){ if (MyAcct != null) { if (SAcct == null) { SAcct = new List<cAcct>(); for(cAcct cacct : MyAcct){ /* As each Accout is processed, check if the selected value is true. */ if (cacct.selected == TRUE) { SAcct.add(cacct); } } } return SAcct; } else { SAcct = null; return SAcct; } } }
What you have done is basically defined two get methods and the get method which query the account is never being executed.
I have found that it is best to use apex properties {get; set;} for primitive variables such as strings, booleans, numbers etc.
Message Edited by TehNrd on 01-19-2009 09:01 AM
Perfect! Worked like a charm.
Franco
Question...how is an account marked as selected when the user clicks the checkbox, i.e., where does the aAcct.selected property get set to true? I am trying to do something similar but cannot get it to work.
Thanks!
- Carolyn
My Apex page
But I do not know why value is coming as null in what is that I am doing wrong: