• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selecting some entries in selectManyListbox

 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'd like someone to advise me how to have (only) some entries in selectManyListbox selected. I have many-to-many relationship between Clients and Products, and when I was adding new Client I was populating selectManyListbox with all the available Products. Everything was correctly inserted into database, and now I am trying to create edit page. This selectManyListbox should contain all the available products (like before), but I would like to have Client's products selected. So in the backing bean I have list with all the products in form of List<SelectItem> (used earlier when adding), and I have List<Product> retrieved from the database. SelectItem entries where created using product id and name (ie, new SelectItem(product.getId(),product.getName())). And I don't know how to combine them to have what I described earlier. <f:selectedItem> and <f:selectedItems> don't even have 'selected' attribute, or something similar. BTW, I am using MyFaces 1.2 and Facelets.

Regards, Ben.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to preset them in the 'value' attribute of the input component.
 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bauke,

Thank you for your reply. Can you please explain what you mean by 'value of the input component'? When translated to HTML, SELECT component contains OPTIONs whose values are product ids, and product names are displayed for viewing. What I need is to have SELECTED for those options whose id matches products ids in List<Products>. I can match them easily I think, but have problems marking them as SELECTED using selectManyListbox.

Regards, Ben.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All input components have a 'value' attribute which you normally bind to a bean property. If you want to give it another default value than null, then just preset it in beanĀ“s constructor or so.
 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke, perhaps I wasn't clear in my first post ... I have binding to the bean property in place and the 'value' attributes are all set. What I am after is a way of programatically selecting some of the items displayed in the listbox. I know the values of the items I want to select, I just need to know how to select them.

Regards, Ben.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. As I said, preset the property behind the value attribute. You can do that in the bean constructor.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not have something like, setSelectedValues(Object[] selectedValues) for your component?
 
anna yuan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. backingBean.allProducts -->list for all products
2. backingBean.selectProductList --> list for client's products retrieved from the database.

<s:selectManyPicklist id="selectProductList" style="width:300px; height: 340px;" value="#{backingBean.selectProductList}" size="20" styleClass="inputText">
<f:selectItems value="#{backingBean.allProducts}" />
</s:selectManyPicklist>
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example:



This will show a multiselect listbox with 5 items labeled "label1" up to "label5" where the "label2" and "label4" are pre-selected.

Got it now? It is nothing different from how all other input components work. Just preset the default value in the property behind the 'value' attribute.
 
Ben Gateau Jr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you anna and Bauke for the examples. I've made a mistake of not posting my code, it would be clear what was wrong. I had 'value' attribute set but only on 'selectItems'. That items can be selected by setting the 'valule' of selectManyListbox - I would not have guessed it for a long time. Thank you both so much for your help and assistance, and Bauke I appreciate your perseverance

Regards, Ben.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic