JavaWeb中关于easyui-datagrid分页

时间:2021-04-29 13:17:32

1、创建一个easyui-datagrid的数据网格,其中formatter依次是格式化文件、日期和销售状态

<table id="dg" class="easyui-datagrid" 
data-options="url:'showGoods',fitColumns:true,singleSelect:true,fit:true,toolbar:'#tb',pagination:true">

<thead>
<tr>
<th data-options="field:'gurl',formatter:function(value,row,index){
return tupian(value,row,index);
}"
>
图片</th>
<th data-options="field:'gid',checkbox:'true'">商品编号</th>
<th data-options="field:'agname'">商品名称</th>
<th data-options="field:'gprice'">商品价格</th>
<th data-options="field:'gnum'">商品数量</th>
<th data-options="field:'prodate',formatter: function(value,row,index){
var prodate=new Date(value);
return prodate.toLocaleString();
}"
>
生产日期</th>
<th data-options="field:'bzdate'">保质期</th>
<th data-options="field:'gaddr'">生产地</th>
<th data-options="field:'gtel'">联系电话</th>
<th data-options="field:'gstatus',formatter: function(value,row,index){
return value==1?'在售中':'已下架';
}"
>
销售状态</th>
</tr>
</thead>
</table>

2、创建Servlet用来交互代码如下

        int page = Integer.parseInt(request.getParameter("page"));//当前第几页
int rows = Integer.parseInt(request.getParameter("rows"));//每页多少行
PrintWriter out = response.getWriter();
GoodsDao dao=new GoodsDaoImpl();
try {
PageBean pb = dao.showGoods(page,rows);
ObjectMapper om = new ObjectMapper();//创建jackjson的对象
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));//格式化日期
String json = om.writeValueAsString(pb);//把集合转为json数据发送给页面
out.print(json);
} catch (Exception e) {
e.printStackTrace();
}
out.close();

3、JDBC查询语句的编写

String sql = "Select * from goods where 1=1";
String pagesql="select count(1) from ("+sql+") a";
sql += " order by gid desc limit "+(page-1)*rows+","+rows;
ResultSet rs = MysqlJDBC.select(pagesql);
//获取总共多少条数据
int total=0;
if(rs.next()){
total = rs.getInt(1);
}
//利用commons返回数据
List<Goods> gs = new BeanListHandler<Goods>(Goods.class).handle( MysqlJDBC.select(sql));
PageBean pb = new PageBean();
pb.setTotal(total);
pb.setRows(gs);
MysqlJDBC.close();
return pb;