Home  >  Article  >  Web Front-end  >  Using AJAX to implement paging function

Using AJAX to implement paging function

php中世界最好的语言
php中世界最好的语言Original
2018-03-31 13:47:371634browse

This time I will bring you the use of AJAX to implement the paging function, and what are the precautions for using AJAX to implement the paging function. The following is a practical case, let's take a look.

I recently wrote a function to add roles to user groups. It requires unadded roles on one side and added roles on the other, as well as a search function. After clicking add, ajax saves the operation.

Consider the function to be paging for the query function. The paging bar below has a total of 2*2 and 4 ajax...

The JS code is as follows:

$(document).ready(function() {
  App.init();
  currentRole(); // 当前角色
  currentRolePage();//当前角色分页
  noAddRole(); //未添加角色
  noAddRolePage();//未添加角色分页
 });
//当前角色列表
function currentRole(){
  var currentRoleCheckName =$("#currentRoleCheckName").val();
  // 当前角色的list集合
  $.ajax({
  async:true, 
  type:"POST", 
  //date:"groupId=rose",//发送到服务器的数据
  url:"${ctx}/group/ajax_showRolesForGroup.do",//请求路径
  data:{"groupId":groupId, 
  "page":page1,
  "checkName":currentRoleCheckName
  },
  dataType:"json", //返回数据的类型
  success:function(data){ //成功响应后的回调函数
  var result =data.pageSupport.items;
  console.log(data.pageSupport)
  var s="";
  for(var i in result){ 
   s+=""+result[i].name+""
   +""+result[i].remark+""
   +"";
  }
  $("#currentRole").html(s);
  }
 });
 }
//当前角色的分页
 function currentRolePage(){
  var currentRoleCheckName =$("#currentRoleCheckName").val();
  var totalPage=0;
  $.ajax({
  async:true, 
  type:"POST", 
  //date:"groupId=rose",//发送到服务器的数据
  url:"${ctx}/group/ajax_showRolesForGroup.do",//请求路径
  data:{"groupId":groupId, 
  "page":page1,
  "checkName":currentRoleCheckName
  },
  dataType:"json", //返回数据的类型
  success:function(data){ //成功响应后的回调函数
  totalPage=data.pageSupport.last;
  console.log(totalPage)
  var i= 0;
  var a="";
  for( i=page1-2; i<=page1+2;i++){
  if(i>0 && i<=totalPage){
   if(i == 1){
   $("#prev1").attr('class','disabled'); 
   }
   if(page1 == i){
   a+="
  • "+i+"
  • ";    }else{    a+="
  • "+i+"
  • ";    }   }   }   $("#fy_list").html(a);   }  });  }  //中间页   function a_method(i) {   page1 = i;   currentRole(); // 当前角色   currentRolePage();//当前角色分页  } //查询操作 function currentRoleCheck(){  page1=1;  currentRole(); // 当前角色  currentRolePage();//当前角色分页  }

    HTML The code is as follows:

    
    

     

     

      

    已选角色

     

     

       

        

        

              

      

       

        
       

       

       

                                                           
    角色名称备注信息操作
       

        

        

        

          
        

          

        

     

    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 Ajax+php performs data interaction and refreshes partial pages

    HTTP messages and Detailed explanation of ajax basic knowledge

    The above is the detailed content of Using AJAX to implement paging function. 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