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

187 篇文章 2 订阅
运行环境: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值