This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

javax.faces.model.ArrayDataModel does not provide a remove method.  RSS feed

 
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just notice ArrayDataModel does not have remove method. So, what should we do if we want to remove elements in ArrayDataModel?
This is an example of the Core JSF :


If we have and we wrap it in ArrayDataModel like this:


How can we remove a name from the data model since there is no remove method in ArrayDataModel?

 
Bartender
Posts: 19726
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF DataModels are not collections, they are wrappers for collections.

If you alter a collection wrapped by a DataModel, the updated collection will be displayed the next time the page is rendered. You don't have to do anything in JSF.

On the other hand, a Java Array does not permit data cells to be added or removed, only replaced. So you'd have to create a new Array without the removed items in it. Then you'd have to use the ArrayDataModel's setWrappedData() method to point to the new Array.

Or, alternatively, you could simply create a new ArrayDataModel with the new Array in its constructor. Either way the results are the same.
 
Himai Minh
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
Thanks for your explanation.
I modify the chapter 6 delete example in Core JSF.


In index.xhtml, we can click on the Delete link to delete one name at a time

 
Tim Holloway
Bartender
Posts: 19726
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is bad:


you should not be coding "function calls" in EL. The whole point of a DataModel is to allow you do do what you need to do  using only a function reference:


This references a JSF Action Method. Note that Action Methods take no parameters because this is a Model/View/Controller architecture and the Model (tableData bean) was updated before the Action Method fired.

Here's what the doDeleteName Action Method looks like:


Apologies for any bugs I might have introduced.

In truth, stuff like this is easier with an ArrayList, since you can just do a remove(deleteRow) and drop about 90% of the other logic - no need to copy or to create/update the DataModel. But whatever amuses you.
 
Himai Minh
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim. Thanks for your suggestion.
I need to convert the Name[] into ArrayList<Name> in order to remove the selected index.
The code is:

The index.xhtml:
 
Tim Holloway
Bartender
Posts: 19726
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's more work than necessary.

 
Himai Minh
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim.
Thanks for your suggestion. I just simply remove the index from the array without recreating a new ArrayList. It works on my side.


The reason why I don't use nameDataModel to remove because ListDataModel class does not have remove method.
 
Tim Holloway
Bartender
Posts: 19726
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DataModels don't have remove or add methods because they are wrappers for the actual data. In this case, that data is the ArrayList.

So it's quite correct to remove the row from the list.

The only fault I can give on your latest attempt is that your action method returns "void". JSF seems to accept this, but properly, it should return a navigation string or null. Because the action method's result is how you navigate from View to View in JSF.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!