Friday, November 18, 2011

Adding multiple times set of adf components from backing bean

This is an requirement I had in my project, I would like to share this.
This requirement I solved with help of Frank. The requirement is as follows,

There are five fields say, Input text, InputDate and so on. And an ADD button.
Initially I am not showing any the components in the page. When I click on ADD button I have to show the pages. Similarly I have to add one more set of pages when I click ADD button. Like this I will add as many as I need.

Let me tell you the steps

1. Create a managed bean that exposes a HashMap property. The HashMap is of type e.g. Map

2. To create a new set of fields, add a new entry to the HashMap and refresh the af:formLayout. The af:formLayout contains and af:forEach that you use
to iterate as often as you have entries in the HashMap. Each text field you bind to #{bean.HashMapProperty['key'].attribute}, for example
#{bean.HashMapProperty['key'].FirstName}
#{bean.HashMapProperty['key'].LastName}
#{bean.HashMapProperty['key'].EmployeeId}


3. When the user updates a field, the HashMap is updated based on the "key"

4. The managed bean should be in viewScope and referenced from the field's value property

5. Finally, like this




5 comments:

  1. Hi Gopinath,

    Thanks for the post.
    I have multiple buttons each leads to different forms. Now, I want to change the labels and components of each form based on the values in the table I have in the database.
    That is, the Labels and Components in the form of your post are 'First Name' and InputText field etc. What I am trying to do is the both will change with respect to the data in the table. ie, for some case, Label will be 'Designation', component will be a Drop down and so on.

    ReplyDelete
  2. Xahar,

    Use the Switcher component to render the components inside the foreach in jspx. use the key in the map as facet for switcher.
    Then you have to add the components say, inputdate,dropdown or whatever it is into the pojo.
    While creating the instance in the bean, add the values to the dropdown and add to the list

    ReplyDelete
  3. Gopinath,

    Will I be able to insert data into the database from each different form I display?
    What all changes I need to make to the above application?

    ReplyDelete
  4. Xahar,

    I used the iterator to get the values in the Map. You have to get the values from the map and put it into the Viewobject and commit it finally,
    You can able to get the values from the page means, you can update it easily to database. I hope you are using ADF BC.

    ReplyDelete