codeigniter实现ajax分页

<?php
/**
 *417 add  主要是实现ajax分页
 **/
 class MY_Pagination extends CI_Pagination{
  public function __construct()
    {
        parent::__construct();
    }

  /**
   * Generate the pagination links
   *
   * @access    public
   * @return    string
   */
  function create_ajax_links()
  {
    // If our item count or per-page total is zero there is no need to continue.
    if ($this->total_rows == 0 OR $this->per_page == 0)
    {
      return ‘‘;
    }

    // Calculate the total number of pages
    $num_pages = ceil($this->total_rows / $this->per_page);

    // Is there only one page? Hm... nothing more to do here then.
    if ($num_pages == 1)
    {
      return ‘‘;
    }

    // Set the base page index for starting page number
    if ($this->use_page_numbers)
    {
      $base_page = 1;
    }
    else
    {
      $base_page = 0;
    }

    // Determine the current page number.
    $CI =& get_instance();

    if ($CI->config->item(‘enable_query_strings‘) === TRUE OR $this->page_query_string === TRUE)
    {
      if ($CI->input->get($this->query_string_segment) != $base_page)
      {
        $this->cur_page = $CI->input->get($this->query_string_segment);

        // Prep the current page - no funny business!
        $this->cur_page = (int) $this->cur_page;
      }
    }
    else
    {
      if ($CI->uri->segment($this->uri_segment) != $base_page)
      {
        $this->cur_page = $CI->uri->segment($this->uri_segment);

        // Prep the current page - no funny business!
        $this->cur_page = (int) $this->cur_page;
      }
    }

    // Set current page to 1 if using page numbers instead of offset
    if ($this->use_page_numbers AND $this->cur_page == 0)
    {
      $this->cur_page = $base_page;
    }

    $this->num_links = (int)$this->num_links;

    if ($this->num_links < 1)
    {
      show_error(‘Your number of links must be a positive number.‘);
    }

    if ( ! is_numeric($this->cur_page))
    {
      $this->cur_page = $base_page;
    }

    // Is the page number beyond the result range?
    // If so we show the last page
    if ($this->use_page_numbers)
    {
      if ($this->cur_page > $num_pages)
      {
        $this->cur_page = $num_pages;
      }
    }
    else
    {
      if ($this->cur_page > $this->total_rows)
      {
        $this->cur_page = ($num_pages - 1) * $this->per_page;
      }
    }

    $uri_page_number = $this->cur_page;

    if ( ! $this->use_page_numbers)
    {
      $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
    }

    // Calculate the start and end numbers. These determine
    // which number to start and end the digit links with
    $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
    $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;

    // Is pagination being used over GET or POST?  If get, add a per_page query
    // string. If post, add a trailing slash to the base URL if needed
    if ($CI->config->item(‘enable_query_strings‘) === TRUE OR $this->page_query_string === TRUE)
    {
      $this->base_url = rtrim($this->base_url).‘&‘.$this->query_string_segment.‘=‘;
    }
    else
    {
      $this->base_url = rtrim($this->base_url, ‘/‘) .‘/‘;
    }

    // And here we go...
    $output = ‘‘;

    // Render the "First" link
    if  ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
    {
      $first_url = ($this->first_url == ‘‘) ? $this->base_url : $this->first_url;
      $output .= $this->first_tag_open.‘<a ‘." onclick=‘ajax_page(0);return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$this->first_link.‘</a>‘.$this->first_tag_close;
    }

    // Render the "previous" link
    if  ($this->prev_link !== FALSE AND $this->cur_page != 1)
    {
      if ($this->use_page_numbers)
      {
        $i = $uri_page_number - 1;
      }
      else
      {
        $i = $uri_page_number - $this->per_page;
      }

      if ($i == 0 && $this->first_url != ‘‘)
      {
        $output .= $this->prev_tag_open.‘<a ‘." onclick=‘ajax_page(0);return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$this->prev_link.‘</a>‘.$this->prev_tag_close;
      }
      else
      {
        $i = ($i == 0) ? ‘‘ : $this->prefix.$i.$this->suffix;
        $output .= $this->prev_tag_open.‘<a ‘." onclick=‘ajax_page({$i});return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$this->prev_link.‘</a>‘.$this->prev_tag_close;
      }

    }

    // Render the pages
    if ($this->display_pages !== FALSE)
    {
      // Write the digit links
      for ($loop = $start -1; $loop <= $end; $loop++)
      {
        if ($this->use_page_numbers)
        {
          $i = $loop;
        }
        else
        {
          $i = ($loop * $this->per_page) - $this->per_page;
        }

        if ($i >= $base_page)
        {
          if ($this->cur_page == $loop)
          {
            $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
          }
          else
          {
            $n = ($i == $base_page) ? ‘‘ : $i;

            if ($n == ‘‘ && $this->first_url != ‘‘)
            {
              $output .= $this->num_tag_open.‘<a ‘." onclick=‘ajax_page(0);return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$loop.‘</a>‘.$this->num_tag_close;
            }
            else
            {
              $n = ($n == ‘‘) ? ‘‘ : $this->prefix.$n.$this->suffix;

              $output .= $this->num_tag_open.‘<a ‘." onclick=‘ajax_page({$n});return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$loop.‘</a>‘.$this->num_tag_close;
            }
          }
        }
      }
    }

    // Render the "next" link
    if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
    {
      if ($this->use_page_numbers)
      {
        $i = $this->cur_page + 1;
      }
      else
      {
        $i = ($this->cur_page * $this->per_page);
      }
      $ajax_p = $this->prefix.$i.$this->suffix;
      $output .= $this->next_tag_open.‘<a ‘." onclick=‘ajax_page({$ajax_p});return false;‘".$this->anchor_class.‘href="javascript:void(0)">‘.$this->next_link.‘</a>‘.$this->next_tag_close;
    }

    // Render the "Last" link
    if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
    {
      if ($this->use_page_numbers)
      {
        $i = $num_pages;
      }
      else
      {
        $i = (($num_pages * $this->per_page) - $this->per_page);
      }
      $ajax_p = $this->prefix.$i.$this->suffix;
      $output .= $this->last_tag_open.‘<a ‘." onclick=‘ajax_page({$ajax_p});‘". $this->anchor_class.‘href="javascript:void(0)">‘.$this->last_link.‘</a>‘.$this->last_tag_close;
    }

    // Kill double slashes.  Note: Sometimes we can end up with a double slash
    // in the penultimate link so we‘ll kill all double slashes.
    $output = preg_replace("#([^:])//+#", "\\1/", $output);

    // Add the wrapper HTML if exists
    $output = $this->full_tag_open.$output.$this->full_tag_close;

    return $output;
  }
 }

然后我们会在controller中来调用这个方法,代码如下

public function show_tpl($page=1){
    //配置分页类
    $config[‘base_url‘] = ‘/wechat_msg_template/send_message‘;
    $config[‘per_page‘] = 10;
    $config[‘uri_segment‘] = 3;
    $data[‘page_total‘] = $config[‘total_rows‘] = $this->wechat_msg_template_model->msg_template_list(); //为页总数
    $config[‘cur_tag_open‘] = ‘<a class="current">‘;
    $config[‘cur_tag_close‘] = ‘</a>‘;
    $config[‘first_link‘] = ‘«‘;
    $config[‘last_link‘] = ‘»‘;
    //获取分页
    $this->pagination->initialize($config);
    $data[‘page‘] = $this->pagination->create_ajax_links();
    if(!empty($data[‘page‘])){

    }

    $data[‘power_language‘]= $this->config->item(‘power_language‘);
    $data[‘top_item‘] = ‘msg_template_manage‘;
    $data[‘item‘]     = ‘wechat_msg_template‘;

    $list_result = $this->wechat_msg_template_model->msg_template_list(10,intval($this->uri->segment(3))); //结果
    $data[‘result‘] = $list_result;
    $this->load->view(‘show_tpl‘,$data);
  }

最后我们通过ajax 来调用 show_tpl 加载数据,达到无刷分页

javascript 代码如下:

function ajax_page(page){
  if(page==‘‘ || page==null || page==undefined){
    page = ‘‘;
  }
  content = ‘‘;
  $.ajax({
    type:‘GET‘,
    url:"/wechat_msg_template/show_tpl/"+page,
    async:false,
    success:function(data){
      content = data;
    }
  });
  $(".tab1_class").html(‘‘);

  $(".tab1_class").html(content);
  return false;
}
时间: 2024-10-05 20:15:13

codeigniter实现ajax分页的相关文章

基于jquery的ajax分页插件(demo+源码)

前几天打开自己的博客园主页,无意间发现自己的园龄竟然有4年之久了.可是看自己的博客列表却是空空如也,其实之前也有写过,但是一直没发布(然而好像并没有什么卵用).刚开始学习编程时就接触到博客园,且在博客园学习了很多的知识,看过很多人的分享.说来也惭愧,自己没能为园友们分享自己的所学所得(毕竟水平比较差). 过去的一年也是辗转了几个城市换了几份工作(注定本命年不太平?).八月份来到现在所在的公司(OTA行业),公司是做互联网的,所以可能大家的前端都屌屌的?之前一直从事.NET开发(现在在这边也是),

Django - Ajax分页

目前总结了2种方法: 1. Ajax 分页 尼玛各种google,stackoverflow,搞了好久才总结出这个,之前使用Pagination tag loading的方式不好用,并且不能进行ajax提交请求的页面无刷新的方式去分页 1.view.py 1 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 2 from django.shortcuts import render 3 def xxx

瀑布流ajax分页

index.jsp 1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 6 <title>图片&

ajax 分页完全代码整理

/* ajax分页 */var page_cur = 1; //当前页 var total_num, page_size, page_total_num;//总记录数,每页条数,总页数function getData(page) { //获取当前页数据     $.ajax({          type: 'GET',          url: 处理数据地址,          data: {              'page': page,              'catid': 

jQuery、Ajax分页

1.效果图预览 2.HTML代码 <div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12 col-xxs-12"> <table class="table table-striped table-hover table-bordered bootstrap-datatable " id="TemplateTable"> <

jquery ajax分页插件特效源代码demo完整版

原文:jquery ajax分页插件特效源代码demo完整版 源代码下载地址:http://www.zuidaima.com/share/1550463586798592.htm 网上找的,原版本没有测试数据和建表脚本啥的,我给加上了.

Pagination jquery ajax 分页参考资料

http://www.zhangxinxu.com/wordpress/2010/01/jquery-pagination-ajax%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6%E4%B8%AD%E6%96%87%E8%AF%A6%E8%A7%A3/ 个人博客参考 中文项目地址:http://www.zhangxinxu.com/jq/pagination_zh/ 原项目地址:http://plugins.jquery.com/project/pagination 版

MvcPager 概述 MvcPager 分页示例 — 标准Ajax分页 对SEO进行优化的ajax分页 (支持asp.net mvc)

该示例演示如何使用MvcPager最基本的Ajax分页模式. 使用AjaxHelper的Pager扩展方法来实现Ajax分页,使用Ajax分页模式时,必须至少指定MvcAjaxOptions的UpdateTargetId属性,该属性值即是分页后要通过Ajax来更新的 DOM 元素的 ID. Ajax.Pager()方法返回AjaxPager对象,您可以通过Ajax.Pager()方法的重载来传递PagerOptions和MvcAjaxOptions参数,也可以通过新的AjaxPager的Opti

yii下多条件多表组合查询以及自写ajax分页

多条件组合查询主要用到yii的CDbCriteria,这个类很多oem框架都有,非常好用. 前台查询表单效果是这样的,多个条件组,每个组里放多个input,name为数组.当任何一个复选框被勾选上,发起ajax请求,当然,最顶层的复选框勾上时判断是否有子项,有的话把所有的子项勾选上. 但提交一次请求会向服务端post这样一个表单 其中currentPage是隐藏字段,当分页按钮被点击是这个字段的值会发生变化,并且发起查询请求. 这个表单会提交到如下的action中进行处理 1 <?php 2 3