创建jquery分页? [英] create jquery pagination?

查看:122
本文介绍了创建jquery分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在CodeIgniter中实现 jQuery分页



我已经阅读了 CodeIgniter论坛 CodeIgniter AJAX分页示例/指南。 CI论坛上的人员建议使用 TOHIN的博客的解决方案,但我不明白如何做。 给我一个例子



另外,有人可以解释一下, $ this-> load- ; model('model')表示此处。 / p>

我的代码:

  function show()
{
$ this-> load-> library('Jquery_pagination');

$ this-> admin_model-> header();

$ this-> load-> library('pagination');

$ config ['base_url'] ='show';
$ config ['total_rows'] ='200';
$ config ['per_page'] ='2';
$ this-> pagination-> initialize($ config);
$ data ['pagination'] = $ this-> pagination-> create_links();

$ data ['query_select'] = $ this-> db-> query('SELECT @rownum:= @ rownum + 1 rownum,t。* FROM(SELECT @rownum:= 0 )r,hotel_submits t order by id desc');

$ this-> load-> view('admin / accommodation_submit_show',$ data);
}

对于

解决方案

您在Codeigniter论坛中看到的代码是使用渐进增强。这意味着代码的工作原理与没有javascript(尽管有一个页面刷新)。以及。因此,你的第一步是使你的分页工作与javascript关闭,然后您将能够添加AJAX功能。



现在,分页的工作原理是使用SQL的 LIMIT ,以限制每个查询的结果。 LIMIT 需要2个值,偏移量和金额,如下所示:如果要在每页显示2行,您将使用 LIMIT 0,2 。这意味着从第一行开始,并给我总共2行。然后,对于下一页,使用 LIMIT 2,2 执行查询,这意味着从第三行开始(2是第三行 - 这是一个基于零的索引)



每次查询都会这样做,这样下一个查询会有 LIMIT 4, 2 ,等等。正如你所看到的,从一个查询到下一个查询的唯一变化是偏移量(从哪个行开始查询)。



现在,Codeigniter分页类的工作原理是,它将链接中的每个页面的偏移量用 $ this-> pagination-> create_links()生成。偏移量在哪里?这由 $ config ['uri_segment'] 决定,但默认情况下设置为3.由于您没有提供完整的代码,我不知道你的控制器叫什么。让我们假设它被称为 hotels 。所以,你必须设置 $ config ['base_url'] ='hotels / show'; 。如您所见,您的第一个URI段是 hotels ,第二个是 show ,第三个是分页类为您产生的偏移。



如何获得该偏移量?我很高兴你问。通过简单地让你的 show 方法接受一个参数,Codeigniter会传递它的第三个URI段。所以我们得到如下:

  function show($ offset = 0)
{
$ this- > load-> library('pagination');

$ config ['base_url'] ='hotels / show'; //将此更改为您的控制器名称
$ config ['total_rows'] ='200'; //实际上,这应该是你的表中的实际行数
$ config ['per_page'] ='2';

$ this-> pagination-> initialize($ config);
$ data ['pagination'] = $ this-> pagination-> create_links();

$ offset =(int)$ offset; //只是为了确保这里没有什么有趣的东西
$ data ['query_select'] = $ this-> db-> query(SELECT @rownum:= @ rownum + 1 rownum,t。 。
FROM(SELECT @rownum:= 0)r,hotel_submits t。
ORDER BY id desc LIMIT $ offset,2);
//我只是把它拆分成行,所以SO不会让你滚动太多:-)

$ this-> load-> view('admin / accommodation_submit_show' ,$ data);
}

现在,您的分页链接应该像广告一样工作,页面刷新。要解决这个问题,请执行以下操作:

  $(function(){
//假设您的分页链接
//在一个分页ID的分页,
//和你想分页一个表的分页
$('#pagination a')。live('click' function(){
$ .get($(this).attr('href'),function(html){

$('table#paginate')。replaceWith html).find('table#paginate'));
$('#pagination')。replaceWith($(html).find('#pagination'));
});
return false;
});
});

所有这些都应该可以工作。但是,我们可以改进这一点。在代码的当前状态,我们得到html的每一次点击的完整页面,只是过滤掉我们想要的。



它们在论坛上正在做什么,是创建另一个控制器专为



如果你需要帮助,请告诉我。


I need to implement jQuery pagination in CodeIgniter.

I've read some posts on CodeIgniter forum and CodeIgniter AJAX Pagination Example/Guideline. People on CI forum suggest using a solution from TOHIN's blog, but I can't understand how it could be done. Could someone give me an example?

Also, could someone explain, what $this->load->model('model') means here.

My code:

function show() 
    {
    $this->load->library('Jquery_pagination');

        $this->admin_model->header();

        $this->load->library('pagination');

        $config['base_url'] = 'show';
        $config['total_rows'] = '200';
        $config['per_page'] = '2'; 
        $this->pagination->initialize($config); 
        $data['pagination'] = $this->pagination->create_links();

        $data['query_select'] = $this->db->query('SELECT @rownum:=@rownum+1 rownum, t.* FROM (SELECT @rownum:=0) r, hotel_submits t order by id desc');

        $this->load->view('admin/accommodation_submit_show', $data);   
    }

With respect

解决方案

The code you are looking at in the Codeigniter forums is using progressive enhancement. What that means is that the code works just as well without javascript (although with a page refresh).

So, the first step for you is to make your pagination work with javascript off, and then you'll be able to add the AJAX functionality.

Now, the way pagination works is that you use SQL's LIMIT in your query to limit the results per query. LIMIT takes 2 values, the offset and the amount, as follows: if you want to show 2 rows per page, you'll query the database with LIMIT 0, 2. This means "start with the first row, and give me 2 rows in total". Then, for the next page, you do your query with LIMIT 2, 2, which means "start with the third row (2 is the third row - it's a zero based index) and give me 2 rows in total".

You'll do this with every query, so that the next query will have LIMIT 4, 2, and so on and so forth. As you can see, the only thing that changes from one query to the next is the offset (which row to start the query from).

Now, the way the Codeigniter Pagination class works, is that it puts the offset to each page within the links that it generates with $this->pagination->create_links(). Where does the offset go? That's determined by $config['uri_segment'], but by default it's set to 3. Since you have not provided your full code, I do not know what your controller is called. Let's assume it's called hotels. So, you have to set $config['base_url'] = 'hotels/show';. As you can see, your first URI segment is hotels, the second one is show, and the third one would be the offset that the Pagination class has generated for you.

How do you get to that offset? I'm glad you asked. By simply having your show method take an argument, Codeigniter will pass it the 3rd URI segment. So we get the following:

function show($offset = 0) 
{
    $this->load->library('pagination');

    $config['base_url'] = 'hotels/show'; // Change this to your controller name
    $config['total_rows'] = '200'; // In reality, this should be the actual number of rows in your table
    $config['per_page'] = '2'; 

    $this->pagination->initialize($config); 
    $data['pagination'] = $this->pagination->create_links();

    $offset = (int) $offset; // just to make sure nothing funky gets in here
    $data['query_select'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.* ".
        "FROM (SELECT @rownum:=0) r, hotel_submits t ".
        "ORDER BY id desc LIMIT $offset, 2");
    // I just split it into lines so that SO doesn't make you scroll too much :-)

    $this->load->view('admin/accommodation_submit_show', $data);   
}

Now your pagination links should work as advertised, but they'll do a complete page refresh. To get around that, do this:

$(function() {
    // Assuming your pagination links
    // are in a div with ID of pagination,
    // and that you want to paginate a table with ID of paginate
    $('#pagination a').live('click', function() {
        $.get( $(this).attr('href'), function(html) {

            $('table#paginate').replaceWith( $(html).find('table#paginate') );
            $('#pagination').replaceWith( $(html).find('#pagination') );
        });
        return false;
    });
});

All of this should work. However, we can improve upon this. In the code's current state, we are getting the html for the full page with each click, and just filtering out what we want. Although this works, there's no need to send so much extra information that is not needed.

What they're doing on the forums there, is creating another controller specifically tailored for these ajax requests, so that it'll only serve the relevant table html and nothing more.

If you need help with that, let me know.

这篇关于创建jquery分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆