一:前台加載出前端頁面:
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}]}
