Tree组件使用指南之九:使用ViewCriteria过滤Tree子节点

来源:互联网 发布:burpsuite下载ubuntu 编辑:程序博客网 时间:2024/06/08 14:38
运行环境:JDeveloper 11.1.2.2.0 + Oracle Database 10g Express Edition 10.2.0.1。

ADF Tree组件可以帮助我们轻松创建“一棵树”,但有时我们需要在子节点上设置一些过滤条件。
比如,显示Department/Employee树,希望能够按照Salary范围显示Employee。

重要步骤说明:

1. 创建Model Project,选择Departments和Employees表

2. 在EmployeesView上增加一个ViewCriteria:EmployeesViewCriteria。
根据参数bv_lowSalary和bv_highSalary获取Employees。


3. 在Application Module中,修改DepartmentView1下的EmployeeView3,选中EmployeesViewCriteria。
为了马上能看到效果,我这里直接给参数bv_lowSalary和bv_highSalary赋值。


4. 创建页面,并运行。
创建页面中的Tree的步骤和《点击树节点刷新表单》相同,这里不再赘述。
运行效果如下:


发现并没有按照Salary范围显示Employees,这是为什么呢?
原来,ADF Tree组件访问字节点的方法是通过Accessor,而不是通过VO,因此在VO上设置的ViewCriteria也不会起作用,那么该如何解决呢?
为了更清楚的说明问题,我首先把在Application Module中的DepartmentView1下的EmployeeView3的EmployeesViewCriteria设置为不选中状态,并去掉参数值。


5. 为DepartmentsView生成Java实现类,并Override方法createViewLinkAccessorRS。
    protected ViewRowSetImpl createViewLinkAccessorRS(AssociationDefImpl associationDefImpl,                                                      ViewObjectImpl viewObjectImpl, Row row, Object[] object) {        ViewRowSetImpl viewRowSetImpl =            super.createViewLinkAccessorRS(associationDefImpl, viewObjectImpl, row, object);        ViewObject vo = viewObjectImpl.getViewObject();        ViewCriteriaManager vcm = viewObjectImpl.getViewCriteriaManager();        ViewCriteria vc = vcm.getViewCriteria("EmployeesViewCriteria");        VariableValueManager vvm = vc.ensureVariableManager();        vvm.setVariableValue("bv_lowSalary", 10000);        vvm.setVariableValue("bv_highSalary", 20000);        viewObjectImpl.applyViewCriteria(vc);        return viewRowSetImpl;    }

DepartmentsView就是通过这个方法来访问其子节点的,因此我在这里通过代码让其“找到并使用”EmployeesViewCriteria,并为参数赋值。
这里直接给参数赋了常量值,实际中你可以从其它地方获取参数值,后面我将给出了另外一个实现方法。

6. 重新运行页面,发现ViewCriteria过滤起作用了。


7. 在页面上暴露ViewCriteria参数,由用户输入。
上面实现的方法是给给参数赋了常量值,如果你希望由用户输入参数值,可以考虑在DepartmentsViewImpl.java中写一个公有方法,并通过Data Control暴露出来。
(1)公有方法filterEmployeesBySalary
    public void filterEmployeesBySalary(Number lowSalary, Number highSalary) {        Row row = getCurrentRow();        if (row != null) {            RowSet rs = (RowSet)row.getAttribute("EmployeesView");            if (rs != null) {                ViewObject employeesVO = rs.getViewObject();                employeesVO.ensureVariableManager();                employeesVO.getVariableManager().setVariableValue("bv_lowSalary", lowSalary);                employeesVO.getVariableManager().setVariableValue("bv_highSalary", highSalary);                ViewCriteriaManager vcm = employeesVO.getViewCriteriaManager();                ViewCriteria vc = vcm.getViewCriteria("EmployeesViewCriteria");                employeesVO.applyViewCriteria(vc);            }            executeQuery();        }    }

(2)为DepartmentsView生成Client Interface,并选中方法filterEmployeesBySalary。

刷新Data Control,会发现DepartmentView1下面出现了一个方法:filterEmployeesBySalary。

(3)创建一个新页面,拖放filterEmployeesBySalary生成Form,拖放DepartmentView1生成Tree。
运行效果如下:

注意,使用此方法要注释掉原来重写的方法:createViewLinkAccessorRS。
实际使用时,根据情况可以选择不同的解决方法,如果不需要用户输入,选择方法一;如果需要用户输入,选择方法二。

Project 下载:ADF_Filter_Tree.7z

参考文献:
1. http://jobinesh.blogspot.jp/2010/01/search-by-child-attributes-on-tree.html

2. http://www.oracle.com/technetwork/developer-tools/adf/learnmore/feb2011-otn-harvest-328207.pdf

http://maping930883.blogspot.com/2012/06/adf139treeviewcriteriatree.html

0 0