1.创建表格。url指后台接收请求的链接,注意设置pagination="true"分页才会显示。分页会向后台发送2个参数:
page:表示当前页数
rows:表示一页显示多少条记录
<table id="tt" class="easyui-datagrid" style="width:600px;height:250px"
url="datagrid2_getdata.php"
title="Load Data" iconCls="icon-save"
rownumbers="true" pagination="true">
<thead>
<tr>
<th field="itemid" width="80">Item ID</th>
<th field="productid" width="80">Product ID</th>
<th field="listprice" width="80" align="right">List Price</th>
<th field="unitcost" width="80" align="right">Unit Cost</th>
<th field="attr1" width="150">Attribute</th>
<th field="status" width="60" align="center">Stauts</th>
</tr>
</thead>
</table>
2.后台接收page和rows参数用于计算从mysql数据库里的第几条开始取,取多少条。
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
// ...
$rs = mysql_query("select count(*) from item");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from item limit $offset,$rows");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);
3.返回json包含total和rows两部分,即可实现分页效果
{
"total": "28",
"rows": [
{
"itemid": "EST-19",
"productid": "AV-SB-02",
"listprice": "15.50",
"unitcost": "2.00",
"status": "P",
"attr1": "Adult Male"
},
{
"itemid": "EST-2",
"productid": "FI-SW-01",
"listprice": "16.50",
"unitcost": "10.00",
"status": "P",
"attr1": "Small"
},
{
"itemid": "EST-20",
"productid": "FI-FW-02",
"listprice": "5.50",
"unitcost": "2.00",
"status": "P",
"attr1": "Adult Male"
},
{
"itemid": "EST-21",
"productid": "FI-FW-02",
"listprice": "5.29",
"unitcost": "1.00",
"status": "P",
"attr1": "Adult Female"
},
{
"itemid": "EST-22",
"productid": "K9-RT-02",
"listprice": "135.50",
"unitcost": "100.00",
"status": "P",
"attr1": "Adult Male"
},
{
"itemid": "EST-23",
"productid": "K9-RT-02",
"listprice": "145.49",
"unitcost": "100.00",
"status": "P",
"attr1": "Adult Female"
},
{
"itemid": "EST-24",
"productid": "K9-RT-02",
"listprice": "255.50",
"unitcost": "92.00",
"status": "P",
"attr1": "Adult Male"
},
{
"itemid": "EST-25",
"productid": "K9-RT-02",
"listprice": "325.29",
"unitcost": "90.00",
"status": "P",
"attr1": "Adult Female"
},
{
"itemid": "EST-26",
"productid": "K9-CW-01",
"listprice": "125.50",
"unitcost": "92.00",
"status": "P",
"attr1": "Adult Male"
},
{
"itemid": "EST-27",
"productid": "K9-CW-01",
"listprice": "155.29",
"unitcost": "90.00",
"status": "P",
"attr1": "Adult Female"
}
]
}