您的位置:首页 > 产品设计 > UI/UE

关于easyui中datagrid分页问题【不喜勿喷】【今天才弄懂】

2016-12-20 12:43 387 查看
首先datagrid中的pagination属性设置为true: 如

$('#dg1').datagrid({
url:"{:U('Rearlogin/ajaxNoticeAll')}",    //请求后台的地址
fitColumns:true,
scrollbarSize:0,
singleSelect:true,
pagination:true,     //分页开关
rownumbers: true,
nowrap: false,
pageSize: 10,     //每页显示条数
pageList: [10, 20, 50, 100, 150, 200],   //每页显示条数供选项
showFooter: true,
striped : true,    //设置表格奇偶行颜色是否不同
onSelect:function(rowIndex,rowData){
$(":radio[name='radio_res'][value='"+rowIndex+"']").prop("checked",true);
},
onUnselect:function(rowIndex,rowData){
$(":radio[name='radio_res'][value='"+rowIndex+"']").prop("checked",false);
},
columns:[[
{field:'ck',title:'',formatter:function(value,rowData,rowIndex){
return "<input name='radio_res' value='"+rowIndex+"' type='radio'/>";
}},
{field:'n_id',title:'ID',sortable:true,width:100},
{field:'n_title',title:'公告标题',sortable:true,width:100},
{field:'n_pubdate',title:'发布时间',sortable:true,width:100}
]] ,
toolbar:"#btn"
});


每次会自动传输 pageNum和pageSize到你写的那个URL地址     

后台接收的时候是 page // 当前页数  和 rows //每页显示数目   两个数

然后根据这两个数就可以用 MySQL 中的 limit 方法 控制输出条数    还要得到数据的总条数 total  

两个数转换成 json 格式 返回给前台 就可以了。

附上后台的代码:

$page = I('post.page') ? I('post.page') : 1;     //当前页数
$rows = I('post.rows') ? I('post.rows') : 10;     //每页显示条数
$notice = D('Notice');   //连接数据库
$total = count($notice->getAllNotice());    //总条数
$first_row = ($page-1)*$rows;   //起始位置 = (当前页数-1)*每页显示条数
$list = $notice->order('n_pubdate desc')->limit($first_row.','.$rows)->select(); //满足条件数

$result = array("total" => $total,"rows"=>$list);  //得到的每页数据 和总条数组合成数组
echo json_encode($result);   //输出json字符串


  注意: 是在 thinkphp 框架中实现的 数据库是 MySQL

写的不好的地方不要喷。  第一次写,我也是今天才弄清楚。 写出来只是为了让更多的人知道。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: