Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the steps to implement data paging query with ajax

php中世界最好的语言
Release: 2018-04-02 14:56:44
Original
2431 people have browsed it

This time I will bring you a detailed explanation of the steps to implement ajax data paging query. What are the precautions for ajax to implement data paging query. The following is a practical case, let's take a look.

Use ajax to query the database and paginate the query data for your reference. The specific content is as follows

Main page code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.11.2.min.js"></script>
<style type="text/css">
.dangqian{ background-color:#69F}
</style>
</head>
<body>
<p>
<input type="text" id="key" />
<input type="button" value="查询" id="chaxun" />
</p>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
 <tr>
  <td>代号</td>
  <td>名称</td>
  <td>父级代号</td>
 </tr>
 
 <tbody id="nr">
 
 </tbody> 
</table>
<p id="xinxi">
</p>
</body>
Copy after login

js code

<script type="text/javascript">
var page = 1; //当前页 定义一个变量 当前页
Load(); //加载数据
LoadXinXi(); //加载分页信息
//查询
$("#chaxun").click(function(){
  page = 1;
  Load(); //加载数据
  LoadXinXi(); //加载分页信息
 })
function Load()
{
 var key = $("#key").val(); //查询条件
 
 $.ajax({
   url:"chuli.php",
   data:{page:page,key:key},
   type:"POST",
   dataType:"JSON",
   success: function(data){
     var str = "";
     for(var k in data)
     {
      str +="<tr><td>"+data[k].AreaCode+"</td><td>"+data[k].AreaName+"</td><td>"+data[k].ParentAreaCode+"</td></tr>";
     }
     $("#nr").html(str);//把数据返回表格
    }
  });
}
functionLoadXinXi()
{
 var str = "";
 var minys = 1;
 var maxys = 1;
 var key = $("#key").val();
 
 //查总页数
 $.ajax({
   async:false,
   url:"zys.php",
   data:{key:key},
   type:"POST",
   dataType:"TEXT",
   success: function(d){
     maxys = d;
    }
  });
 
 str += "<span>总共:"+maxys+"页</span>  ";
 str += "<span id=&#39;prev&#39;>上一页</span>";
 
 for(var i=page-2;i<page+3;i++)
 {
  if(i>=minys && i<=maxys)
  {
   if(i==page)
   {
    str += "<span class=&#39;dangqian&#39; bs=&#39;"+i+"&#39;>"+i+"</span>  ";
   }
   else
   {
    str += "<span class=&#39;list&#39; bs=&#39;"+i+"&#39;>"+i+"</span>  ";
   }
  
  }
 }
  
 str += "<span id=&#39;next&#39;>下一页</span>"; 
 
 $("#xinxi").html(str);
 
 //给上一页添加点击事件
 $("#prev").click(function(){
   page = page-1;
   if(page<1)
   {
    page=1;
   }
   Load(); //加载数据
   LoadXinXi(); //加载分页信息
  })
 //给下一页加点击事件
 $("#next").click(function(){
   page = page+1;
   if(page>maxys)
   {
    page=maxys;
   }
   Load(); //加载数据
   LoadXinXi(); //加载分页信息
  })
 //给中间的列表加事件
 $(".list").click(function(){
   page = parseInt($(this).attr("bs"));
   Load(); //加载数据
   LoadXinXi(); //加载分页信息
  })
}
</script>
Copy after login

Processing Page 1

<?php
include("DBDA.class.php");
$db = new DBDA();
$key = $_POST["key"];
$num = 20;
$sql = "select count(*) from chinastates where areaname like &#39;%{$key}%&#39;";
$zts = $db->StrQuery($sql);
echo ceil($zts/$num);
Copy after login

Processing Page 2

<?php
include("DBDA.class.php");
$db = new DBDA();
$page = $_POST["page"];
$key = $_POST["key"];
$num = 20;
$tiao = ($page-1)*$num;
$sql = "select * from chinastates where areaname like &#39;%{$key}%&#39; limit {$tiao},{$num}";
echo $db->JSONQuery($sql);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS implements AJAX partial refresh (with code)

AJAX cross-domain request JSONP detailed steps to obtain JSON data ( Code attached)

The above is the detailed content of Detailed explanation of the steps to implement data paging query with ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template