一.datagrid数据表格
<script type="text/javascript">
$(function(){
//加载数据
$('#myTable').datagrid({
url:ctx+'/goods.do',//请求路径
pagination:true,//显示分页栏
fitColumns:true,//让列自动适应宽度
singleSelect:true,//只允许选中一行
toolbar: '#myTools',//加载工具栏
loadMsg:'亲,正在努力加载中❥',//提示语句
columns:[[
{field:'gid',title:'商品编号',width:100,align:'center'},
{field:'gname',title:'商品名称',width:100,align:'center'},
{field:'gprice',title:'商品价格',width:100,align:'center'},
{field:'gxl',title:'商品销量',width:100,align:'center'},
{field:'gkc',title:'商品库存',width:100,align:'center'},
{field:'gtime',title:'商品时间',width:100,align:'center'},
{field:'gpath',title:'商品图片',width:100,align:'center', formatter: function(value,row,index){
return '<img src="'+value+'" style="max-height:100px;max-width:100px;"/>';
}},//value表示当前单元格的值,row表示当前行的数据对象,index表示当前行的索引
{field:'ginfo',title:'商品简介',width:100,align:'center'},
{field:'tid',title:'商品分类',width:100,align:'center'},
{field:'gnamePinYin',title:'商品拼音',width:100,align:'center'},
]]});
total:总行数
rows:对象数组
二.Map集合(key-String value-Object)
怎么存值?放一对 put(key,value)
//Map集合
Map<String,Object> mym = new HashMap<String,Object>();
//开始存进去
mym.put("total", rows);//行数
mym.put("rows", ls);//集合
三.实现分页效果
/**
* 模糊查询的分页
* @param pageIndex 设置第几页
* @param pageSize 每页多少条数据
* @param str 关键字
* @param colName 列名
* @return 商品集合
*/
public List<Type> page(int pageIndex, int pageSize, String str, String colName) {
List<Type> ls = new ArrayList<>();
try {
con=DBAccess.getConnection();
String sql="select * from tb_type where "+colName+" like '%"+str+"%' order by tid desc limit ?,?";
ps=con.prepareStatement(sql);
ps.setInt(1,(pageIndex-1)*pageSize);
ps.setInt(2, pageIndex*pageSize);
rs=ps.executeQuery();
while(rs.next()) {
Type t = new Type(rs.getInt(1), rs.getString(2));
ls.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBAccess.close(con, ps, rs);
}
return ls;
}
rows(每页多少条数据)
四.servlert接收
//调用biz层
IGoodsBiz igb = new GoodsBiz();
int pageIndex=1;
int pageSize=10;
//接收参数 page rows str
String pid = req.getParameter("page");
pageIndex=Integer.parseInt(pid);
String sid = req.getParameter("rows");
pageSize=Integer.parseInt(sid);
String str = req.getParameter("str");
if(str==null) {
str="";//相当于查询全部
}
//调用分页的方法得到对象集合
List<Goods> ls = igb.page(pageIndex, pageSize, str, "gname");
//调用方法得到总行数
int rows = igb.getRows("tb_goods where gname like '%"+str+"%'");
//Map集合
Map<String,Object> mym = new HashMap<String,Object>();
//开始存进去
mym.put("total", rows);//行数
mym.put("rows", ls);//集合
//把Map集合转成json数据
String jsonStr = JSON.toJSONString(mym);
//输送到客户端
out.print(jsonStr);