You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Caroline Jen <ji...@yahoo.com> on 2005/10/13 16:54:01 UTC

Re: UISelectMany. Mr. Weber, Question Regarding Cleaning Multiple Selections

Mr. Weber:

     Thanks for your guidance again.

     One problem remains.  If users change mind about
the multiple selections that have been made "before"
submitting the form, the action listener will invoke
the clear( ActionEvent e ) method in the backing bean.
 And then, users make another set of selections.  Your
code has been tested working well in cleaning the
previous selection "INTERNALLY".  (I mean either set
selectedItems to null or get the component ValueBiding
and set it to null in the FacesContext will work.)

     The problem is that the "previous selections"
remains "on the screen" after the clear( ActionEvent e
) is invoked.  For example, I first selected File No.2
and File No. 4, then, I invoked the action listener
and the clear( ActionEvent e ).  At this point, I
think file2 and file4 are erased from the
selectedItems, but, File No. 2 and File No. 4 are
still highlighted in the list box on the screen.

     itemDisplay = null; does not help.  How do I also
clear the previous selections "visually"?

--Caroline


  public void clear(ActionEvent e) {

    selectedItems = null;

// or if no direct access possible
//    FacesContext facesContext =
//        FacesContext.getCurrentInstance();
//    ValueBinding vb =
e.getComponent().getValueBinding("value");
//    if (vb != null) {
//      vb.setValue(facesContext, null);
//    }

// ! we have to clear itemsDisplay here also !
// otherwise it holds his value until next submit
click.    
    itemsDisplay = null;
  }



--- Volker Weber <us...@weber-oldenburg.de>
wrote:

> Hi Caroline,
> 
> 
> 
> Caroline Jen wrote:
> ...
> > Meanwhile, would you mind advising:
> > 
> 
> You want to clear the selected Items?
> 
> just clear or null the field in the bean.
> public void clear(ActionEvent e)
> {
>   FacesContext facesContext =
>       FacesContext.getCurrentInstance();
>   ValueBinding vb =
> e.getComponent().getValueBinding();
>   if (vb != null) {
>     vb.setValue(facesContext, null);
>   }
> }
> 
> 
> 
> > 1. How do I deliver each element of the Object[]
> > selectedItems into this method:
> > 
> > public void clear(ActionEvent e) 
> > {
> >     FacesContext facesContext =
> > FacesContext.getCurrentInstance();
> >     UIViewRoot uiViewRoot =
> > facesContext.getViewRoot();
> > 
> >     // Here, I want to iterating through the
> Object[]
> >     if (selectedItems[i] instanceof
> > EditableValueHolder)
> >     {
> 
> this can never happen, selectedItems[i] is not an
> instance of
> EditableValueHolder !
> selectedItems[i] is the first argument of SelectItem
> constructor:
> String in my example.
> 
> >     EditableValueHolder evh =
> (EditableValueHolder)
> > selectedItems[i];
> >     evh.setSubmittedValue(null);
> 
> This has no effect in application phase.
> just setting a variable to null and throwing away
> afterwards.
> 
> >     } 
> > }
> > 
> > The code snippet above is a draft.  I am uncertain
> if
> > I am doing it right.
> > 
> 
> 
> 
> > 2. The multiple selections made by user are in an
> > Object array (i.e. Object[] selectedItems).  How
> do I
> > retrieve the values from this Object array (I
> think
> 
> Just fetch them from your bean or via ValueBinding
> from your Component.
> 
> > that the values retrieved will be a String[] if I
> am
> > not terribly wrong.) to be sent to the database?
> 
> The type is the array type of the objects used to
> create the
> selectItems, in my example Strings.
> 
> I attached updated TestBean.java and test.jsp with
> examples.
> 
> Regards
> Volker Weber
> -- 
> Don't answer to From: address!
> Mail to this account are droped if not recieved via
> mailinglist.
> To contact me direct create the mail address by
> concatenating my forename to my senders domain.
> > package org.apache.myfaces.examples;
> 
> import javax.faces.component.UISelectMany;
> import javax.faces.component.UISelectItems;
> import javax.faces.model.SelectItem;
> import javax.faces.event.ActionEvent;
> import javax.faces.context.FacesContext;
> import javax.faces.el.ValueBinding;
> import java.util.List;
> import java.util.ArrayList;
> 
> public class TestBean {
> 
>   private UISelectMany selectMany;
> 
>   private Object[] selectedItems;
> 
>   public TestBean() {
>     selectMany = new UISelectMany();
>     selectMany.setId("selectMany");
> 
>     UISelectItems items = new UISelectItems();
>     items.setId("selectManyItems");
>     items.setValue(createSelectItems());
> 
>     selectMany.getChildren().add(items);
>   }
> 
>   private Object createSelectItems() {
>     List items = new ArrayList();
>     for (int i = 0; i < 10; i++){
>       items.add(new SelectItem("item" + i, "Item No.
> " + i));
>     }
>     return items;
>   }
> 
>   public UISelectMany getSelectMany() {
>     return selectMany;
>   }
> 
>   public void setSelectMany(UISelectMany selectMany)
> {
>     this.selectMany = selectMany;
>   }
> 
>   public Object[] getSelectedItems() {
>     return selectedItems;
>   }
> 
>   public void setSelectedItems(Object[]
> selectedItems) {
>     this.selectedItems = selectedItems;
>   }
> 
>   public void clear(ActionEvent e) {
> 
>     selectedItems = null;
> 
> // or if no direct access possible
> //    FacesContext facesContext =
> //        FacesContext.getCurrentInstance();
> //    ValueBinding vb =
> e.getComponent().getValueBinding("value");
> //    if (vb != null) {
> //      vb.setValue(facesContext, null);
> //    }
> 
> // ! we have to clear itemsDisplay here also !
> // otherwise it holds his value until next submit
> click.    
>     itemsDisplay = null;
>   }
> 
>   private String itemsDisplay;
> 
>   public void submit(ActionEvent e){
>     Object[] items;
> 
>     items = selectedItems;
> 
> // or if no direct access possible
> //    FacesContext facesContext =
> //        FacesContext.getCurrentInstance();
> //    ValueBinding vb =
> e.getComponent().getValueBinding("value");
> //    if (vb != null) {
> //      items = vb.getValue(facesContext);
> //    }
> 
>     if (items == null || items.length == 0) {
>       itemsDisplay = "No items selected!";
>     } else {
>       StringBuffer sb = new StringBuffer("Selected
> Items: ");
>       for (int i = 0; i < items.length; i++) {
>         sb.append(items[i]);
>         sb.append("; ");
>       }
>       itemsDisplay = sb.toString();
>     }
>   }
> 
>   public String getItemsDisplay() {
>     return itemsDisplay;
>   }
> }
> > <%@ page
> import="org.apache.myfaces.examples.TestBean"%>
> <%@ taglib uri="http://java.sun.com/jsf/html"
> prefix="h"%>
> <%@ taglib uri="http://java.sun.com/jsf/core"
> prefix="f"%>
> 
> <%
>   Object testbean =
> pageContext.getAttribute("TestBean",
> PageContext.SESSION_SCOPE);
>   if (testbean == null) {
>     pageContext.setAttribute("TestBean", new
> TestBean(), PageContext.SESSION_SCOPE);
>   }
> %>
> 
> <html>
> <body>
> <f:view>
>   <h:form>
> 
>     <h:selectManyListbox id="selectMany"
>         binding="#{TestBean.selectMany}"
>         value="#{TestBean.selectedItems}" />
> 
>     <br>
>     <h:commandButton value="Submit"
> actionListener="#{TestBean.submit}"/>
>     <h:commandButton value="Clear"
> actionListener="#{TestBean.clear}"/>
>     <br>
>     <br>
>     <h:outputText value="#{TestBean.itemsDisplay}"/>
> 
>   </h:form>
> </f:view>
> </body>
> </html>



		
__________________________________ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/