1

我有有效的分页。我已将限制设置为每页 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条记录。

任何帮助将不胜感激。

问候,佐兰

4

2 回答 2

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;

并且不要忘记在执行此操作之前加载表单助手。否则您可以提供正统的 htmlselect

编辑:(另一种方式

看法:

<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();
于 2012-10-03T12:03:41.233 回答
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);
    }
}

实用程序助手功能:

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

/**
 * 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

于 2014-07-09T08:05:08.633 回答