【问题标题】:records per page allow user to choose - codeigniter pagination每页记录允许用户选择 - codeigniter 分页
【发布时间】:2012-09-24 08:24:30
【问题描述】:

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

任何帮助将不胜感激。

问候,卓然

【问题讨论】:

    标签: codeigniter pagination


    【解决方案1】:

    你不应该那样做一个下拉菜单。试试这个

    查看

    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;
    

    并且在执行此操作之前不要忘记加载 form helper.. 否则你可以给正统的 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();
    

    【讨论】:

    • 没有办法让它与现有的下拉菜单一起使用吗?如果我可以使用该下拉菜单,它非常适合我。我会在一分钟内尝试你的代码,让你知道它是否有效。
    • @Zoran 有一种方法..您在id..中给出了选项值。尝试将其作为参数传递..就像...backOfficeUsers/displayAllUsers/2..并检索选项值在这样的控制器中...$limit = $this-&gt;uri-&gt;segment(3).. 你必须为此加载 url helper
    • 它部分工作。你有几分钟的时间让我们继续聊天吗?
    • if ($this->uri->segment(3) !="") { $limit = $this->uri->segment(3); } 其他 { $limit = 5; }
    • 这是做什么的,第一次打开页面时,显示5条记录。但是,当我选择 2 条记录时,它会在第二页发送给我,并显示 2 条记录。如果我点击任何其他链接,它会再次显示每页 5 条记录。
    【解决方案2】:

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

    这是控制器中的代码:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多