隔了几年又接触了CI框架,需要做个AJAX分页,以前也就是懂个皮毛,现在是现学活用了。


首先来个正常分页的demo

public function querylog(){
    $data = [];

    if(!empty($_GET["stime"])){
        $current_url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }
    else{
        $current_url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?noparam=1';
    }

    !empty($_GET["user"]) && $this->db->where("user",$_GET["user"]);
    !empty($_GET["host"]) && $this->db->where("host",$_GET["host"]);
    $stime = !empty($_GET["stime"])? $_GET["stime"]: date('Y-m-d H:i',time()-3600*24*30);
    $etime = !empty($_GET["etime"])? $_GET["etime"]: date('Y-m-d H:i',time()+60);
    $this->db->where("create_time >=",$stime);
    $this->db->where("create_time <=",$etime);

    // 分页
    $this->load->library('pagination');
    $config['base_url'] = $current_url;
    $config['total_rows'] = $this->log->get_total_rows();
    $config['per_page'] = 30;
    $config['num_links'] = 5;
    $config['page_query_string'] = TRUE;
    $config['use_page_numbers'] = TRUE;
    $this->pagination->initialize($config);
    $offset = !empty($_GET['per_page']) ? $_GET['per_page'] : 1;

    !empty($_GET["user"]) && $this->db->where("user",$etime);
    $this->db->order_by("id","desc");
    $data['datalist'] = $this->log->get_total_record_paging($config['per_page'],($offset-1)*$config['per_page']);

    $setval["host"]=isset($_GET["host"]) ? $_GET["host"] : "";
    $setval["user"]=isset($_GET["user"]) ? $_GET["user"] : "";
    $setval["stime"]=$stime;
    $setval["etime"]=$etime;
    $data["setval"]=$setval;

    $this->layout->view("inception/querylog",$data);
}

接下来,就是正题了,去system/libraries/Pagination.PHP添加个create_ajax_links方法
/**
 * 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 ($_REQUEST['page'] != $base_page)
        {
            $this->cur_page = $_REQUEST['page'];

            // 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;
}
其实也很简单,大致就是把create_links方法里的啊标签里的html改了下,借鉴TP的。

CI支持AJAX分页的更多相关文章

  1. Html5原创俄罗斯方块(基于canvas)

    这篇文章主要介绍了Html5原创俄罗斯方块(基于canvas)的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. swift 数据类型转换 string 转换为 int, int转换为string 等等 string转换为nsmutablestring

    bytheway:刚接触swift不就,学到的东西比较浅~~,但是有问题可以在评论里说说,我每天上,一起成长~~早晚小牛长成大牛,~more牛~~~牛的立方~~gogo

  3. 【swift_1】swift基本语法及事例Demo

    语法类的文档网上比较多,我这里参考:Swift基本语法事例Demo:链接:http://pan.baidu.com/s/1jGCINCq密码:5mdk语法须知2个不需要不需要编写main函数:全局作用域中的代码会被自动当做程序的入口点(从上往下执行)不需要在每一条语句后面加上分号letradius=10你喜欢的话,也可以加上letradius=10;有一种情况必须加分号:同一行代码上有多条语句时1

  4. 数组的enumerate(swift)

    数组的enumerateby伍雪颖letarr:Array=[1,2,216)">3,216)">4,216)">5]varresult=0forinenumerate{result+=num}println

  5. swift语言的学习笔记十二(初始化方法)

    所以Swift有了超级严格的初始化方法。与designated初始化方法对应的是在init前加上convenience关键字的初始化方法。所有的convenience初始化方法都必须调用同一个类中的designated初始化完成设置,另外convenience的初始化方法是不能被子类重写或从子类中以super的方式被调用的。这样的一个最大的好处是可以保证依赖于某个designated初始化方法的convenience一直可以被使用。这在要求子类不直接使用父类中的convenience初始化方法时会非常有帮

  6. Swift 柯里化(currying)和反柯里化(uncurrying)

    //DemoofcurryingfuncaddTwoNums(a:Int)(num:Int)->Int{returna+num}letaddToFour=addTwoNums(4)letresult=addToFour(num:6)print("result:\(result)")funcgreaterThan(comparor:Int)(input:Int)->Bool{returninput>

  7. 从零学习Swift&lt;2&gt;

    ,可以强行解包注意:必须要确保解包后的值不是nil,否则会报错常见错误unexpectedlyfoundnilwhileunwrappinganOptionalvalue翻译在[解包]一个可选值时发现nil??运算符可以用于判断变量/常量的数值是否是nil,如果是则使用后面的值替代在使用Swift开发时,??

  8. 挺好的一篇总结文(等有空时看看)

    最近几天的感受是,Swift并不像我上一篇表达自己初步看法的文章里所说的那样,相对于objc来说有更好的学习曲线。另外一种类型是复合类型,包括函数和多元组。它们在使用的时候不会被命名,而是由Swift内部自己定义。在Swift的世界中,一切看得到的东西,都一定属于某一种类型。这也正式Swift的类型的一种很常见的组织方式。而当我们检查到Array的时候,发生了一点神奇的事情。这里的原因其实在Apple的官方文档中有一些说明。

  9. Swift 338. Counting Bits

    Givenanonnegativeintegernumbernum.Foreverynumbersiintherange0≤i≤numcalculatethenumberof1'sintheirbinaryrepresentationandreturnthemasanarray.Example:Fornum=5youshouldreturn[0,1,2,2].题意:给定num,计算从0到num中的

  10. swift 中常用的进制转换

随机推荐

  1. xe-ajax-mock 前端虚拟服务

    最新版本见Github,点击查看历史版本基于XEAjax扩展的Mock虚拟服务插件;对于前后端分离的开发模式,ajax+mock使前端不再依赖后端接口开发效率更高。CDN使用script方式安装,XEAjaxMock会定义为全局变量生产环境请使用xe-ajax-mock.min.js,更小的压缩版本,可以带来更快的速度体验。

  2. vue 使用 xe-ajax

    安装完成后自动挂载在vue实例this.$ajaxCDN安装使用script方式安装,VXEAjax会定义为全局变量生产环境请使用vxe-ajax.min.js,更小的压缩版本,可以带来更快的速度体验。cdnjs获取最新版本点击浏览已发布的所有npm包源码unpkg获取最新版本点击浏览已发布的所有npm包源码AMD安装require.js安装示例ES6Module安装通过Vue.use()来全局安装示例./Home.vue

  3. AJAX POST数据中文乱码解决

    前端使用encodeURI进行编码后台java.net.URLDecoder进行解码编解码工具

  4. Koa2框架利用CORS完成跨域ajax请求

    实现跨域ajax请求的方式有很多,其中一个是利用CORS,而这个方法关键是在服务器端进行配置。本文仅对能够完成正常跨域ajax响应的,最基本的配置进行说明。这样OPTIONS请求就能够通过了。至此为止,相当于仅仅完成了预检,还没发送真正的请求呢。

  5. form提交时,ajax上传文件并更新到&lt;input&gt;中的value字段

  6. ajax的cache作用

    filePath="+escape;},error:{alert;}});解决方案:1.加cache:false2.url加随机数正常代码:网上高人解读:cache的作用就是第一次请求完毕之后,如果再次去请求,可以直接从缓存里面读取而不是再到服务器端读取。

  7. 浅谈ajax上传文件属性contentType = false

    默认值为contentType="application/x-www-form-urlencoded".在默认情况下,内容编码类型满足大多数情况。在这里,我们主要谈谈contentType=false.在使用ajax上传文件时:在其中先封装了一个formData对象,然后使用post方法将文件传给服务器。说到这,我们发现在JQueryajax()方法中我们使contentType=false,这不是冲突了吗?这就是因为当我们在form标签中设置了enctype=“multipart/form-data”,

  8. 909422229_ajaxFileUpload上传文件

    ajaxFileUpload.js很多同名的,因为做出来一个很容易。我上github搜AjaxFileUpload出来很多类似js。ajaxFileUpload是一个异步上传文件的jQuery插件传一个不知道什么版本的上来,以后不用到处找了。语法:$.ajaxFileUploadoptions参数说明:1、url上传处理程序地址。2,fileElementId需要上传的文件域的ID,即的ID。3,secureuri是否启用安全提交,默认为false。4,dataType服务器返回的数据类型。6,error

  9. AJAX-Cache:一款好用的Ajax缓存插件

    原文链接AJAX-Cache是什么Ajax是前端开发必不可少的数据获取手段,在频繁的异步请求业务中,我们往往需要利用“缓存”提升界面响应速度,减少网络资源占用。AJAX-Cache是一款jQuery缓存插件,可以为$.ajax()方法扩展缓存功能。

  10. jsf – Ajax update/render在已渲染属性的组件上不起作用

    我试图ajax更新一个有条件渲染的组件。我可以确保#{user}实际上是可用的。这是怎么引起的,我该如何解决呢?必须始终在ajax可以重新呈现之前呈现组件。Ajax正在使用JavaScriptdocument.getElementById()来查找需要更新的组件。但是如果JSF没有将组件放在第一位,那么JavaScript找不到要更新的内容。解决方案是简单地引用总是渲染的父组件。

返回
顶部