Home Web Front-end JS Tutorial Ajax implements paging query function

Ajax implements paging query function

Mar 31, 2018 pm 04:11 PM
ajax accomplish Query function

This time I will bring you the ajax implementation of the paging query function. What are the precautions for the ajax implementation of the paging query function? The following is a practical case, let's take a look.

The specific code of ajax paging query function is for your reference. The specific content is as follows

The displayed effect is as follows:

The code to achieve the effect is as follows:

1.fenye.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link type="text/css" rel="stylesheet" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow" />
<script src="../jquery-3.2.0.min.js"></script>
<script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<style type="text/css">
.list:hover{ cursor:pointer}
#prev:hover{ cursor:pointer}
#next:hover{ cursor:pointer}
</style>
</head>
<body>
<p class="page-header">
<h1>AJAX分页</h1>
</p>
<p>名称:<input type="text" id="name" /> <button type="button" class="btn btn-info btn-xs" id="chaxun">查询</button></p>
<br />
<table width="100%" class="table table-hover">
 <tr>
  <td>代号</td>
  <td>名称</td>
 </tr>
 
 <tbody id="shuju">
  
 </tbody>
 
</table>
<br />
<ul class="pagination" id="xinxi"></ul>
</body>
<script type="text/javascript">
//代表当前页
var page = 1;
//每页显示几条
var num = 5;
//加载数据
Load();
//加载分页列表
LoadFenYe();
//加载数据的方法
function Load()
{
 var name = $("#name").val();
 $.ajax({
  url:"chuli.php",
  data:{page:page,num:num,name:name},
  type:"POST",
  dataType:"JSON",
  success: function(data){
   var str = "";
   for(var k in data)
   {
    str = str + "<tr><td>"+data[k].code+"</td><td>"+data[k].nno+"</td></tr>";
   }
   $("#shuju").html(str);
  } 
 });
}
//加载分页信息
function LoadFenYe()
{
 //存储所有分页信息的代码
 var s = "";
 var name = $("#name").val();
 //加载上一页
 s = "<li><a id='prev'>«</a></li>";
 
 //加载列表
 var zts = 0;
 $.ajax({
  async:false,
  data:{name:name},
  type:"POST",
  url:"zongtiaoshu.php",
  dataType:"TEXT",
  success: function(data){
   zts = data;
  }
 });
 
 //求总页数
 var zys = Math.ceil(zts/num);
 //为了防止出错
 page = parseInt(page);
 for( var i=page-2;i<page+3;i++)
 {
  if(i>0 && i<=zys)
  {
   if(i==page)
   {
    s = s+"<li class='active'><a ys='"+i+"' class='dangqian'>"+i+"</a></li>";
   }
   else
   {
    s = s+"<li><a ys='"+i+"' class='list'>"+i+"</a></li>";
   }
   
  }
 }
 
 //加载下一页
 s = s+"<li><a id='next'>»</a></li>";
 
 $("#xinxi").html(s);
 
 //给上一页加事件
 $("#prev").click(function(){
  page = parseInt(page);
  if(page>1)
  {page--;}
  
  //重新加载数据
  Load();
  //重新加载分页信息
  LoadFenYe();
 })
 //给下一页加事件
 $("#next").click(function(){
  page = parseInt(page);
  if(page<zys)
  {page++;}
  
  //重新加载数据
  Load();
  //重新加载分页信息
  LoadFenYe();
 })
 
 //给列表加事件
 $(".list").click(function(){
  page = parseInt($(this).attr("ys"));
  //重新加载数据
  Load();
  //重新加载分页信息
  LoadFenYe();
 })
}
$("#chaxun").click(function(){
 //重新加载数据
 Load();
 //重新加载分页信息
 LoadFenYe();
})
</script>
</html>
Copy after login
2.chuli.php

<?php
$page = $_POST["page"];
$num = $_POST["num"];
$name = $_POST["name"];
require "../DBDA.class.php";
$db = new DBDA();
$tguo = ($page-1)*$num;
$sql = "select * from nation where nno like '%{$name}%' limit {$tguo},{$num}";
echo $db->jsonquery($sql);
Copy after login
3.zongtiaoshu.php

<?php
$name = $_POST["name"];
require"../DBDA.class.php";
$db = new DBDA();
$sql ="select count(*) from nation where nno like '%{$name}%'";
echo $db->strquery($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:

How to use ajax to submit comments and automatically refresh

How to let the browser remember the ajax request And control the browser forward and backward

The above is the detailed content of Ajax implements paging query function. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

PHP Programming Guide: Methods to Implement Fibonacci Sequence

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

Master how Golang enables game development possibilities

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

How to implement exact division operation in Golang

See all articles