easyUI数据表格(DataGrid)
获取方式
效果
1.先写好所需要的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">
<!--全局样式 -->
<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/default/easyui.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>
<!--组建库源码的 js文件 -->
<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>
<input type="hidden" id="ctx" value="${pageContext.request.contextPath }"/>
<table id="dg"></table>
<div id="tb">
<div id="tb">
<input class="easyui-textbox" id="name" name="name" style="width:25%;padding-left:5px" data-options="label:'书名:',">
<a id="btn-search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">增加</a>
</div>
</div>
</body>
</html>
2,通过标签创建DataGrid控件。在表格内使用
标签定义列。 |
---|
准备json文件
{"total":28,"rows":[
{"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
]}
3,获取方式查看我们所需要获取的数据表
编写数据库所对应的实体类
package entity;
import java.sql.Timestamp;
public class Book{
private long id;
private String name;
private String pinyin;
private long cid;
private String author;
private float price;
private String image;
private String publishing;
private String description;
private int state;
private Timestamp deployTime;
private int sales;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
public long getCid() {
return cid;
}
public void setCid(long cid) {
this.cid = cid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPublishing() {
return publishing;
}
public void setPublishing(String publishing) {
this.publishing = publishing;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public Timestamp getDeployTime() {
return deployTime;
}
public void setDeployTime(Timestamp deployTime) {
this.deployTime = deployTime;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", pinyin=" + pinyin + ", cid=" + cid + ", author=" + author
+ ", price=" + price + ", image=" + image + ", publishing=" + publishing + ", description="
+ description + ", state=" + state + ", deployTime=" + deployTime + ", sales=" + sales + "]";
}
public Book() {
super();
}
public Book(long id, String name, String pinyin) {
super();
this.id = id;
this.name = name;
this.pinyin = pinyin;
}
}
在编写dao方法
package dao;
import java.sql.SQLException;
import java.util.List;
import entity.Book;
import uilt.BaseDao;
import uilt.PageBean;
import uilt.StringUtils;
public class BookDao extends BaseDao<Book>{
public List<Book> list(Book book,PageBean pagebean) throws Exception{
String name=book.getName();
String sql=" select * from t_easyui_book where true ";
if(StringUtils.isNotBlank(name)) {
sql +=" and name like '%"+name+"%'";
}
return super.executeQuery(Book.class, sql, pagebean);
}
public static void main(String[] args) throws Exception {
BookDao bd=new BookDao();
Book book=new Book();
List<Book> list = bd.list(book , null);
for (Book b : list) {
System.out.println(b);
}
}
}
这时我们及可以测试一下看我们是否拿数据库里面的值
拿到了我们就开始写bookActin
package web;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriven;
import dao.BookDao;
import entity.Book;
import uilt.DataGridResult;
import uilt.PageBean;
import uilt.ResponseUtil;
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book=new Book();
private BookDao bd=new BookDao();
private Object dataGridResult;
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
public String datagrid(HttpServletRequest req,HttpServletResponse resp) throws Exception {
// total中的数据从哪来 符合条件的总记录数(pagebean toatl属性)
// rows中的数据从哪来 每一页的行数
PageBean pagebean=new PageBean();
pagebean.setRequest(req);
try {
List<Book> list = this.bd.list(book, pagebean);
/* Map<String, Object> map=new HashMap<String, Object>();
map.put("total", pagebean.getTotal());
map.put("rows", list);
*/
ResponseUtil.writeJson(resp, DataGridResult.ok(pagebean.getTotal()+"",list));
//ResponseUtil.writeJson(resp, DataGridResult.SUCCESS);
//硬编码 1 0 魔鬼数字
//ResponseUtil.writeJSON(resp, DataGridResult.ok(pagebean.getTotal()+"", list));
//ResponseUtil.writeJSON(resp, DataGridResult.success);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws JsonProcessingException {
Map<String, Object> map=new HashMap<String, Object>();
map.put("total", 28);
List<Book> asList = Arrays.asList(new Book(1,"x1","x1"),new Book(2,"x2","x2"),new Book(3,"x3","x3"));
map.put("rows", asList);
ObjectMapper om=new ObjectMapper();
String jsonstr=om.writeValueAsString(map);
System.out.println(jsonstr);
}
}
在导入我们需要的工具类DataGridResult
package uilt;
public class DataGridResult <T>{
private String total;
private T rows;
private String msg;
public static DataGridResult SUCCESS=new DataGridResult<>(/*200,"操作成功"*/);
public static DataGridResult failtrue=new DataGridResult<>(/*400,"操作失败*/);
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public T getRows() {
return rows;
}
public void setRows(T rows) {
this.rows = rows;
}
private DataGridResult() {}
private DataGridResult(String total, T rows) {
super();
this.total = total;
this.rows = rows;
}
public static <T>DataGridResult<T> ok (String total,T rows){
return new DataGridResult<>(total,rows);
}
}
然后就是js
$(function(){
var ctx=$("#ctx").val();
$('#dg').datagrid({
url:ctx+'/book.action?methodName=datagrid',
pagination:true,
toolbar: '#tb',
columns:[[
{field:'id',title:'id',width:100},
{field:'name',title:'书籍名称',width:100},
{field:'pinyin',title:'拼音',width:100,align:'right'},
{field:'cid',title:'书籍类别',width:100},
{field:'author',title:'作者',width:100},
{field:'price',title:'价格',width:100},
{field:'image',title:'图片',width:500}
]]
});
//点击搜索按钮1完成名字进行书籍名称
$('#dg').datagrid('load', {
name: 'easyui',
address: 'ho'
});
})