Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to implement paging technology using ajax (with code)

Detailed explanation of the steps to implement paging technology using ajax (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-04-02 15:24:001770browse

This time I will bring you a detailed step-by-step explanation of using ajax to implement paging technology (with code). What are the precautions for using ajax to implement paging technology? . Here is a practical case, let’s take a look.

The ajax paging rendering is as follows:

#First, let’s look at the HTML code and CSS code. We need a table and a footer:

<p id="global">
<p id="table">
 <table>
 <col width="19%">
 <col width="19%">
 <col width="19%">
 <col width="19%">
 <col width="24%">
 <tr>
 <th>日期</th>
 <th>时间</th>
 <th>事件</th>
 <th>报警画面</th>
 <th>事件备注</th>
 </tr>
 </table>
 </p>
 <p id="footer">
 <span id="summary"></span>
 <ul id="pagination">
 <li id="01">首页</li>
 <li id="02">上一页</li>
 <li id="03">下一页</li>
 <li id="04">最后一页</li>
 </ul>
 <p id="select">
 <span>跳转到 </span>
 <input type="text" name="page_num">
 <span> 页 </span>
 <input type="button" name="go_btn" value="跳转">
 </p>
 </p>
</p>

The following is the css code:

#global{
 position: relative;
}
#table{
 position: absolute;
 top:19%;
 left:1.6%;
 width: 55%;
}
#table textarea{
 width: 10vw;
 height: 10vh;
 background-color: transparent;
 color: #fff;
 border-width: 0;
 text-align: center;
}
table, th, td {
 border: 0.2px solid rgba(60,166,206,0.2);
 border-collapse: collapse;
 color:rgba(60,166,206,1); 
}
th, td {
 padding: 3px;
 text-align: center;
 font-size: 1.6vmin;
}
td{
 background: rgba(2,29,54,1);
}
th{
 background: rgba(20,29,54,1);
 padding: 1.8% 0;
 color: rgba(255,255,255,0.8);
}
#footer{
 position: absolute;
 bottom:5vh;
 left:7vw;
 text-align: center;
 color: rgba(60,166,206,1);
}
#pagination{
 display: inline-block;
}
#pagination li{
 display: inline;
}
#select{
 display: inline-block;
 margin-left: 40px;
}
#select input[type="text"]{
 width: 30px;
 height: 20px;
 background-color: #000;
 border-width: 1px;
}
#select input[type="button"]{
 width: 40px;
 height: 23px;
 background: #000;
 border:none;
}
ul li{
 cursor: pointer;
}

Initialize the start date, end date, number of pages requested, number of pages requested, how many pages of data there are in total, and pass these data to the API provided by the background through ajax Data interface, and then get the data from the database, and then display it on the front end:

var start_date = "2017-01-01", end_date = "2017-01-08";
var pageNo = 1;
var pageSize = 4;
var pages = 0;

How to get the data of the table and append it to the front end? How to get paginated data and append it to frontend? Use the function we defined below:

loadData(pageNo, pageSize);

Let’s see how this function communicates with the API data interface:

function loadData(pageNo, pageSize){
 $(".detail").remove(); //每次重新从 API 数据接口获取数据都要先清除原先表格 `<tr>` 的内容
 $.ajax({
 url: "/history_alarm",
 type: "POST",
 data: JSON.stringify({date:date, page_num:pageNo, page_size:pageSize}),
 success:function(result){
 var results = JSON.parse(result);
 var list = results.alarm;
 var totalCount = results.alarm_count;
 pages = results.page_count;
 if(list.length != 0){
  for(var i=0; i<list.length; i++){
  var alarm_id = list[i].alarm_id;
  var alarm_pic = list[i].alarm_pic;
  var date = list[i].date;
  var event = list[i].event;
  var time = list[i].time;
  var remark = list[i].remark;
  appendData(alarm_id, alarm_pic, date, event, time, remark);
  addEvent(alarm_id);
  }
  $("#table").show();
  $("#footer").show();
  displayFooter(totalCount, pages, pageNo);
 } else{
  $("#table").hide();
  $("#footer").hide();
 }
 },
 error:function(){
 //error handle function
 }
 });
 }

In the loadData function, we have also defined three other functions. Next, let’s look at appendData:

//注意到我们将 `alarm_id` 作为 `<textarea>` 'class` 的值,也作为提交按钮 `id` 的值,这是因为我们要通过 ajax 将用户输入到某一个 `<textarea>` 的值作为参数传给后台 API 接口,由其写入数据库。
function appendData(alarm_id, alarm_pic, date, event, time, remark){
 var text = '<tr class="detail"><td>'+date+'</td><td>'+time+'</td<td>'+event+'</td>'+
  '<td><img class="img01" src=data:image/jpeg;base64,' + alarm_pic + '</td>'+
  '<td class="modity_btn"><textarea cols="5" rows="3" 
  class=&#39;+alarm_id+&#39;>'+remark+'</textarea>'+'<img id=&#39;+alarm_id+&#39; src="{{ 
  static_url("slice/modify.png") }}"></td></tr>';;
 $("#table table").append(text);
 }
//该函数定义了如何通过 ajax 将用户输入到某一个 `<textarea>` 的值作为参数传给后台 API 接口,并写入数据库
function addEvent(alarm_id){
 $("#"+alarm_id).click(function(){
 var remark = $("."+alarm_id).val();
 if(remark != ""){
 $.ajax({
  url:"/history_alarm",
  type:"POST",
  data:JSON.stringify({alarm_id:alarm_id, note:remark}),
  success:function(result){
  var results = JSON.parse(result);
  if(results.status == "ok"){
  console.log('ok');
  }
  }
 })
 }
 })
 }
function displayFooter(totalCount, pages, pageNo){
 var newText = '共' + totalCount + '条,' + '第' + pageNo + '页,' + '共' + pages + '页';
 $("#summary").text(newText);
 }

The function to obtain the data has been written. Next, click on the paging "Home Page, Previous Page, next page, last page, jump" events corresponding to the time. The idea is this: for each item in the paging that the user clicks on, the pageNo must be re-judged, and then the pageNo is used as a parameter to call the API interface for obtaining data again:

$("input[name='page_num']").keydown(function(e){ if(e.keyCode == 13){ $("input[name='go_btn']").click(); } });
$("input[name='go_btn']").click(function(){
 var goPage = $("input[name='page_num']").val();
 if(goPage >= 1 && goPage <=pages && goPage != pageNo){
  pageNo = goPage;
  loadData(pageNo, pageSize);
 } else{
  return false;
 }
});
$("#01").click(function(){
 pageNo = 1;
 loadData(pageNo, pageSize);
});
$("#04").click(function(){
 pageNo = pages;
 loadData(pageNo, pageSize);
});
$("#02").click(function(){
 if(pageNo == 1){
  return false;
 } else{
  pageNo--;
  loadData(pageNo, pageSize);
 }
});
$("#03").click(function(){
 if(pageNo == pages){
  return false;
 } else{
  pageNo++;
  loadData(pageNo, pageSize);
 }
});

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

Recommended reading:

Ajax+mysql to create a message board function

The most basic way for ajax to realize three-level linkage concept

The above is the detailed content of Detailed explanation of the steps to implement paging technology using ajax (with code). 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