2012-10-03 45 views
1

我有分頁工作。我已將限制設置爲每頁5條記錄,但我希望用戶能夠在需要時進行更改。問題是我不知道該怎麼做。每頁記錄允許用戶選擇 - codeigniter分頁

在視圖中,我創建了下拉菜單,讓用戶可以選擇他多少記錄想要每頁看到:

<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條記錄。

任何幫助將深表謝意。

問候,卓然

回答

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(); 
+0

有沒有辦法使它與現有的下拉菜單一起工作?如果我可以使用該下拉菜單,它非常適合我。我會在一分鐘內嘗試你的代碼,並讓你知道它是否有效。 – Zoran

+0

@Zoran有一種方法..你已經在'id'中給出了選項值..嘗試將它作爲參數傳遞..就像...'backOfficeUsers/displayAllUsers/2'...並檢索控制器中的選項值這個......'$ limit = $ this-> uri-> segment(3)'..你必須爲這個 –

+0

加載_url helper_它部分的工作。你有幾分鐘的時間,我們可以聊天嗎? – Zoran

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