Home  >  Article  >  Web Front-end  >  Ajax writes paging query (without refreshing the page)

Ajax writes paging query (without refreshing the page)

韦小宝
韦小宝Original
2018-01-01 18:21:421586browse

This article mainly introduces an example of writing a paging query in Ajax (without refreshing the page). It has a good reference and value for learning ajax. Let’s follow the editor to take a look at writing a paging query in Ajax (without refreshing the page).

Requirements:

#To obtain a large amount of information in the database and display it on the page, paging queries must be used;

If you don’t use Ajax, but use other methods, you will definitely need to refresh the page, and the user health check will be very bad.

So it is best to use the Ajax method to write paging queries;

1. First find a table with a lot of data!

A simple table

Code, introduce jquery package:

ff6f2b2d10322e7dae71e7e3cf8987252cacc6d41bbb37262a98f745aa00fbf0

Write a table showing our codename and name:

<table class="table table-striped">
 <thead>
 <tr>
 <td>代号</td>
 <td>名称</td>
 <td>操作</td>
 </tr>
 </thead>
 <tbody id="td">
 </tbody>
</table>

These are very simple and there is nothing wrong with them!

2. Set a current page and define a variable as 1 (first page):

 var page = 1;
 //当前页,默认等于1

3. Let’s write the first method: you need to use ajax. This method is mainly used for query and paging:

function load()
 {
 $.ajax({
  url: "jiazai.php",
// 显示所有的数据不用写data
  data:{page:page},
//当前页记得传过去
  type:"POST",
  dataType: "TEXT",
  success: function (data) {
  }
 });
 }

4. Write the processing page for displaying data; what you need to consider here is how many pieces of data you want to skip and how many pieces of data you want to display. Use limit:

<?php
include ("db.class.php");
$db = new db();
$page=$_POST["page"];
//去当前页page
$num = 3;
//每页显示几条
$tg = ($page-1)*3;//跳过几条
$sql = "select * from min limit {$tg},{$num}";
//limit:两个参数,第一个是跳过多少条,第二个是取多少条
echo $db->Query($sql);

After completing the first step, let’s take a look at the picture:

Display data implementation!

Okay, three pieces of data on each page have been implemented. (I used Bookstrap to beautify the webpage to make the page look like this, as mentioned earlier)

5. Display paging information , write a method, first use ajax to get the total number of pages:

function loadfenye()
 {
 var s = "";
 //用于接收
 var xiao = 1;
// 最大页
 var da = 1;
// 最小页
 $.ajax({
 async:false,
//  做成同步
 url:"zys.php",
 dataType:"TEXT",
 success:function(data){
 da = data;
 //最大页数
  }
});
 }

Next, do the php page to query the total number of pages:

<?php
//查询总页数
include ("db.class.php");
$db = new db();
$sql = "select count(*) from min";
$zts = $db->strquery($sql);
//总条数
echo ceil($zts/3);
//ceil向上取整

Okay, the total number of pages has been obtained, come back and finish the paging:

//加载分页信息方法
 function loadfenye()
 {
  var s = "";
  //用于接收
  var xiao = 1;
//  最大页
  var da = 1;
//  最小页
  $.ajax({
 async:false,
//   做成同步
 url:"zys.php",
 dataType:"TEXT",
 success:function(data){
  da = data;
  //最大页数,查到的最大页数交个默认的最大页数
   }
});
//加载上一页
  s += "<li><a>«</a></li>";
//  加载分页列表
for(i=page-4;i<page+5;i++)
{
 //i代表列表的页数
 if(i>=xiao && i<=da)
 {
  s += " <li><a>"+i+"</a></li>"
 }
}
  //  加载下一页
  s += "<li><a>»</a></li>";
$("#fenye").html(s);
 }

After writing this, look at the picture:

##The paging information is also displayed

6. Let’s change the number of pages selected by default Let’s change the background color

Take a look at Bookstrap; how to change the background color:

Obviously there is an additional active style , let’s add it using judgment

if(i>=xiao && i<=da) {
  if (i == page) {
   s += " <li class=&#39;active&#39;><a>" + i + "</a></li>"
  }
  else {
   s += " <li><a>" + i + "</a></li>";
  }

Okay, let’s take a look:

Okay, No problem

7. Create a click event for the page number, jump to the page number and display the data when clicking the page number, and update the list;

Give it first Number list plus a class

s += " cafacf378c71ace5e07679b1af3510833499910bf9dac5ae3c52d5ede7383485" + i + "5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb"

Then write the method:

//给列表加上点击事件
  $(".list").click(function(){
   //改变当前页数
   //把点击的页数,扔给page(当前页)
   page = $(this).text();
//   page获取了当前页,重新加载以下方法
   //调用load方法
   load();
   //把加载数据封装成一个方法
   loadfenye();
   //加载分页信息方法
  })
 }

When I click on the fifth page:

Nothing wrong;

8. Next is the click event on the previous page and the next page. The first is the click event on the previous page:

First, add class to the list on the previous page to facilitate writing events:

s += "d5a5fceb7048334bb2abfc2cbf7e02c73499910bf9dac5ae3c52d5ede7383485«5db79b134e9f6b82c0b36e0489ee08ed548cefb384a95312edb7e8fb1f4ecc29";

Come on, click event on the previous page:

$(".sy").click(function(){
   //改变当前页
   if(page>1)
   {
    //如果不是第一页
    page = parseInt(page) - 1;
   }
   //   page获取了当前页,重新加载以下方法
   //调用load方法
   load();
   //把加载数据封装成一个方法
   loadfenye();
   //加载分页信息方法
  })

Click event on the next page:

Same as above: Add class to the list to facilitate writing events:

s += "a853b89d5d558e18b116fc5eb8396df43499910bf9dac5ae3c52d5ede7383485»5db79b134e9f6b82c0b36e0489ee08ed548cefb384a95312edb7e8fb1f4ecc29";

Next page click event:

//下一页点击事件
  $(".xy").click(function(){
//   alert(da);
   if(page<da)
   {
    //如果不是第一页
    page = parseInt(page) + 1;
   }
   //   page获取了当前页,重新加载以下方法
   //调用load方法
   load();
   //把加载数据封装成一个方法
   loadfenye();
   //加载分页信息方法
  })

Okay, perfect implementation of ajax paging query;

8. Add a conditional query:

Add a text box:

<p>
 <input type="text" id="name"/>
 <input type="button" id="chaxun" value="查询"/>
</p>

To write the click event:

//给查询加点击事件
 $("#chaxun").click(function(){
  //重新加载
  //调用load方法
  load();
  //把加载数据封装成一个方法
  loadfenye();
  //加载分页信息方法
 })

Next we need to change these two methods:

ajax just needs to pass the name of the text box:


data:{page:page,name:name},
   type:"POST",

data:{name:name},
 type:"POST",

On the processing page, set an equality condition:

$tj = " 1=1 ";
if(!empty($_POST["name"]))
{
 $name = $_POST["name"];
 $tj = " name like &#39;%{$name}%&#39; ";
}

最后在sql语句后面调用就好啦

图:

页面不刷新的分页查询就欧克了;

源码:

显示页面:




 
 无标题文档
 
 
 



显示数据

<p> <input type="text" id="name"/> <input type="button" id="chaxun" value="查询"/> </p>
代号 名称 操作

<script> var page = 1; //当前页,默认等于1 //调用load方法 load(); //把加载数据封装成一个方法 loadfenye(); //加载分页信息方法 //给查询加点击事件 $(&quot;#chaxun&quot;).click(function(){ //重新加载 //调用load方法 load(); //把加载数据封装成一个方法 loadfenye(); //加载分页信息方法 }) function loadfenye() { var s = ""; //用于接收 var name = $("#name").val(); var xiao = 1; // 最大页 var da = 1; // 最小页 $.ajax({ async:false, // 做成同步 url:"zys.php", data:{name:name}, type:&quot;POST&quot;, dataType:"TEXT", success:function(data){ da = data; //最大页数 } }); //加载上一页 s += "<li class=&#39;sy&#39;><a>«</a></li>"; // 加载分页列表 for(var i=page-4;i<page+5;i++) { //i代表列表的页数 if(i>=xiao && i<=da) { if (i == page) { s += " <li class=&#39;active list&#39;><a>" + i + "</a></li>" } else { s += " <li class=&#39;list&#39;><a>" + i + "</a></li>"; } } } // 加载下一页 s += "<li class=&#39;xy&#39;><a>»</a></li>"; $("#fenye").html(s); //给列表加上点击事件 $(".list").click(function(){ //改变当前页数 //把点击的页数,扔给page(当前页) page = $(this).text(); // page获取了当前页,重新加载以下方法 //调用load方法 load(); //把加载数据封装成一个方法 loadfenye(); //加载分页信息方法 }) //上一页点击事件 $(&quot;.sy&quot;).click(function(){ //改变当前页 if(page&gt;1) { //如果不是第一页 page = parseInt(page) - 1; } // page获取了当前页,重新加载以下方法 //调用load方法 load(); //把加载数据封装成一个方法 loadfenye(); //加载分页信息方法 }) //下一页点击事件 $(&quot;.xy&quot;).click(function(){ // alert(da); if(page&lt;da) { //如果不是第一页 page = parseInt(page) + 1; } // page获取了当前页,重新加载以下方法 //调用load方法 load(); //把加载数据封装成一个方法 loadfenye(); //加载分页信息方法 }) } function load() { var name = $("#name").val(); $.ajax({ url: "jiazai.php", // 显示所有的数据不用写data data:{page:page,name:name}, type:&quot;POST&quot;, dataType: "TEXT", success: function (data) { var str = ""; var hang = data.split("|"); //split拆分字符串 for (var i = 0; i < hang.length; i++) { //通过循环取到每一行;拆分出列; var lie = hang[i].split("-"); str = str + "<tr><td>" + lie[0] + "</td><td>" + lie[1] + "</td><td>" + "<button type=&#39;button&#39; class=&#39;btn btn-info sc&#39; ids=&#39;"+lie[0]+"&#39;>点击删除</button><button type=&#39;button&#39; class=&#39;btn btn-primary xq&#39; ids=&#39;"+lie[0]+"&#39;>查看详情</button>" + //ids里面存上主键值 "</td></tr>"; } $("#td").html(str); //找到td把html代码扔进去 addshanchu(); addxiangqing(); } }); } //给查看详情加事件 function addxiangqing() { $(".xq").click(function(){ $(&#39;#myModal&#39;).modal(&#39;show&#39;) //打开模态框 var ids = $(this).attr("ids"); $.ajax({ url:"xiangqing.php", data:{ids:ids}, dataType:"TEXT", type:"POST", success:function(data){ //拆分 var lie = data.split("^"); // var str = "<p>代号:"+lie[0]+"</p><p>名称:"+lie[1]"</p>"; //造字符串 var str = "<p>代号:"+lie[0]+"</p><p>名称:"+lie[1]+"</p>"; $("#nr").html(str); } }); //在模态框里面要显示的内容 }) } //把删除事件封装成方法: function addshanchu() { //给删除按钮加上事件 $(".sc").click(function () { var ids = $(this).attr("ids"); $.ajax({ url: "shanchu.php", data: {ids: ids}, dataType: "TEXT", type: "POST", success: function (d) { if (d.trim() == "ok") { alert("删除成功"); //调用加载数据的方法 load(); } else { alert("删除失败"); } } }); }) } </script>

查询总页数的页面:

strquery($sql);
//总条数
echo ceil($zts/3);
//ceil向上取整

加载分页信息的页面:

Query($sql);
//遍历
$str="";
foreach ($arr as $v)
{
 $str = $str.implode("-",$v)."|";
 //用-把$v拼起来,拼出来是1-红2-蓝,用|分割,拼出来是1-红|2-蓝|
}
$str = substr($str,0,strlen($str)-1);
//截取字符串:从第0个开始,截取它的长度-1
//strlen获取字符串长度
echo $str;

以上就是本篇文章的所有内容了,希望对大家学习有所帮助!

相关推荐:

json格式的Ajax提交示例代码

关于多个Ajax请求执行返回先后的问题示例探讨

AJax实现类似百度搜索栏的功能

The above is the detailed content of Ajax writes paging query (without refreshing the page). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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