ADF 动态table以及自定义搜索的实现

来源:互联网 发布:origin8.0作图软件 编辑:程序博客网 时间:2024/06/12 01:50

最近遇到一个需求,客户希望在进行搜索的时候不要固定字段,而是他想添加哪个字段就添加哪个字段,而且搜索出来的结果的列的顺序还得与他添加的顺序一样。 其实ADF的table组件就类似一个迭代器。应该是需要遍历一个list,但是迭代器里面的元素是什么类型呢?想一下,迭代出来的一个对象就是一行,一行里面又有很多列。所以应该又是一个集合,然而每一列都有字段名和字段值,很明显是一个Map;所以table迭代的value类型就好确定了,即List<Map<String,String>> 既然类型确定好了那就好办了,我们就只需要根据需要进行查询了。

1、 自定义搜索,动态添加字段

字段添加在一个下拉列表里面选择,搜索字段组件的添加顺序是根据添加的顺序来的。
下拉列表自动提交为true
每当值改变时都会更新列集合

<af:selectOneChoice id="soc1" autoSubmit="true"                              valueChangeListener="#{backingBeanScope.cdpSearchBean.chooseListener}">            <af:selectItem label="职等" value="job_level" id="si7"/>            <af:selectItem label="最高学历" value="best_edu" id="si2"/>            <af:selectItem label="最高学历专业" value="best_major" id="si1"/>            <af:selectItem label="年龄" value="age" id="si5"/>            <af:selectItem label="部门名称" value="org_name" id="si3"/>            <af:selectItem label="科室名称" value="room_name" id="si6"/>            <af:selectItem label="岗位名称" value="position_name" id="si8"/>            <af:selectItem label="专业性" value="employee_major" id="si10"/>            <af:selectItem label="评语" value="employee_comment" id="si12"/>            <af:selectItem label="期望部门名称" value="except_org_name" id="si9"/>            <af:selectItem label="期望岗位名称" value="except_pos_name" id="si4"/>            <af:selectItem label="建议部门名称" value="suggest_org_name" id="si13"/>            <af:selectItem label="建议岗位名称" value="suggest_pos_name" id="si11"/>          </af:selectOneChoice>

2、 添加字段之后的如何在页面上显示

添加字段之后会遍历之前的字段集合,然后根据字段集合生成对应的组件

  /**     *添加字段的下拉列表监听     * @param valueChangeEvent     */    public void chooseListener(ValueChangeEvent valueChangeEvent) {        //选择的字段        String value = (String)valueChangeEvent.getNewValue();        List list = (List)Tools.getPageFlowScope().get("colList");        //如果该字段未被添加        if (!list.contains(value))            //将这个字段添加到字段集合中            list.add(value);        //存储字段集合        Tools.getPageFlowScope().put("colList", list);        //  根据字段集合重新生成组件        addComponents(list);        System.out.println(value);    }
    /**     *根据附加字段集合添加组件     * @param list     */    public void addComponents(List list) {        //字段与名称对应关系map        Map map = (Map)Tools.getPageFlowScope().get("searchMap");        //  遍历附加字段集合        for (int i = 0; i < list.size(); i++) {            // 如果不存在该组件            if (getGroup().findComponent("id00" + list.get(i)) == null) {                //创建一个新的HtmlTableLayout                HtmlTableLayout tb = new HtmlTableLayout();                //设置id                tb.setId("id00" + list.get(i));                //设置布局                tb.setInlineStyle("float:left;width:350px");                // 创建一个新的HtmlRowLayout                HtmlRowLayout rl = new HtmlRowLayout();                // 创建一个新的HtmlCellFormat                HtmlCellFormat cf1 = new HtmlCellFormat();                //设置布局                cf1.setInlineStyle("width:200px;height:30px;");                //创建一个新得RichOutputText                RichOutputText out = new RichOutputText();                //设置显示值                out.setValue(map.get(list.get(i)));                //创建一个新的HtmlCellFormat                HtmlCellFormat cf2 = new HtmlCellFormat();                //设置布局宽度                cf2.setInlineStyle("width:100px;");                //创建一个新的RichInputText                RichInputText input = new RichInputText();                String el = "#{pageFlowScope['" + list.get(i) + "']}";                ValueExpression ve = Tools.createEL(el);                //根据el表达式添加value绑定                input.setValueExpression("value", ve);                //自动提交                input.setAutoSubmit(true);                // 创建一个新的HtmlCellFormat                HtmlCellFormat cf3 = new HtmlCellFormat();                cf3.setInlineStyle("width:50px;");                // 创建一个新的RichCommandButton                RichCommandButton button = new RichCommandButton();                button.setText("delete");                SetPropertyListener spl = new SetPropertyListener();                spl.setFrom(list.get(i) + "");                spl.setValueExpression("to",                                       Tools.createEL("#{pageFlowScope.deleteValue}"));                //根据el表达式添加传值监听和ActionListener方法                button.addActionListener(spl);                button.setActionListener(Tools.createActionMethodBinding("#{backingBeanScope.cdpSearchBean.deleteColListener}"));                //将组件添加到新的panelGroup中                Tools.addComponent(cf1, out);                Tools.addComponent(cf2, input);                Tools.addComponent(cf3, button);                Tools.addComponent(rl, cf1);                Tools.addComponent(rl, cf2);                Tools.addComponent(rl, cf3);                Tools.addComponent(tb, rl);                Tools.addComponent(getGroup(), tb);                System.out.println(getGroup());            }        }    }

在进入搜索页面之前会进行一个初始化
代码如下:

    public void initSearch() {        //初始化搜索字段对应值        Tools.getPageFlowScope().put("employee_code", null);        Tools.getPageFlowScope().put("employee_name", null);        Tools.getPageFlowScope().put("first_edu", null);        Tools.getPageFlowScope().put("first_major", null);        Tools.getPageFlowScope().put("job_level", null);        Tools.getPageFlowScope().put("best_edu", null);        Tools.getPageFlowScope().put("best_major", null);        Tools.getPageFlowScope().put("age", null);        Tools.getPageFlowScope().put("org_name", null);        Tools.getPageFlowScope().put("room_name", null);        Tools.getPageFlowScope().put("position_name", null);        Tools.getPageFlowScope().put("employee_major", null);        Tools.getPageFlowScope().put("employee_comment", null);        Tools.getPageFlowScope().put("except_org_name", null);        Tools.getPageFlowScope().put("except_pos_name", null);        Tools.getPageFlowScope().put("suggest_org_name", null);        Tools.getPageFlowScope().put("suggest_pos_name", null);        //初始化搜索字段集合        List list1 = new ArrayList();        Tools.getPageFlowScope().put("colList", list1);        Map map = new HashMap();        map.put("employee_code", "员工编号");        map.put("employee_name", "员工姓名");        map.put("first_edu", "第一学历");        map.put("first_major", "第一学历专业");        map.put("job_level", "职等");        map.put("best_edu", "最高学历");        map.put("best_major", "最高学历专业");        map.put("age", "年龄");        map.put("org_name", "部门名称");        map.put("room_name", "科室名称");        map.put("position_name", "岗位名称");        map.put("employee_major", "专业性");        map.put("employee_comment", "评语");        map.put("except_org_name", "期望部门名称");        map.put("except_pos_name", "期望岗位名称");        map.put("suggest_org_name", "建议部门名称");        map.put("suggest_pos_name", "建议岗位名称");        //初始化字段中文对应关系        Tools.getPageFlowScope().put("searchMap", map);        System.out.println(map.size());    }

下面是删除字段的监听代码

  /**     *删除搜索附加字段     * @param actionEvent     */    public void deleteColListener(ActionEvent actionEvent) {        List list = (List)Tools.getPageFlowScope().get("colList");        System.out.println(Tools.getPageFlowScope().get("deleteValue"));        String deletevalue =            (String)Tools.getPageFlowScope().get("deleteValue");        //从搜索字段中移除这个字段        list.remove(deletevalue);        //将该字段隐藏        getGroup().findComponent("id00" + deletevalue).setRendered(false);        //将该字段id释放掉,避免第二次加载在之前的位置        getGroup().findComponent("id00" + deletevalue).setId("ewqe4454545");        //将搜索条件的值清空        Tools.getPageFlowScope().put(deletevalue, null);        //重置搜索字段        Tools.getPageFlowScope().put("colList", list);    }

后台AM中查询的部分代码

    /**     *根据list中的字段进行模糊搜索     * @param colList 需要搜索的字段     */    public void searchByCol(List colList) {        getPageFlowScope().put("colList", colList);        List<Map<String, String>> list = new ArrayList<Map<String, String>>();        String col = "employee_code,employee_name,first_edu,first_major,";        String where = "where ";        //拼接默认字段的查询条件,不为空则拼接        if ((String)getPageFlowScope().get("employee_code") != null &&            (String)getPageFlowScope().get("employee_code") != "") {            where =                    where + " employee_code" + " like '%" + (String)getPageFlowScope().get("employee_code") +                    "%' and";        }        if ((String)getPageFlowScope().get("employee_name") != null &&            (String)getPageFlowScope().get("employee_name") != "") {            where =                    where + " employee_name" + " like '%" + (String)getPageFlowScope().get("employee_name") +                    "%' and";        }        if ((String)getPageFlowScope().get("first_edu") != null &&            (String)getPageFlowScope().get("first_edu") != "") {            where =                    where + " first_edu" + " like '%" + (String)getPageFlowScope().get("first_edu") +                    "%' and";        }        if ((String)getPageFlowScope().get("first_major") != null &&            (String)getPageFlowScope().get("first_major") != "") {            where =                    where + " first_major" + " like '%" + (String)getPageFlowScope().get("first_major") +                    "%' and";        }        //判断list是否为空,拼接第一个字段的查询条件        if (colList.size() > 0) {            if (colList.get(0) != null) {                col = col + colList.get(0);                String key = (String)getPageFlowScope().get(colList.get(0));                if (key != null) {                    where =                            where + " " + colList.get(0) + " like '%" + key + "%' ";                }            }            //拼接后面字段的查询条件            if (colList.size() > 1)                for (int i = 1; i < colList.size(); i++) {                    col = col + ", " + colList.get(i);                    String key =                        (String)getPageFlowScope().get(colList.get(i));                    if (key != null) {                        where =                                where + "and " + colList.get(i) + " like '%" + key +                                "%' ";                    }                }        }        //对SQL语句进行验证 去除多余关键字        if (col.trim().endsWith(","))            col = col.trim().substring(0, col.length() - 1);        if (where.endsWith("and"))            where = where.substring(0, where.length() - 3);        if (where.trim().equals("where"))            where = "";        //拼接SQL语句        String sql = "select " + col + " from cdp_search_result_v " + where;        System.out.println(sql);        Connection conn = null;        ;        PreparedStatement pst = null;        try {            conn =        this.getDBTransaction().createPreparedStatement(sql, 1).getConnection();            pst = conn.prepareStatement(sql);            ResultSet rs = pst.executeQuery();            //遍历结果集            while (rs.next()) {                //存入默认字段                Map<String, String> map = new HashMap<String, String>();                map.put("employee_code", rs.getString("employee_code"));                map.put("employee_name", rs.getString("employee_name"));                map.put("first_edu", rs.getString("first_edu"));                map.put("first_major", rs.getString("first_major"));                //  若有附加字段则显示附加字段                if (colList != null)                    if (colList.size() > 0)                        for (int j = 0; j < colList.size(); j++) {                            String value =                                rs.getString((String)colList.get(j));                            map.put((String)colList.get(j), value);                        }                //结果集合                list.add(map);            }        } catch (SQLException e) {            e.printStackTrace();        }        System.out.println(list);        //存储查询出来的结果的集合        getPageFlowScope().put("searchCollection", list);    }

结果演示图
这里写图片描述

参考文章:http://blog.csdn.net/ygj26/article/details/8744272

原创粉丝点击