ADF11g-032: SelectManyShuttle組件的簡單使用

介紹

ADF中的多選組件多種多樣,有<af:SelectManyCheckbox>,<af:SelectManyChoice>, <af:SelectManyListbox>,<af:selectManyShuttle>等,各組件使用起來都大同小異,都以數組來存儲選中的值。

selectManyShuttle組件提供了用戶的良好體驗,本文簡單介紹selectManyShuttle使用,其它多選組件可以自行舉一反三。

selectManyShuttle組件效果預覽如下圖:


步驟

編寫和配置Manage Bean(說明參見注釋)

package adf.selectmanyshuttle.view.bean;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.context.AdfFacesContext;

public class SelectManyShuttleBean {
    
    //保存選中值的數組
    private String[] selectedValues;
    
    //顯示選中結果
    private String showMessage = "";
    
    //用於提供SelectManyShuttle中的列表值
    private List<String> values;
    
    public SelectManyShuttleBean() {
        super();
        
        //初始化選框列表
        if(values == null) {
            values = new ArrayList<String>();     
        }
        values.add("AAAAAAA");
        values.add("BBBBBBB");
        values.add("CCCCCCC");
        values.add("DDDDDDD");
        values.add("EEEEEEE");
        
        //初始化選擇的值
        selectedValues = new String[] {"AAAAAAA", "BBBBBBB"};
    }

    public String showSelectedValues() {
        // Add event code here...
        if(selectedValues != null) {
            for(int i=0; i<selectedValues.length; i++) {
                showMessage = showMessage + selectedValues[i] + ";";
            }
        }
        return null;
    }
    
    public void setSelectedValues(String[] selectedValues) {
        String[] tempValues = selectedValues;
        if(selectedValues != null) {
            List<String> newValues = new ArrayList<String>(Arrays.asList(selectedValues));
            List<String> oldValues = new ArrayList<String>(Arrays.asList(this.selectedValues));
            //特殊處理,選中DDDDDDD的同時,選中自動選中EEEEEEE
            if(newValues.contains("DDDDDDD") && !oldValues.contains("DDDDDDD")) {
                if(!newValues.contains("EEEEEEE") ) {
                    newValues.add("EEEEEEE");
                    tempValues = newValues.toArray(new String[] {});
                } 
            }
        } 
        this.selectedValues = tempValues;
    }

    public String[] getSelectedValues() {
        return selectedValues;
    }

    public void setValues(List<String> values) {
        this.values = values;
    }

    public List<String> getValues() {
        return values;
    }

    public void setShowMessage(String showMessage) {
        this.showMessage = showMessage;
    }

    public String getShowMessage() {
        return showMessage;
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
  <managed-bean id="__2">
    <managed-bean-name id="__4">SelectManyShuttleBean</managed-bean-name>
    <managed-bean-class id="__3">adf.selectmanyshuttle.view.bean.SelectManyShuttleBean</managed-bean-class>
    <managed-bean-scope id="__1">request</managed-bean-scope>
  </managed-bean>
</adfc-config>

構建頁面

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:form id="f1">
        <af:panelGroupLayout id="pgl1" layout="vertical"
                             inlineStyle="margin:5.0%;">
          <!--value屬性初始化選中結果和保存選中結果-->
          <af:selectManyShuttle
                                value="#{SelectManyShuttleBean.selectedValues}"
                                id="sms1" autoSubmit="true"
                                partialTriggers="sms1">
            <!--通過for each循環List初始化列表值-->
            <af:forEach items="#{SelectManyShuttleBean.values}" var="item">
              <af:selectItem id="si1" value="#{item}" label="#{item}"/>
            </af:forEach>
          </af:selectManyShuttle>
          <af:spacer width="10" height="10" id="s1"/>
          <af:outputText id="ot1" value="#{SelectManyShuttleBean.showMessage}"/>
          <af:commandButton text="Show Selected Values" id="cb1"
                            action="#{SelectManyShuttleBean.showSelectedValues}"/>
        </af:panelGroupLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>

參考文檔

http://docs.oracle.com/cd/E23943_01/apirefs.1111/e12419/tagdoc/af_selectManyShuttle.html



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章