Saturday, January 8, 2011

MultiSelectChoice: How to get underlying values selected


Let's say you include a multiselectchoice component in your jspx/jsff page, which has <f;selectItem> or <af:forEach> binded to a VO iterator to populate the multiselectchoice and the value property of which is binded to a List attribute binding.

When the user selects some items in that choice List then u want the actual values to be posted.You can check the valuepassthrough flag to true , but many a times it doesn't help and you end up getting the indexes of multiselect values.

Here is a way to get the actual values..

Lets say in the bean u have a utility method to achieve this as follows..

You can associate a valueChangeListener for the multiselectchoice as follows..

public void onValueChangeOfLOV(ValueChangeEvent valueChangeEvent) {
       //get array of indexes of selected items in master list
       List valueIndexes = (List)valueChangeEvent.getNewValue();
       String concatCodes = 
returnSelectmanyChoiceValues(valueIndexes,"YourIterator", 
"YourAttribute");       
   }

   public String returnSelectmanyChoiceValues(List valueIndexes,String 
iterName, String idAttrName){
          DCBindingContainer dc = 
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
          DCIteratorBinding iter = dc.findIteratorBinding(iterName);
          ViewObject vo = iter.getViewObject();

          String codes = "";

          for(Object index : valueIndexes){
              String iIndex = (String)index;
              Row row  = vo.getRowAtRangeIndex(Integer.parseInt(iIndex));
              codes = codes +(String)row.getAttribute(idAttrName)+",";
          }
          //remove last ","  
          if(codes.endsWith(","))
              codes = codes.substring(0,codes.lastIndexOf(","));
          return codes;
   }

This will return u a comma separated values of the selected items. if you want then
You can store it in a List.

No comments:

Post a Comment