easyUI数据表格(DataGrid)
DataGrid的使用
DataGrid以表格形式展示数据,并提供了丰富的选择、排序、分组和编辑数据的功能支持。DataGrid的设计用于缩短开发时间,并且使开发人员不需要具备特定的知识。它是轻量级的且功能丰富。单元格合并、多列标题、冻结列和页脚只是其中的一小部分功能。
通过js来创建
json文件:
获取数据库的方式
所需表
实体类:
/**
*
*/
package com.shegx.entity;
import java.sql.Timestamp;
/**
* @author SHE
*
* 2020年6月27日上午8:37:34
* com.shegx.entity
*/
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;
}
public Book(long id, String name, String pinyin, long cid, String author, float price, String image, String publishing,
String description, int state, Timestamp deployTime, int sales) {
super();
this.id = id;
this.name = name;
this.pinyin = pinyin;
this.cid = cid;
this.author = author;
this.price = price;
this.image = image;
this.publishing = publishing;
this.description = description;
this.state = state;
this.deployTime = deployTime;
this.sales = sales;
}
public Book() {
super();
}
public Book(long id, String name, String pinyin) {
super();
this.id = id;
this.name = name;
this.pinyin = pinyin;
}
@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 + "]";
}
}
Bookdao
/**
*
*/
package com.shegx.dao;
import java.util.List;
import com.shegx.entity.Book;
import com.shegx.util.BaseDao;
import com.shegx.util.PageBean;
import com.shegx.util.StringUtils;
/**
* @author SHE
*
* 2020年6月27日上午8:59:07
* com.shegx.dao
*/
public class Bookdao extends BaseDao<Book>{
public List<Book> listbook(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(sql, Book.class,pageBean );
}
public static void main(String[] args) throws Exception {
PageBean pageBean=new PageBean();
Bookdao bd=new Bookdao();
Book book=new Book();
book.setName("测试");
List<Book> lk = bd.listbook(book, pageBean);
for (Book b : lk) {
System.out.println(b);
}
}
}
查詢測試結果:
DataGridResult(用于优化简化代码)
/**
*
*/
package com.shegx.util;
/**
* @author SHE
*
* 2020年6月27日上午10:34:29
* com.shegx.util
*/
public class DataGridResult<T> {
private String total;
private T rows;
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(String total, T rows) {
super();
this.total = total;
this.rows = rows;
}
private DataGridResult() {
super();
}
public static <T> DataGridResult<T> ok(String total,T rows){
return new DataGridResult<>(total,rows);
}
}
BookAction
package com.shegx.web;
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.shegx.dao.Bookdao;
import com.shegx.entity.Book;
import com.shegx.util.DataGridResult;
import com.shegx.util.PageBean;
import com.shegx.util.ResponseUtil;
import com.zhuchenxi.framework.ActionSupport;
import com.zhuchenxi.framework.ModelDriven;
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book=new Book();
private Bookdao bookdao=new Bookdao();
/* (non-Javadoc)
* @see com.zhuchenxi.framework.ModelDriven#getModel()
*/
@Override
public Book getModel() {
return book;
}
public String datarid(HttpServletRequest req,HttpServletResponse resp) {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Book> list =this.bookdao.listbook(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));
} catch (Exception 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);
/* Arrays.asList(new Book(1,"x1","x1"),new Book(2,"x2","x2"),new Book(3,"x3","x3"));*/
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);
}
}
js代码
<input type="hidden" id="ctx" value="${pageContext.request.contextPath }">
<div id="tb">
书名:<input id="name" class="easyui-validatebox" data-options="required:true,validType:'email'" />
<a id="btn-ss" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">搜索</a>
<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true">修改</a>
</div>
<table id="dg"></table>