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) {
map.put("rows", newsDao.queryNews(rows, page));
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 {
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) {
}
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) {
}
Long count = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
return count;
}
}
心得体会
- 我们要知道如何把额外参数传出去
- 我们要知道如何接收额外参数
- 对参数的处理,当参数太多时可以考虑QBE