每页记录允许用户选择 - codeigniter分页

时间:2012-10-03 11:55:57

标签: codeigniter pagination

我的分页有效。我已将限制设置为每页5条记录,但我希望用户能够在需要时更改它。问题是我不知道该怎么做。

在视图中,我创建了下拉菜单,因此用户可以选择每页要查看的记录数:

 <ul class="dropdown-menu">
    <li>
        <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="2" class="pPage" data-tableid="smpl_tbl">
        2 records per page
        </a>
    </li>
    <li>
        <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers"  id ="50" class="pPage" data-tableid="smpl_tbl">
        50 records per page
        </a>
    </li>
    <li><a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="100" class="pPage" data-tableid="smpl_tbl">
        100 records per page
        </a>
    </li>
    <li>
        <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="all" class="pPage" data-tableid="smpl_tbl">
        Display all records
        </a>
    </li>
</ul>

在我的控制器中,我有以下代码:

public function displayAllUsers()
    {


        $recordsPerPage = 5;
        $limit = $recordsPerPage;
        $offset = 3;

        $offset = $this->uri->segment(3);
        $this->db->limit($limit, $offset);

        $data['users'] = $this->backOfficeUsersModel->get();

        $totalresults = $this->db->get('back_office_users')->num_rows();

        //initializing & configuring paging
        $this->load->library('pagination');
        $config['base_url'] = site_url('/backOfficeUsers/displayAllUsers');
        $config['total_rows'] = $totalresults;
        $config['per_page'] = $limit;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';
        $config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

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



        $data['main_content'] = 'bousers/users';
        $data['title'] = 'Back Office Users';
        $errorMessage = FALSE;


        $this->load->vars($data,$errorMessage);
        $this->load->vars($currentUser);
        $this->load->view('backOffice/template');


    } // end of function displayAllUsers

有人能告诉我如何显示用户从下拉菜单中选择的记录数量?如果他没有选择任何内容,我希望默认显示5条记录。

任何帮助都将深表感谢。

的问候,卓然

2 个答案:

答案 0 :(得分:3)

你不应该像那样下拉..试试这个

查看

echo form_open('controller/displayAllUsers');
            $options = array(
                            '' => 'Select',
                             '2' => '2',
                             '50' => '50',
                             '100' => '100');
            echo form_dropdown('sel',$options,'');
echo form_submit('submit',Submit);

<强>控制器

if(isset($post['sel']) && !empty($post['sel']))
                $config['per_page'] = $post['sel'];
                else
                $config['per_page'] = 5;

在执行此操作之前不要忘记加载表单助手 ..否则你可以给正统的html select

编辑: (另一种方式

查看:

<ul class="dropdown-menu">
<li>
    <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/2"  class="pPage" data-tableid="smpl_tbl">
    2 records per page
    </a>
</li>
<li>
    <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/50"  class="pPage" data-tableid="smpl_tbl">
    50 records per page
    </a>
</li>
<li><a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/100"  class="pPage" data-tableid="smpl_tbl">
    100 records per page
    </a>
</li>
<li>
    <a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="all" class="pPage" data-tableid="smpl_tbl">
    Display all records
    </a>
</li>

控制器:

public function displayAllUsers()
{

$currentUser = $this->isLoggedIn();
$this->load->model('backOfficeUsersModel');
$this->db->order_by('userid');
//$recordsPerPage = 5;
//$limit = $recordsPerPage;
if ($this->uri->segment(3) !="") {
$limit = $this->uri->segment(3);
} else {
$limit = 5;
}

$offset = 4;

$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);

$data['users'] = $this->backOfficeUsersModel->get();

答案 1 :(得分:0)

我也实现了相同的代码,它也适用于我的其他页面。

以下是控制器中的代码:

class Test extends CI_Controller {

    public $limit = 2;
    public $data = array();
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        if($this->input->post('paging_limit')){
            $this->limit = $this->input->post('paging_limit');
        }
    }


    public function index()
    {

        $this->load->library('pagination');
        $this->limit = ($this->uri->segment(4)) ? $this->uri->segment(4) : $this->limit;
        $config['base_url'] = site_url('test/index/' . $this->limit);
        $config['uri_segment'] = 5;
        $config['per_page'] = $this->limit;

        $offset = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
        $this->data['page_offset'] = $offset;

        $this->data['test_list'] = $this->test_model->get_test_list($this->limit, $offset);

        $found_rows =  $this->test_model->get_found_rows();
        $config['total_rows'] = $found_rows;

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

        $this->data['paging_info'] =  get_paging_info($offset, $this->limit, $fetched_rows, $found_rows);

        $this->data['paging_limit_dropdown'] =  get_paging_limit_options('test/', $this->limit);
        $this->load->view('test', $this->data);
    }
}

UTILITY Helper功能:

// ------------------------------------------------------------------------

/**
 * get_paging_info
 *
 * Returns the pagination info
 * @param int
 * @param int
 * @param int
 * @access  public
 * @return  string
 */
if ( ! function_exists('get_paging_info'))
{
    function get_paging_info($offset, $limit, $fetched_rows, $found_rows)
    {
        $X = $offset + 1;
        $Y = $offset + $limit;
        if($fetched_rows < $limit)
        {
            $Y -= ($limit - $fetched_rows); 
        }
        $CI =& get_instance();
        $CI->lang->load('common', 'english');
        return sprintf($CI->lang->line('common_paging_info'), $X , $Y, $found_rows);
    }
}


// ------------------------------------------------------------------------

/**
 * get_paging_limit_options
 *
 * Returns the pagination limit drop down 
 * @param int
 * @param int
 * @param int
 * @access  public
 * @return  string
 */
if ( ! function_exists('get_paging_limit_options'))
{
    function get_paging_limit_options($url, $limit = '')
    {
        $CI =& get_instance();
        $CI->load->helper('form');
        $dropdown = form_open($url);
        $options = array(
                    '' => 'Select',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6' => '6',
                    '10' => '10',
                    '20' => '20'
                    );
        $dropdown .= form_dropdown('paging_limit', $options, $limit);
        $dropdown .= form_submit('submit', 'Go');
        return $dropdown;
    }
}

公共语言代码:

$lang['common_paging_info']         = "Showing %s to %s of %s entries";

查看档案:

<div class="row-fluid">
    <div class="span6">
        <div class="pagination">{$paging_info}</div>
    </div><!--/span-->

    <div class="span6">
    {$paging_limit_dropdown}
    {$pagination}    
    </div><!--/span-->
</div><!--/row-->

@注意,我正在使用Smarty模板,用PHP echo替换上面的变量