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

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

问题描述

我需要在 CodeIgniter 中实现 jQuery 分页.

I need to implement jQuery pagination in CodeIgniter.

我在 CodeIgniter 论坛CodeIgniter AJAX 分页示例/指南.CI 论坛上的人建议使用 TOHIN 的博客中的解决方案,但我不明白这是怎么做到的.有人可以举个例子吗?

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?

另外,有人可以解释一下,$this->load->model('model') 是什么意思 此处.

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

我的代码:

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 的情况下也能正常工作(尽管需要刷新页面).

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).

因此,对您而言,第一步是在关闭 javascript 的情况下进行分页,然后您就可以添加 AJAX 功能了.

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.

现在,分页的工作方式是在查询中使用 SQL 的 LIMIT 来限制每个查询的结果.LIMIT 接受 2 个值,偏移量和数量,如下所示:如果您想每页显示 2 行,您将使用 LIMIT 0, 2 查询数据库.这意味着从第一行开始,总共给我 2 行".然后,对于下一页,您使用 LIMIT 2, 2 进行查询,这意味着从第三行开始(2 是第三行 - 它是一个基于零的索引)并给我 2 行总共".

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".

您将对每个查询执行此操作,以便下一个查询具有 LIMIT 4, 2,依此类推.如您所见,从一个查询到下一个查询的唯一变化是偏移量(从哪一行开始查询).

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).

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

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.

你是如何达到这个偏移的?我很高兴你问.通过简单地让您的 show 方法接受一个参数,Codeigniter 会将第三个 URI 段传递给它.所以我们得到以下内容:

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;
    });
});

所有这些都应该有效.但是,我们可以改进这一点.在代码的当前状态下,我们每次点击都会获取整个页面的 html,并过滤掉我们想要的内容.虽然这有效,但没有必要发送这么多不需要的额外信息.

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.

他们在那里的论坛上所做的是创建另一个专门为这些 ajax 请求量身定制的控制器,以便它只提供相关的表格 html,仅此而已.

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天全站免登陆