Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a List or Array of Child Elements To Feed To a Java Bean  RSS feed

 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project I am currently working on using Primefaces and MySQL. I have a page that will display information queried from the database. I have textfields that will display the data from a given record. But I want to ensure that the displayed data cannot be altered. I decided on disabling the textfields unless the create or edit button were clicked. At which point all textfields will be enabled for editing, and cleared if create button was clicked. I want to push the disabling/enabling of the elements back to a managed bean, but I want to have the bean take in two lists or arrays, one containing all elements by ID and one containing a list of exempt elements. Is there a way I can create a List or Array of the ID's that I can send to the bean? I know little in Javascript and previous attempts to implement it have met with little success.

A short example of the code:



I want to create a list that would contain all and another that would contain the exemptions such as searchField, searchCriteria, searchButton, projectList and viewButton. The actual exemption list is far smaller than the list of all children. I have found sources that can help me disable the elements with a java bean, but I have not been able to find one that would allow me to define those that should and should not be disabled, then vice versa for enabling.


Thank you for any help that you can offer.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you may really need is a dataTable instead of building up a table by brute-force primitives, but I'm not sure since I'm too lazy to spend time deciphering the minutiae.

JavaScript is just a mess for this kind of this, however, dataTable or not. Use the "readOnly" attribute on the input controls, define the on/off values as properties in the backing bean, and use AJAX to toggle them and update the display (or simply use a commandButton and re-render the whole page).
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I can properly articulate my problem and desired result. I have not been able to as far as Google is concerned.

I considered a dataTable when I started working on this page, but it does not seem to suit my needs. The layout without any styling currently has been designed like:



Link: http://i.imgur.com/4xmnDcq.png just in case the image is hard to see.


Currently I just have the elements readonly attribute referencing back to a String in the bean.

For instance:



Here is the bean:




Then say when a commandButton such as my edit button is clicked I want it to change readonly to false and render those components again. I've tried to change the readonly String in the bean to false and have it render the components again with:





I'm not sure this would be a good way of doing it, even if it were working. But I am not sure how to go about addressing the issue.

I know there is plenty I don't understand and am still learning, but with what tutorials I have gone through, I haven't found how to address this.


Thank you for any help you can offer.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only benefit from using a dataTable when you have a set of homogeneous rows such as a set of records returned from a database. That is, you would be iterating over a collection of rows. For heterogeneous output in a 2-dimensional layout, a panelGrid is more appropriate.

Please note that an action attribute is supposed to reference a function, not call it (JSF does the calling). So you don't need the "()" on your invocation of setReadOnlyFalse. I don't really recommend making action method names start with "set" or "get", however, as it can confuse the difference between a backing bean Javabean property and an executable method.

I don't see anything objectionable in what you are attempting above and beyond that.
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to fix the issue. I corrected the mentioned problem of me calling the method instead of referencing it. I modified the names and added a few temporary println statements to see what else I was missing.

I did not realize and it did not occur to me that each element was creating it's own bean. To remedy the issue I changed the bean being referenced for the readonly to a SessionScoped bean and had it call the dependant bean. I do not think a SessionScope will be suitable, but I have more to read before I can best decide the scope of the bean. It need only last as long as the page does.

The Session bean:



The dependant bean:




I noticed and resolved another issue while working on this one. the <p:commandButton> action attribute would not call the method being referenced so when update attribute was calling it would refresh the elements listed, but the String they referenced would not change to "false".
I need to read why, as I do not know why at this point, but to fix the action I needed to add an <h:form> around the tabView.


Thank you Tim for your assistance. I would not have resolved this issue without your suggestions.

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit disorganized this morning, so I don't know how many of your core concerns I can detect and respond to, but here's some more wild help.

1. You don't need to use the text values "true" and "false" for your controls. Just use booleans. That means that instead of a "getEditable()" method, you'd have a "isEditable()" method, but other than that, the same thing applies.

Actually, I'm not even sure that the form controls will behave properly if you are set up for text properties on the "enable", "rendered", "readonly" and other boolean tag attributes.

2. In JSF, Request Scope is almost 100% totally useless. So you generally want at least View Scope or Session Scope.
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the values to Boolean per your suggestion. I didn't see any error from using String, though I also just assumed it parsed the String and got a Boolean value out of it.

The SessionScope I think is too much as I want it to only last as long as you remain on that page. I will change that to a ViewScoped. The @Dependent, if I read right, should last until the ViewScoped bean goes away.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!