Codeigniter:如何使用ajax修复分页链接,单击后不更新 [英] Codeigniter: How to fix pagination links, not updating when clicked, using ajax

查看:53
本文介绍了Codeigniter:如何使用ajax修复分页链接,单击后不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ajax将分页链接放到我的网站中,以防止页面刷新.我设法使其工作,但分页链接未更新.当我单击下一页时,将加载该页面,但分页链接将保留在页面1上.我尝试将 $ this-> pagination-> create_links(); 放入变量中并回显它在一个div内,但什么也没显示.这是我的代码:

I'm trying to put pagination links into my website using ajax to keep the page from refreshing. I managed to make it work but the pagination links are not updating. When I click on the next page, the page will load but the pagination link will stay on page 1. I tried putting $this->pagination->create_links(); inside a variable and echo it inside a div but its showing nothing. Here are my codes:

型号:

public function get_posts($limit = FALSE, $rowno = FALSE)
        {
            if($limit)
            {
                $this->db->limit($limit, $rowno);
            }

            $this->db->select('*, posts.image AS post_image, posts.id AS post_id, posts.user_id AS post_user_id');
            $this->db->from('posts');
            $this->db->join('categories', 'categories.id = posts.category_id');
            $this->db->join('users', 'users.id = posts.user_id');
            $this->db->order_by('posts.created_at', 'DESC');

            $query = $this->db->get();
            return $query->result_array();
        }

控制器:

public function index()
        {
            // pagination page number
            $pageno = $this->input->post('pageno');

            // pagination config
            // $config['base_url'] = base_url()."posts/index";
            $config['base_url'] = '#';
            $config['total_rows'] = $this->db->count_all('posts');
            $config['per_page'] = 2;
            $config['num_links'] = 3;
            $config['attributes'] = array('class' => 'pagination-class');
            $config['full_tag_open'] = '<div id="paginator">';
            $config['full_tag_close'] = '</div>';

            $data['posts'] = $this->post_model->get_posts($config['per_page'], $pageno);
            $data['title'] = 'Latest Posts';

            $data['pagination'] = $this->pagination->create_links();
            $data['row'] = $pageno;

            // init pagination
            $this->pagination->initialize($config);

            $this->load->view('posts/index', $data);

        }

视图:

<div id="pagination">
            <?php echo $this->pagination->create_links(); ?>
    </div>

ajax:

$('#paginator').on('click', 'a', function(e) {
        e.preventDefault();

        var pageno = $(this).attr('data-ci-pagination-page');

        $.ajax({
            data : { 'pageno' : pageno },
            method : 'post',
            // dataType : 'json',
            url : base_url + 'posts/index',
            success : function(response) {
                $("#main_container").html(response);
            }
        });

推荐答案

可能是您使用了错误的元素ID?

May be you are using the wrong element ID?

分页X分页器?

<div id="pagination"> </div>

$('#pagination') /* .... */

这篇关于Codeigniter:如何使用ajax修复分页链接,单击后不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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