html ajax分页,使用jQuery和Ajax请求实现分页效果

本文介绍了一个使用JavaScript实现的分页脚本Page.js,它包含获取总页数、切换页面等功能,并与后台进行Ajax数据交互。在后台OrderAction中,通过Ajax Dish方法根据条件获取并返回数据。
摘要由CSDN通过智能技术生成

在要求分页的程序中,我们一般会知道如下几个数据的参数:

总记录数count

每一页要显示的记录数:size

当前页:num

总页数我们可以通过count和size得到,在JS中可以使用Math.ceil()

编写我们的JS分页脚本page.js  //定义一个Page函数,接收两个参数,总记录数和参数列表var Page = function(count,params){  this.size=10;//初始化每一页显示10条数据  this.num=1;//当前页为第一页  this.count=count;  this.url;//定义一个url  this.cacheKey=Math.random();//定义一个随机数,保证在ajax传输的时候唯一地址,防止缓存提交  this.sumNum = function(){//得到总页数  return Math.ceil(this.count/this.size);  };  this.params = {};//定义一个参数列表  this.first=function(){//首页  this.show(1);  }  this.last=function(){//最后一页  this.show(this.sumNum());  }  this.next=function() {//下一页  this.show(this.num + 1);  }  this.prev=function() {//上一页  this.show(this.num - 1);  }  this.show=function(num) {//处理页面函数  if(num>0&&num<=this.sumNum()){  this.num = num;                          //构造页面地址参数                          var args = {'pager.offset':(this.num-1)*this.size,'page.size':this.size,'_temp':this.cacheKey};  for(p in this.params){  args[p] = this.params[p];  }  $.get(this.url,args,function(data){  showJson(data);//将异步请求的json数据显示出来  });  delete args;  }  }  this.reload=function(){//重新加载  this.clearCahce();//清除缓存  if(num>0&&num<=this.sumNum()){  this.show(this.num);  }else{  this.show(1);  }  }  this.clearCahce=function(){  this.cacheKey=Math.random();//改变key的值  }  this.bindForm=function(id){//绑定到表单中中  $(function(){  $('#'+id+' input[type=text]').each(function(i,input){  if($(input).val()!=''){  page.params[$(input).attr('name')] = $(input).val();  }  });  $('#'+id+' input:checked').each(function(i,input){  if($(input).val()!=''){  page.params[$(input).attr('name')] = $(input).val();  }  });  $('#'+id+' select').each(function(i,s){  if($(s).find('option:selected').val()!=''){  page.params[$(s).attr('name')] = $(s).find('option:selected').val();  }  });  $('#'+id).bind('submit',function(f){  for(var i=0;i

          New Document                               
编号姓名年龄性别
${users.userId}${users.userName}${users.userAge}${users.userSex}
         当前页: 0/ 0 页                 首页          上页                 下页          末页                总共 条      
         调用page.js和编写我们的脚本语句       我们在OrderAction中编写我们的ajaxDish方法    /**   * Ajax 获取菜谱   */  public String ajaxDish() throws Exception{  PrintWriter writer = response.getWriter();  try{  int resId = Integer.parseInt(request.getParameter("resId"));  int preId = Integer.parseInt(request.getParameter("preId"));  PageModel pageModel = null;  if(preId==0){ //查询全部  pageModel = restaurantDishDao.findPageModelDishes(resId);  }else{  pageModel = restaurantDishDao.findPageModelDishesOfPreferentialId(resId, preId);  }  JSONObject object = new JSONObject();  object.accumulate("resId", resId);  object.accumulate("preId", preId);  object.accumulate("count", pageModel.getCount());  object.accumulate("data", pageModel.getDatas());  response.setContentType("text/plain");  response.setCharacterEncoding("UTF-8");  writer.print(object.toString());  }catch(Exception e){  throw e;  }finally{  writer.flush();  writer.close();  }  return null;  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值