You need to sign in to do that
Don't have an account?
Zach Gavin Delacroix
Working with Collection (List) in VisualForce
Hi Experts,
I'm trying to learn VisualForce and it seems that I'm stuck with using Collection.
I have <apex:inputField> and <apex:commandButton>, what I want to do is add the value I type in the inputField when I click the commandButton.
Can anyone please give me an Idea of the correct syntax?
Here's the code I created so far that is not working as expected.
When I run Click the button, the result goes like below.
09:41:52:039 USER_DEBUG [13]|DEBUG|A
09:41:52:039 USER_DEBUG [13]|DEBUG|B
The result I want should look like below because I typed "A" first then pressed the button, then Typed B then pressed the button. So Everytime I type value in the Text Field, I want to add it as a value in my List.
09:41:52:039 USER_DEBUG [13]|DEBUG|A
09:41:52:039 USER_DEBUG [13]|DEBUG|B
NOTE: I'm using Account for testing, when I have this working, I'll try to work on inserting the Records from the List of Accounts (But don't answer this part coz I want to figure it our my self first :D )
Thanks for the help!
I'm trying to learn VisualForce and it seems that I'm stuck with using Collection.
I have <apex:inputField> and <apex:commandButton>, what I want to do is add the value I type in the inputField when I click the commandButton.
Can anyone please give me an Idea of the correct syntax?
Here's the code I created so far that is not working as expected.
<apex:page controller="MultiAccountInsertController" sidebar="false"> <apex:pageBlock title="Enter Account Name"> <apex:form > <apex:inputText value="{!acc.name}"/> <apex:commandButton value="Add Account" action="{!addAccount}"/> </apex:form> </apex:pageBlock> <apex:pageBlock title="List of Accounts to Save"> To be continued... </apex:pageBlock> </apex:page>CLASS
public class MultiAccountInsertController { Public account acc{get;set;} List<account> AccountList; public MultiAccountInsertController(){ acc = new account(); AccountList = new List<account>(); } public void addAccount(){ AccountList.add(acc); //Check value of the AccountList in Debug for(integer i=0;i<AccountList.size();i++){ system.debug(AccountList[i].name); } } public List<account> getViewAccounts(){ return AccountList; } }
When I run Click the button, the result goes like below.
09:41:52:039 USER_DEBUG [13]|DEBUG|A
09:41:52:039 USER_DEBUG [13]|DEBUG|B
The result I want should look like below because I typed "A" first then pressed the button, then Typed B then pressed the button. So Everytime I type value in the Text Field, I want to add it as a value in my List.
09:41:52:039 USER_DEBUG [13]|DEBUG|A
09:41:52:039 USER_DEBUG [13]|DEBUG|B
NOTE: I'm using Account for testing, when I have this working, I'll try to work on inserting the Records from the List of Accounts (But don't answer this part coz I want to figure it our my self first :D )
Thanks for the help!
Page
Controller
All Answers
Page
Controller
That worked as expected.
Just one more question though, am I doing the correct approach? or is there any other way to do it?
Can you give me advice what to read if I'm going for these kinds of pages?
Thanks
Page Updated the command button to re-render the whole page. Instead of having the page "reload" This give you more control.
Controller I've simplified the controller a little bit, reformatted it a little bit and then "reset" the account after it's been added to make it so the form is ready to be used.
Appreciate it!