datagrid表格显示分页及查询

您所在的位置:网站首页 datagrid分页属性 datagrid表格显示分页及查询

datagrid表格显示分页及查询

2023-08-14 11:44| 来源: 网络整理| 查看: 265

datagrid表格显示分页及查询 具体代码总结:

具体代码

前言:根据我上一篇博客继续写的 实体类:

package com.zhoujun.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 com.zhoujun.dao; import java.sql.SQLException; import java.util.List; import com.zhoujun.entity.Book; import com.zhoujun.util.BaseDao; import com.zhoujun.util.PageBean; import com.zhoujun.util.StringUtils; public class BookDao extends BaseDao { public List list(Book book ,PageBean pageBean ) throws InstantiationException, IllegalAccessException, SQLException{ 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 InstantiationException, IllegalAccessException, SQLException { BookDao bookDao = new BookDao(); Book book = new Book(); List list= bookDao.list(book, null); for (Book b : list) { System.out.println(b); } } }

BookAction类

package com.zhoujun.web; import java.sql.SQLException; 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.zhoujun.dao.BookDao; import com.zhoujun.entity.Book; import com.zhoujun.util.DataGridResult; import com.zhoujun.util.PageBean; import com.zhoujun.util.ResponseUtil; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriven; public class BookAction extends ActionSupport implements ModelDriven { private Book book= new Book(); private BookDao bookDao=new BookDao(); @Override public Book getModel() { // TODO Auto-generated method stub return book; } public String datagrid(HttpServletRequest req ,HttpServletResponse resp) { // total中的数据从哪来? pagebean total属性 // rows的数据从哪来? 每页的展示数据 PageBean pageBean =new PageBean(); pageBean.setRequest(req); try { List list = this.bookDao.list(book, pageBean); // Map map=new HashMap(); // map.put("tatal", pageBean.getTotal()); // map.put("rows", list); ResponseUtil.writeJson(resp, DataGridResult.ok(pageBean.getTotal()+"", list)); // ResponseUtil.writeJson(resp, DataGridResult.SUCCESS); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws JsonProcessingException { Map map=new HashMap(); map.put("tatal", 28); List 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); } }

xml配置 在这里插入图片描述

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:100} ]] }); // 点击搜索按钮 $("#btn-search").click(function(){ // alert(11); $('#dg').datagrid('load', { name: $("#name").val() }); }); })

jsp界面:

$(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:100} ]] }); // 点击搜索按钮 $("#btn-search").click(function(){ // alert(11); $('#dg').datagrid('load', { name: $("#name").val() }); }); })

运行结果:在这里插入图片描述

为了代码的简洁性: 写了一个工具类:

package com.zhoujun.util; public class DataGridResult { private String total; private T rows; public static DataGridResult SUCCESS =new DataGridResult(); 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() { super(); } private DataGridResult(String total, T rows) { super(); this.total = total; this.rows = rows; } public static DataGridResult ok(String total,T rows){ return new DataGridResult(total,rows); } }

从而三行代码只有一行,特别是jsp界面多的时候最实用 在这里插入图片描述

总结:

下次见



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3