2012年8月1日星期三

ADF_185:使用Choice List 实现查询

运行环境:JDeveloper 11.1.2.2.0 + Oracle Database 10g Express Edition 10.2.0.1。

本实验来自于客户实际需求:使用Choice List作为查询条件,显示名称,保存ID。

重要步骤说明:

1. 手工创建两个VO,一个用于显示查询结果,一个用于显示查询条件
(1)EmployeesByManagerIdView,作为查询结果显示
SELECT * FROM employees WHERE manager_id = : bv_managerId
(2)ManagersHasEmployeesView,作为查询条件显示
SELECT DISTINCT manager_id,last_name FROM employees WHERE manager_id IS NOT NULL ORDER BY manager_id

2. 创建页面,拖放bv_managerId到页面,选择Select One Choice。
(1)

(2)

(3)点击List Data Source 旁边的Add按钮

(4)选择ManagersHasEmployeesView作为List Data Source,设置返回字段和显示字段。

(5)设计页面显示效果如下:

(6)点击Binding Tab,点击ExecuteWithParams,会发现参数bv_managerId已经绑定到了Choice List组件的值:


3. 修改Choice List,增加ValueChangeListener,隐藏Button
(1)修改后页面代码如下:
<af:form id="f1">
    <af:selectOneChoice value="#{bindings.bv_managerId.inputValue}" label="#{bindings.bv_managerId.label}"
                        required="#{bindings.bv_managerId.hints.mandatory}"
                        shortDesc="#{bindings.bv_managerId.hints.tooltip}"
                        valueChangeListener="#{myBackingBean.managerValueChangeListener}" autoSubmit="true" id="soc1">
        <f:selectItems value="#{bindings.bv_managerId.items}" id="si1"/>
    </af:selectOneChoice>
    <af:commandButton actionListener="#{bindings.ExecuteWithParams.execute}" text="ExecuteWithParams"
                      disabled="#{!bindings.ExecuteWithParams.enabled}" visible="false" id="cb1"/>
    <af:table rows="#{bindings.EmployeesByManagerIdView1.rangeSize}"
              fetchSize="#{bindings.EmployeesByManagerIdView1.rangeSize}"
              emptyText="#{bindings.EmployeesByManagerIdView1.viewable ? 'No data to display.' : 'Access Denied.'}"
              var="row" rowBandingInterval="0" value="#{bindings.EmployeesByManagerIdView1.collectionModel}"
              selectedRowKeys="#{bindings.EmployeesByManagerIdView1.collectionModel.selectedRow}"
              selectionListener="#{bindings.EmployeesByManagerIdView1.collectionModel.makeCurrent}"
              rowSelection="single" id="t1" partialTriggers="::cb1" styleClass="AFStretchWidth">
        <af:forEach items="#{bindings.EmployeesByManagerIdView1.attributeDefs}" var="def">
            <af:column headerText="#{bindings.EmployeesByManagerIdView1.labels[def.name]}" sortable="true"
                       sortProperty="#{def.name}" id="c1">
                <af:outputText value="#{row[def.name]}" id="ot1"/>
            </af:column>
        </af:forEach>
    </af:table>
</af:form>

(2)Managed Bean代码:
    public void managerValueChangeListener(ValueChangeEvent valueChangeEvent) {
        BindingContainer bindings = getBindings();
//        JUCtrlListBinding listBinding = null;
//        listBinding = (JUCtrlListBinding)bindings.get("bv_managerId");
//        ViewRowImpl selectedListRow = null;
//        selectedListRow = (ViewRowImpl)listBinding.getSelectedValue();
//        Integer selectedManagerId = (Integer)selectedListRow.getAttribute("ManagerId");
//        String selectedLastName = (String)selectedListRow.getAttribute("LastName");
//        System.out.println("################ selectedManagerId " + selectedManagerId);
//        System.out.println("################ selectedLastName " + selectedLastName);

        OperationBinding operationBinding = bindings.getOperationBinding("ExecuteWithParams");
//        operationBinding.getParamsMap().put("bv_managerId", selectedManagerId);
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            // ignore
        }
    }


4. 运行效果


Project 下载:ADF_LOV_Query_ChoiceList.7z

没有评论: