EasyUI组合条件查询

EasyUI组合条件查询

HTML:

<table class="easyui-datagrid" style="width:400px;height:250px"   id="myTab"
            data-options="url:'news_find',fitColumns:true,singleSelect:true,fit:true,pagination:true,pageSize:2,pageList:[2,5],
                toolbar: '#tb'
            ">   
        <thead>   
            <tr>   
                <th data-options="field:'news_id',width:100">消息编号</th>   
                <th data-options="field:'news_name',width:100">消息标题</th>   
                <th data-options="field:'new_from',width:100">消息来源</th>   
                <th data-options="field:'newsType',width:100,formatter:showType">消息类型</th> 
            </tr>   
        </thead>   
    </table>  

    <div id="tb">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add()">新增</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="update()">修改</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="del()">删除</a>
        <span style="margin-left: 100px">消息标题</span><input id="queryTitle" class="easyui-textbox" style="width:150px"> 
        <span style="margin-left: 30px">消息类型</span><input id="queryType" class="easyui-combobox" style="width:150px"> 
        <a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="otherQuery()">查询</a> 
    </div>
  • 我们需要加入两个或者多个输入框和一个确定按钮
  • 给确定按钮一个点击事件
  • 当点击的时候JS里面给如下代码
function otherQuery(){
    //获取当前表格
    $('#myTab').datagrid({
        //添加额外参数
        queryParams: {
            "news.news_name": $('#queryTitle').val(),
            "news.newsType.type_id":$('#queryType').val()
        }
    });
    //为了界面友好我会给他显示出去
    $('#queryTitle').textbox('setValue',$('#queryTitle').val());
    $('#queryType').combobox('setValue',$('#queryType').val());
    //当所有代码执行完之后,当前表格会自动提交一份带参数的去后台
}

JAVA代码Service层:

//判断当前对象是否为空,如果为空则为没有条件,否则有条件
if (news == null) {
    //rows的值为获取的分页好的集合
    map.put("rows", newsDao.queryNews(rows, page));
    //total的值为一共有多少条数据
    map.put("total", newsDao.queryCount());
}else {
    //当有条件的时候,我们要根据条件去获取需要的数据
    map.put("rows", newsDao.queryNewsByTerm(news,rows,page));
    map.put("total", newsDao.queryCount(news));
}

JAVA代码Dao层

public class NewsDaoImpl extends BasicDaoImpl<News> implements NewsDao{

    //没有条件的查询需要的数据
    @Override
    public List<News> queryNews(int rows, int page) {
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(new News().getClass()).addOrder(Order.asc("news_id"));
        criteria.setFirstResult((page-1)*rows);
        criteria.setMaxResults(rows);
        @SuppressWarnings("unchecked")
        List<News> list = criteria.list();
        return list;
    }

    //没有条件查询一共有多少数据
    @Override
    public Long queryCount() {
        Long count = (Long) getSessionFactory().getCurrentSession().createCriteria(new News().getClass()).setProjection(Projections.rowCount()).uniqueResult();
        return count;
    }

    //有条件时查询的数据
    @Override
    public List<News> queryNewsByTerm(News news, int rows, int page) {
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(news.getClass()).addOrder(Order.asc("news_id"));
        criteria.setFirstResult((page-1)*rows);
        criteria.setMaxResults(rows);
        //我这里给了两个组合查询,因为我们不知道哪一个有值哪一个没有所有都要try...catch
        //如果有我们就加进去,最后list出结果集返回出去
        try {
            if (news.getNews_name()!=null&&news.getNews_name().length()!=0) {
                criteria.add(Restrictions.like("news_name", "%"+news.getNews_name()+"%"));
            }
        } catch (Exception e) {

        }
        try {
            if (news.getNewsType().getType_id()!=null&&news.getNewsType().getType_id()!=0) {
                criteria.add(Restrictions.eq("newsType.type_id", news.getNewsType().getType_id()));
            }
        } catch (Exception e) {

        }
        @SuppressWarnings("unchecked")
        List<News> list = criteria.list();
        return list;
    }

    //有条件时,我们要根据条件查询一共有多少个数据
    @Override
    public Long queryCount(News news) {
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(news.getClass());
        try {
            if (news.getNews_name()!=null&&news.getNews_name().length()!=0) {
                criteria.add(Restrictions.eq("news_name", news.getNews_name()));

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        try {
            if (news.getNewsType().getType_id()!=null&&news.getNewsType().getType_id()!=0) {
                criteria.add(Restrictions.eq("newsType.type_id", news.getNewsType().getType_id()));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        Long count = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
        return count;
    }

}

心得体会

  • 我们要知道如何把额外参数传出去
  • 我们要知道如何接收额外参数
  • 对参数的处理,当参数太多时可以考虑QBE

猜你喜欢

转载自blog.csdn.net/young_____hu/article/details/79874847
今日推荐