天天看點

af:selectManyChoose使用的擷取值問題

元件綁定VO代碼如下:

<af:selectManyChoice value="#{bindings.allDepartments.inputValue}"  
                     label="#{bindings.allDepartments.label}"
                     id="smc1">
  <f:selectItems value="#{bindings.allDepartments.items}" id="si1"/>
</af:selectManyChoice>
           

在實際開發中,擷取下拉值出現的最多的二種問題是:1拿到的索引;2拿到選中值的前一條記錄:

參考代碼:

public String cb1_action() {
  BindingContext bctx = BindingContext.getCurrent();
  BindingContainer bindings = bctx.getCurrentBindingsEntry();
  JUCtrlListBinding allDepartsmentList = 
           (JUCtrlListBinding) bindings.get("allDepartments");
    Object[] selVals = allDepartsmentList.getSelectedValues();
    for (int i = 0; i < selVals.length; i++) {
      Integer val = (Integer)selVals[i];
      //...
    }
   return null;
}
           
public String cb1_action() {
  BindingContext bctx = BindingContext.getCurrent();
  BindingContainer bindings = bctx.getCurrentBindingsEntry();
  JUCtrlListBinding allDepartsmentList = 
          (JUCtrlListBinding) bindings.get("allDepartments");
  int[] selVals = allDepartsmentList.getSelectedIndices();
  for (int indx : selVals ) {
    Row rw = allDepartsmentList.getRowAtRangeIndex(indx);
    //... do your stuff
  }
  return null;
}
           
/**
     * 
     * @param attrName
     * @param ind 選擇的下拉索引值
     * @param retrunStr 傳回的屬性字段名稱
     * @return
     */
    public static Object getSelectListValue(String attrName,Object ind,String retrunStr){
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        JUCtrlListBinding listBinding = (JUCtrlListBinding) bindings.get(attrName);
        listBinding.setSelectedIndex(Integer.parseInt(ind.toString()));
        Row selectedValue = (Row) listBinding.getSelectedValue();
        return selectedValue.getAttribute(retrunStr);
    }