一:前台加载出前端页面:

HTML:

lay-data="{width:800,height:400, url:'data.php', page:true, id:'test'}

js:

layui.use("table",function(){
var table=layui.table

})

表头加载下就行了主要讲解下对接数据库和返回对象的操作。

二:先连接数据库

header("Content-type:text/html;charset=utf-8");
$zhuji='localhost';
$user='root';
$port=3306;
$pwd='';
$name="test";

$conn=mysqli_connect($zhuji,$user,$pwd,$name,$port);
if(!$conn){
die("连接失败:".mysqli_connect_error());
}
mysqli_query($conn,'set names utf8');//防止数据库乱码必须要加

分页四要素:

1 当前页

2 每页多少条记录//每页多少条记录要和前台对接好,取名最好为limit

3从数据库读取出多少条记录

4总共多少页

$page=isset($_GET["page"])?$_GET["page"]:1;
$limit=10;
$count=mysqli_num_rows(mysqli_query($conn,"select * from area"));
$perpage=ceil($count/$limit);

$sql="select `id`,`Names`,`parentid`,`shortname` from area limit ".($page-1)*$limit.",".$limit;
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
echo '{"code":0,"msg":"","count":1000,"data":[';
$i=1;
while($row = mysqli_fetch_assoc($result)) {
$i++;
echo json_encode($row);
if($i<mysqli_num_rows($result)+1){
echo ",";
}

}
echo "]}";
}

总结:后台返回的json数据必须和前台接口对好,否则会报错,json数据格式为

{"code":0,"msg":"","count":1000,"data":[{"id":10000,"username":"user-0","sex":"女","city":"城市-0","sign":"签名-0","experience":255,"logins":24,"wealth":82830700,"classify":"作家","score":57}]}

Posted on 2017-11-21 21:46  LY_Young  阅读(10072)  评论(0编辑  收藏  举报