Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在Codeigniter中使用AJAX和JSON格式进行分页_Jquery_Json_Ajax_Codeigniter_Pagination - Fatal编程技术网

Jquery 在Codeigniter中使用AJAX和JSON格式进行分页

Jquery 在Codeigniter中使用AJAX和JSON格式进行分页,jquery,json,ajax,codeigniter,pagination,Jquery,Json,Ajax,Codeigniter,Pagination,如何在codeigniter中使用这种ajax、json格式进行分页?我在没有ajax的情况下进行了分页,它正在工作。但我现在正在使用ajax和json格式加载数据。我不知道如何添加分页。有人能帮我吗 这是我的全部代码 谢谢 型号: public function getManufacturerRecords(){ //retrieve data $query = $this->db->get('manufacturer'); return $query->re

如何在codeigniter中使用这种ajax、json格式进行分页?我在没有ajax的情况下进行了分页,它正在工作。但我现在正在使用ajax和json格式加载数据。我不知道如何添加分页。有人能帮我吗

这是我的全部代码

谢谢

型号:

public function getManufacturerRecords(){ //retrieve data
    $query = $this->db->get('manufacturer');
    return $query->result();
}
控制器

public function index(){
    if($this->session->userdata('is_logged_in')){
        $this->load->view('../template/header');
        $this->load->view('manufacturer');
        $this->load->view('../template/footer');
    } else {
        redirect('main/restricted');
    }
}

public function manufacturer_list(){
    $result = $this->manufacturer_model->getManufacturerRecord();
    echo json_encode($result);
}
视图:


身份证件
制造商
行动
阿贾克斯:

showRecords();
函数showRecords(){
$.ajax({
url:“制造商/制造商列表”,
键入:“POST”,
数据类型:“json”,
成功:功能(数据){
var html='';

对于(i=0;i您尝试过这个类吗?

1.Codeigniter使用ajax分页

控制器

/** Load pagination library **/
$this->load->library('pagination');
/** initialize the config **/
$config['per_page'] = 20; /** per page records **/
$config['uri_segment'] = 3; /** in case your url has different please use proper uri segment**/
$config['cur_page'] = $this->uri->segment(3) ? $this->uri->segment(3): '1';
$config['full_tag_open'] = '<ul class="pagination">'; /** we will use this class for ajax call or you can customize pagination as you want**/
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a>';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';

$offset = $this->uri->segment(3);
if ($offset == 1 || $offset == '' || !intval($offset)) 
    $offset = 0;
$offset ? $offset : 0;
$limit = $config['per_page'];
$q = "SELECT * FROM TABLE NAME "; /** change your query or use model **/
$query = $this->db->query($q);
$config['total_rows'] = $query->num_rows();/** COUNT OF TOTAL RECORDS IN DATABASE **/;
/** get your data **/
$q .= " LIMIT $offset, $limit";
$query = $this->db->query($q);
$data['data'] = $query->result();
$config['base_url'] = 'SITE_NAME/CONTROLLER_NAME/METHOD_NAME';
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links(); /** this will create  pagination  links **/
$data['ajax'] = false;
if ($this->input->is_ajax_request()) {
    /*** return only Table view if its ajax call **/
    $data['ajax'] = true;
    echo json_encode($this->load->view('manufacturer',$data,true));
}else{
    $this->load->view('../template/header');
    $this->load->view('manufacturer',$data);
    $this->load->view('../template/footer');
}
  • 使用datatable(服务器端Ajax)和codeigniter进行分页
  • 将dataTable的js和css库包含到html文件中

    <script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
    
    
    
    您的html如下所示

    <table class="table table-condensed table-bordered table-striped" id="example">
        <thead>
           <tr>
               <th>ID</th>
               <th>Manufacturer</th>
               <th>Actions</th>
           </tr>
        </thead>
    </table>
    
    
    身份证件
    制造商
    行动
    
    现在开始ajax调用

    <script>
            $(document).ready(function() {
                $('#example').DataTable({
                    url: '<?php base_url(); ?>controllerName/index',
                    processing: true,
                    serverSide: true,
                    paging: true,
                    searching: true,
                    ordering: true,
                    order: [[0, "asc"]],
                    scrollX: true,
                    scroller: true,
                    columns: [{data: id"}, {data: "manufacturer"}, {data: "action"}]               
                });
            });
        </script>
    
    
    $(文档).ready(函数(){
    $('#示例')。数据表({
    url:'控制器名称/索引',
    处理:对,
    服务器端:是的,
    是的,
    搜索:是的,
    顺序:对,
    订单:[[0,“asc”]],
    是的,
    卷轴:是的,
    列:[{data:id“},{data:“manufacturer”},{data:“action”}]
    });
    });
    
    控制器

    <?php if(!$ajax){?>
        <div class="loadData">
    }?>
    <table  id="showdata" class="table table-condensed table-bordered table-striped" style="width:50%">
        <thead>
           <tr>
               <th>ID</th>
               <th>Manufacturer</th>
               <th>Actions</th>
           </tr>
        </thead>
        <tbody>
            <?php foreach($data as $k=>$v){?>
                <tr><?php echo $v['id']?></tr>
               <tr><?php echo $v['Manufacturer']?></tr>
               <tr>Actions</tr>
            <?php }?>
        </tbody>
    </table>
    <?php if(!$ajax){?>
        </div>
    <?php }?>
    /** This will show pagination link**/
    <?php echo $links;?>
    
    public function index() {
    if($this->session->userdata('is_logged_in')){
            $data = array();
            if ($this->input->is_ajax_request()) {
                /** this will handle datatable js ajax call * */
                /** get all datatable parameters * */
                $search = $this->input->get('search');/** search value for datatable  * */
                $offset = $this->input->get('start');/** offset value * */
                $limit = $this->input->get('length');/** limits for datatable (show entries) * */
                $order = $this->input->get('order');/** order by (column sorted ) * */
                $column = array('id', 'manufacturer');/**  set your data base column name here for sorting* */
                $orderColumn = isset($order[0]['column']) ? $column[$order[0]['column']] : 'parameter';
                $orderDirection = isset($order[0]['dir']) ? $order[0]['dir'] : 'asc';
                $ordrBy = $orderColumn . " " . $orderDirection;
    
                if (isset($search['value']) && !empty($search['value'])) {
                    /** creat sql or call Model * */
    
                    /** I am calling sql directly in controller for your answer 
                     * Please change your sql according to your table name
                     * */
                    $sql = "SELECT * FROM TABLE_NAME WHERE column_name '%like%'" . $search['value'] . " order by " . $ordrBy . " limit $offset,$limit";
                    $sql = "SELECT count(*) FROM TABLE_NAME WHERE column_name '%like%'" . $search['value'] . " order by " . $ordrBy;
                    $result = $this->db->query($sql);
                    $result2 = $this->db->query($sql2);
                    $count = $result2->num_rows();
                } else {
                    /**
                     * If no seach value avaible in datatable
                     */
                    $sql = "SELECT * FROM TABLE_NAME  order by " . $ordrBy . " limit $offset,$limit";
                    $sql2 = "SELECT * FROM TABLE_NAME  order by " . $ordrBy;
                    $result = $this->db->query($sql);
                    $result2 = $this->db->query($sql2);
                    $count = $result2->num_rows();
                }
                /** create data to display in dataTable as you want **/    
    
                $data = array();
                if (!empty($result->result())) {
                    foreach ($result->result() as $k => $v) {
                        $data[] = array(
                             'id' =>  $v['id'],
                             'manufacturer'=>$v['manufacturer'],                         
                             'actions' =>  "<a href=''><strong>Edit</strong></a>" 
                        );
                    }
                }
                /**
                 * draw,recordTotal,recordsFiltered is required for pagination and info.
                 */
                $results = array(
                    "draw" => $this->input->get('draw'),
                    "recordsTotal" => count($data),
                    "recordsFiltered" => $count,
                    "data" => $data 
                );
                echo json_encode($results);
            } else {
                /** this will load by default with no data for datatable
                 *  we will load data in table through datatable ajax call
                 */
            $this->load->view('../template/header');
            $this->load->view('manufacturer',$data);
            $this->load->view('../template/footer');
    
            }
        }else{
            redirect('main/restricted');
        }
    }
    
    公共功能索引(){
    如果($this->session->userdata('is\u logged\u in')){
    $data=array();
    如果($this->input->is\u ajax\u request()){
    /**这将处理datatable js ajax调用**/
    /**获取所有数据表参数**/
    $search=$this->input->get('search');/**数据表的搜索值**/
    $offset=$this->input->get('start');/**偏移值**/
    $limit=$this->input->get('length');/**数据表的限制(显示条目)**/
    $order=$this->input->get('order');/**order by(列排序)**/
    $column=array('id','manufacturer');/**在此处设置数据库列名以进行排序**/
    $orderColumn=isset($order[0]['column'])?$column[$order[0]['column']]:'parameter';
    $orderDirection=isset($order[0]['dir'])?$order[0]['dir']:'asc';
    $ordrBy=$orderColumn.“.”$orderDirection;
    if(设置($search['value'])和&!empty($search['value'])){
    /**创建sql或调用模型**/
    /**我正在控制器中直接调用sql以获取您的答案
    *请根据表名更改sql
    * */
    $sql=“从列名称“%like%”所在的表名称中选择*。$search['value']”。按“$ordrBy.”排序。限制$offset,$limit”;
    $sql=“从列名称“%like%”所在的表名称“$search['value']”中选择计数(*)。$orderby;
    $result=$this->db->query($sql);
    $result2=$this->db->query($sql2);
    $count=$result2->num_rows();
    }否则{
    /**
    *如果数据表中没有可用的搜索值
    */
    $sql=“按“$ordrBy.”limit$offset,$limit”从表中选择*名称顺序;
    $sql2=“从表中选择*按“$ordrBy”命名顺序;
    $result=$this->db->query($sql);
    $result2=$this->db->query($sql2);
    $count=$result2->num_rows();
    }
    /**创建要在dataTable中显示的数据**/
    $data=array();
    如果(!empty($result->result())){
    foreach($result->result()作为$k=>$v){
    $data[]=数组(
    'id'=>$v['id'],
    “制造商”=>v美元[“制造商”],
    “操作”=>“”
    );
    }
    }
    /**
    *分页和信息需要draw、recordTotal和recordsFiltered。
    */
    $results=数组(
    “draw”=>this->input->get('draw'),
    “recordsTotal”=>计数($data),
    “recordsFiltered”=>$count,
    “数据”=>$data
    );
    echo json_编码($results);
    }否则{
    /**默认情况下,这将在没有数据的情况下加载datatable
    *我们将通过DataTableAjax调用在表中加载数据
    */
    $this->load->view(“../template/header”);
    $this->load->view('manufacturer',$data);
    $this->load->view(“../template/footer”);
    }
    }否则{
    重定向(“主/受限”);
    }
    }
    

    Datatable将创建您的分页。

    您可以修改您的分页,请检查此项。但是,我建议您使用data table js library,因为您的视图中有表。如果有任何混淆,请告诉我loadmore函数没有分页,对吗?就像您单击按钮将加载更多数据一样?您是否进行了分页关于数据加载ajax?@shafiqYes我做了。通过加载更多,你可以进行ajax分页。我将与你共享代码。但是我建议检查datatable。它内置了ajax分页。检查我评论中的第二个链接。先生,你能帮我吗?是的,我知道它内置了分页,但遗憾的是,我不知道如何在我的ajax代码上实现。Un定义变量:为foreach()提供的数据和无效参数我在@shafiq遇到此错误我尝试了您的第一个示例如果($t)则不在这里
    <table class="table table-condensed table-bordered table-striped" id="example">
        <thead>
           <tr>
               <th>ID</th>
               <th>Manufacturer</th>
               <th>Actions</th>
           </tr>
        </thead>
    </table>
    
    <script>
            $(document).ready(function() {
                $('#example').DataTable({
                    url: '<?php base_url(); ?>controllerName/index',
                    processing: true,
                    serverSide: true,
                    paging: true,
                    searching: true,
                    ordering: true,
                    order: [[0, "asc"]],
                    scrollX: true,
                    scroller: true,
                    columns: [{data: id"}, {data: "manufacturer"}, {data: "action"}]               
                });
            });
        </script>
    
    public function index() {
    if($this->session->userdata('is_logged_in')){
            $data = array();
            if ($this->input->is_ajax_request()) {
                /** this will handle datatable js ajax call * */
                /** get all datatable parameters * */
                $search = $this->input->get('search');/** search value for datatable  * */
                $offset = $this->input->get('start');/** offset value * */
                $limit = $this->input->get('length');/** limits for datatable (show entries) * */
                $order = $this->input->get('order');/** order by (column sorted ) * */
                $column = array('id', 'manufacturer');/**  set your data base column name here for sorting* */
                $orderColumn = isset($order[0]['column']) ? $column[$order[0]['column']] : 'parameter';
                $orderDirection = isset($order[0]['dir']) ? $order[0]['dir'] : 'asc';
                $ordrBy = $orderColumn . " " . $orderDirection;
    
                if (isset($search['value']) && !empty($search['value'])) {
                    /** creat sql or call Model * */
    
                    /** I am calling sql directly in controller for your answer 
                     * Please change your sql according to your table name
                     * */
                    $sql = "SELECT * FROM TABLE_NAME WHERE column_name '%like%'" . $search['value'] . " order by " . $ordrBy . " limit $offset,$limit";
                    $sql = "SELECT count(*) FROM TABLE_NAME WHERE column_name '%like%'" . $search['value'] . " order by " . $ordrBy;
                    $result = $this->db->query($sql);
                    $result2 = $this->db->query($sql2);
                    $count = $result2->num_rows();
                } else {
                    /**
                     * If no seach value avaible in datatable
                     */
                    $sql = "SELECT * FROM TABLE_NAME  order by " . $ordrBy . " limit $offset,$limit";
                    $sql2 = "SELECT * FROM TABLE_NAME  order by " . $ordrBy;
                    $result = $this->db->query($sql);
                    $result2 = $this->db->query($sql2);
                    $count = $result2->num_rows();
                }
                /** create data to display in dataTable as you want **/    
    
                $data = array();
                if (!empty($result->result())) {
                    foreach ($result->result() as $k => $v) {
                        $data[] = array(
                             'id' =>  $v['id'],
                             'manufacturer'=>$v['manufacturer'],                         
                             'actions' =>  "<a href=''><strong>Edit</strong></a>" 
                        );
                    }
                }
                /**
                 * draw,recordTotal,recordsFiltered is required for pagination and info.
                 */
                $results = array(
                    "draw" => $this->input->get('draw'),
                    "recordsTotal" => count($data),
                    "recordsFiltered" => $count,
                    "data" => $data 
                );
                echo json_encode($results);
            } else {
                /** this will load by default with no data for datatable
                 *  we will load data in table through datatable ajax call
                 */
            $this->load->view('../template/header');
            $this->load->view('manufacturer',$data);
            $this->load->view('../template/footer');
    
            }
        }else{
            redirect('main/restricted');
        }
    }