.04.easyui(DataGrid数据查询)

一.图书显示

1.ueseManage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
//导入js和css
<link rel="stylesheet" type="text/css" href="${ pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="${ pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/icon.css">   
<script type="text/javascript" src="${ pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
<script type="text/javascript" src="${ pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script> 
<script type="text/javascript" src="${ pageContext.request.contextPath }/static/js/book.js"></script> 
<title>存放书籍</title>
</head>
<body>
//隐藏域(给book.jsp全路径名)
<input type="hidden" id="ctx" value="${ pageContext.request.contextPath }">
 
<div id="tb">
    <input class="easyui-textbox" id="bname" name="bname" style="width:20%;padding-left: 10px" data-options="label:'书名:',required:true">
    <a id="btn-search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
</div>
 
<table id="dg"></table>  
 
</body>
</html>

book实体类

package com.zking.entity;
 
public class Book {
 
	private int bid;
	private String bname;
	private float price;
 
	public int getBid() {
		return bid;
	}
 
	public void setBid(int bid) {
		this.bid = bid;
	}
 
	public String getBname() {
		return bname;
	}
 
	public void setBname(String bname) {
		this.bname = bname;
	}
 
	public float getPrice() {
		return price;
	}
 
	public void setPrice(float price) {
		this.price = price;
	}
 
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
 
	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}
 
	public Book() {
		// TODO Auto-generated constructor stub
	}
 
}

BookDao

package com.zking.dao;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.entity.Book;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;
 
public class BookDao extends BaseDao<Book>{
    //查询
	public List<Book> list(Book book,PageBean pageBean) throws Exception{
		String sql = " select * from t_mvc_book where 1=1 ";
		String bname = book.getBname();
		if(StringUtils.isNotBlank(bname)) {
			sql += " and bname like '%"+bname+"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}
}

BookAction子控制器(参考EasyUI API文档; DataGrid表格要求参数为 total(数据总条数)、rows(数据对象))

public class BookAction extends ActionSupport implements ModelDriver<Book> {
	private Book book = new Book();
	BookDao bd = new BookDao();
 
	public Book getModel() {
		return book;
	}
	public String datagrid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(req);
		List<Book> list = bd.list(book, pageBean);
		ObjectMapper om = new ObjectMapper();
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("total", pageBean.getTotal());
//		map.put("rows", list);
		ResponseUtil.writeJson(resp,new R().data("total", pageBean.getTotal()).data("rows", list));
		return null;
	}

子控制器中使用了链式编程,将以下代码进行封装,以达到简化代码的作用 

修改前

Map<String, Object> map = new HashMap<String, Object>();
map.put("total", pageBean.getTotal());
map.put("rows", list);

工具类R(共同结果集写到一个方法里面

package com.zking.util;
import java.util.HashMap;
public class R extends HashMap{
	public R data(String key,Object value) {
		this.put(key, value);
		return this;
	}
}

修改后

ResponseUtil.writeJson(resp,new R().data("total", pageBean.getTotal()).data("rows", list)); 

book.js(field属性要用实体类的属性,否则会报错)

 
$(function() {
$('#dg').datagrid({    
    url:$('#ctx').val()+'/book.action?methodName=datagrid',    
    pagination:true,
    toolbar:'#tb',
    columns:[[    
        {field:'bid',title:'id',width:100},    
        {field:'bname',title:'名称',width:100},    
        {field:'price',title:'价格',width:100,align:'right'}    
    ]]    
});

配置luo.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	
	<action path="/menu" type="com.zking.web.MenuAction">
	</action>
	
	<action path="/book" type="com.zking.web.BookAction">
	</action>
	
</config>

二,分页以及查询

分页:参考EasyUI API文档,找到相应属性,加入js文件中即可

 查询:ueseManage.jsp加入该代码

<div id="tb">
    <input class="easyui-textbox" id="bname" name="bname" style="width:20%;padding-left: 10px" data-options="label:'书名:',required:true">
    <a id="btn-search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
</div>

在js文件中与查询组件进行绑定(通过 id=“tb”)(在js文件中通过btn-search属性进行关键字查询)(book.js)

$(function() {
	/**
	 * 在easyUI中,点击下一页上一页等默认的分页效果,携带参数是page\rows
	 * bootstrap,点击下一页上一页等默认的分页效果,携带参数是page\offset
	 */
//	alert($('#ctx').val());
	console.log($('#ctx').val()+'/book.action?methodName=datagrid');
	$('#dg').datagrid({    
	    url:$('#ctx').val()+'/book.action?methodName=datagrid',    
	    pagination:true,
	    /*fitColumns:填充列  使用后width达到100%*/
	    fitColumns:true,
	    /*绑定查询按钮*/
	    toolbar: '#tb',
	    /*表头*/
	    columns:[[    
	        {field:'bid',title:'id',width:100},    
	        {field:'bname',title:'名称',width:100},    
	        {field:'price',title:'价格',width:100,align:'right'}    
	    ]]    
	}); 
 
	/*模糊查询按钮点击事件*/
	$("#btn-search").click(function(){
	   $('#dg').datagrid('load', {    
           /*模糊查询key*/
		   bname: $("#bname").val()  
	   });  
	});
	
})
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值