【问题标题】:how to create pagination and sorting with search functionality in codeigniter如何在 codeigniter 中使用搜索功能创建分页和排序
【发布时间】:2015-12-18 13:46:02
【问题描述】:

我想创建带有分页和排序的搜索功能。但我不知道如何在 CodeIgniter 中做到这一点。以下是我的控制器功能:

function view($page = 1,$sort='emp_id',$order='asc'){
    $data['title']='View Employee';
    $data['active']='Employee';
    $search=$this->input->post('search');        
    $num_rows=$this->employeeModel->empCount();
    $config['base_url'] ='employee/view';       
    $config['total_rows'] = $num_rows;
    $config['per_page'] =5;              
    $this->pagination->initialize($config);          
    $arr=array(
        'limit'      =>$config['per_page'],
        'start'      =>(($page-1) * $config['per_page']),
        'sort'       =>$sort,
        'order'      =>$order,         
        'search'     =>$search           
        );        
    $data['search']=$search;         
    $data['empDetails']=$this->employeeModel->getEmp($arr);        
    ($this->input->is_ajax_request())? $this->load->view('ajaxFiles/viewEmp',$data): $this->template->load('templates/template','HR-Admin/viewEmp',$data);        
}

以下是查看代码

   <form action="<?php echo base_url().'employee/view' ?>" method="GET">
                <input  type="search" name="search" id="search" value="<?php if(!empty($search)){echo $search;} ?>" placeholder="Search" />
                <input  type="submit" name="" value="Search" class="btn btn-primary" />
        </form>

<table id="emp_table" width="100%" border="1" class="table-condensed table-bordered table-striped table-responsive">                
            <thead>
                <?php if($this->session->userdata('emp_role')=='Admin'){ ?>
                <tr>
                   <form action="<?php echo base_url().'employee/delete'; ?>" name="multiple_delete"  method="post" >      
                       <td colspan="10"><input type="submit" name="delete" id="delete" class="btn btn-danger" style="float: right" value="Delete All" /></td>                       
                </tr>
                <?php } ?>
                <tr class="ajax_request">
                    <th>#</th>
                    <th><a href="<?php echo base_url().'employee/view/'.$page_num.'/first_name/'.$order; ?>">Employee Name</a></th>
                    <th><a href="<?php echo base_url().'employee/view/'.$page_num.'/emp_role/'.$order;?>">Employee Role</a></th>
                    <th><a href="<?php echo base_url().'employee/view/'.$page_num.'/shift/'.$order; ?>">Employee Shift</a></th>
                    <th><a href="<?php echo base_url().'employee/view/'.$page_num.'/emp_position_id/'.$order; ?>">Employee Designation</a></th>
                    <th>Profile Picture</th>
                    <th>Action</th>                                
                </tr>
            </thead>
            <tbody id="values">
        <?php  $cur_page = $this->uri->segment(3) ? intval($this->uri->segment(3)) : 1;
               $i = (($cur_page-1) * 5) +1;
            foreach($empDetails as $val){ ?>
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo ucfirst($val['first_name']).' '.ucfirst($val['last_name']); ?></td> 
                    <td><?php echo $val['emp_role']; ?></td> 
                    <td><?php echo $val['shift_type']; ?></td>  
                    <td><?php echo $val['position']; ?></td>  
                    <?php if(empty($val['profile_img'])){ ?>
                    <td><img src="<?php echo base_url().'images/user_image.png'; ?>" alt="Profile Img" width="80px" height="80px" /></td>
                    <?php } else{ ?>
                    <td><img src="<?php echo base_url().$val['profile_img']; ?>" alt="Profile Img" width="80px" height="80px" /></td>
                    <?php } ?>
                    <td>
                        <a href="<?php echo base_url().'employee/empProfile/'.$this->encrypt->encode($val['emp_id']); ?>"><button class="btn btn-info">View Profile</button></a>
                        <a title="Edit" href="<?php echo base_url().'employee/edit/'.$this->encrypt->encode($val['emp_id']); ?>"><button class="btn btn-default"><i class="glyphicon glyphicon glyphicon-pencil"></i></button></a>
                    <?php if($this->session->userdata('emp_role')=='Admin') { ?>                                
                        <a title="Delete" onclick="return confirm_delete()" href="<?php echo base_url().'employee/delete/'.$this->encrypt->encode($val['emp_id']); ?>"><button class="btn btn-default"><i class="glyphicon glyphicon glyphicon-trash"></i></button></a>                   
                        <input type="checkbox" name="multi_del[]" id="multi_del" value="<?php echo $val['emp_id']; ?>" />                            
                    <?php } ?>                           
                    </td> 
                </tr>
        <?php $i++;  } ?>
            </tbody>
             </form>
        </table>
        <ul class="ajax_request"> <?php echo  $this->pagination->create_links(); ?></ul> 

下面是我的ajax分页脚本

<script>
$(document).ready(function() {
    $(".ajax_request a").click(function() {
        $("#resultDiv").load(this.href);
        return false;
    });         
  });       
</script>

谁能帮我做这个?

提前致谢。

【问题讨论】:

  • 我用谷歌搜索过,但找不到任何合适的解决方案...
  • 为此使用数据表。
  • thanks@pramod 我知道数据表,但我想在不使用数据表的情况下做到这一点
  • 请同时发布您的视图代码,因为您需要在那里创建分页链接。我没有看到你在任何地方加载分页库,所以我假设你在构造函数中这样做了?
  • @Jeroen van Veghel 分页库已自动加载

标签: php codeigniter pagination


【解决方案1】:

使用以下库将 datatables.js 与 codeigniter 一起使用。在这里您可以进行搜索和排序。

Codeigniter Datatables

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2023-03-20
    • 2017-08-10
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多