Codeigniter Bootstrap Pagination

2020-05-29 03:00发布

问题:

I am making a web app with CodeIgniter and Twitter Bootstrap. I found a resource online with a list of $config settings to style the pagination links properly. Is there a way to store this in a library and load it into the controller so I dont have to type it each time?

回答1:

You can save this as pagination.php in application/config:

$config['per_page'] = 2;
$config['uri_segment'] = 3;
$config['num_links'] = 9;
$config['page_query_string'] = TRUE;

$config['query_string_segment'] = 'page';

$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';

$config['first_link'] = '&laquo; First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';

$config['last_link'] = 'Last &raquo;';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';

$config['next_link'] = 'Next &rarr;';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';

$config['prev_link'] = '&larr; Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';

$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';

$config['anchor_class'] = 'follow_link';


回答2:

Very easy solution for bootstrap and codeigniter

$this->load->model('reciever');
                $this->load->library('uri');
                $this->load->library('pagination');
                $config['base_url'] = base_url(). 'users_ci/users';
                $config['total_rows'] = $this->reciever->getRows();
                $config['per_page'] = 4;
                $config['full_tag_open'] = '<ul class="pagination">';
                $config['full_tag_close'] = '</ul>';            
                $config['prev_link'] = '&laquo;';
                $config['prev_tag_open'] = '<li>';
                $config['prev_tag_close'] = '</li>';
                $config['next_link'] = '&raquo;';
                $config['next_tag_open'] = '<li>';
                $config['next_tag_close'] = '</li>';
                $config['cur_tag_open'] = '<li class="active"><a href="#">';
                $config['cur_tag_close'] = '</a></li>';
                $config['num_tag_open'] = '<li>';
                $config['num_tag_close'] = '</li>';
                $config["num_links"] = round( $config["total_rows"] / $config["per_page"] );
                $config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
                $this->pagination->initialize($config);
                $config['pages'] = $this->pagination->create_links();
                $this->load->view('users',$config);

and for view you have

<div><?php echo $pages; ?></div>


回答3:

You can always simply create a .php file with a settings array and require_once() it in your model and return to the pagination initiator. Or it's better to create a model/library that will return all the settings to you.

Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$pagination_config = array(
    'full_tag_open' = '<p>',
    'full_tag_close' = '</p>',
    // ...
);

class Pagiconf extends CI_Model {
    public function load() {
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }
}

Anywhere you need it:

$this->load->model('pagiconf');
echo $this->pagiconf->load();


回答4:

      The bellow working fine 



       $config["base_url"] = base_url() . "product/product/index1";
        $config["total_rows"] = $count_products;    

  $config['per_page'] = 2;
$config['uri_segment'] = 4;
$config['num_links'] = 9;

$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';

$config['first_link'] = '&laquo; First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';

$config['last_link'] = 'Last &raquo;';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';

$config['next_link'] = 'Next &rarr;';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';

$config['prev_link'] = '&larr; Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';

$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';



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


回答5:

Here is my working code for Bootstrap v4.0

$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] ='</ul>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['next_tagl_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tagl_close'] = '</li>';
$config['first_tag_open'] = '<li class="page-item disabled">';
$config['first_tagl_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['last_tagl_close'] = '</a></li>';
$config['attributes'] = array('class' => 'page-link');


标签: codeigniter