php - Datatables library not working in CodeIgniter 3 -
i'm trying datatables library on codeigniter 3, doesn't work in ci3. i've tried this example in codeigniter 2 , works want library on codeigniter 3.
what need change in order work codeigniter 3?
create person_model.php in model directory
<?php defined('basepath') or exit('no direct script access allowed'); class person_model extends ci_model { var $table = 'persons'; var $column = array('firstname','lastname','gender','address','dob'); var $order = array('id' => 'desc'); public function __construct() { parent::__construct(); $this->load->database(); } private function _get_datatables_query() { $this->db->from($this->table); $i = 0; foreach ($this->column $item) { if($_post['search']['value']) ($i===0) ? $this->db->like($item, $_post['search']['value']) : $this->db->or_like($item, $_post['search']['value']); $column[$i] = $item; $i++; } if(isset($_post['order'])) { $this->db->order_by($column[$_post['order']['0']['column']], $_post['order']['0']['dir']); } else if(isset($this->order)) { $order = $this->order; $this->db->order_by(key($order), $order[key($order)]); } } function get_datatables() { $this->_get_datatables_query(); if($_post['length'] != -1) $this->db->limit($_post['length'], $_post['start']); $query = $this->db->get(); return $query->result(); } function count_filtered() { $this->_get_datatables_query(); $query = $this->db->get(); return $query->num_rows(); } public function count_all() { $this->db->from($this->table); return $this->db->count_all_results(); }
create controller file person.php
<?php defined('basepath') or exit('no direct script access allowed'); class person extends ci_controller { public function __construct() { parent::__construct(); $this->load->model('person_model','person'); } public function index() { $this->load->helper('url'); $this->load->view('person_view'); } public function ajax_list() { $list = $this->person->get_datatables(); $data = array(); $no = $_post['start']; foreach ($list $person) { $no++; $row = array(); $row[] = $person->firstname; $row[] = $person->lastname; $row[] = $person->gender; $row[] = $person->address; $row[] = $person->dob; $data[] = $row; } $output = array( "draw" => $_post['draw'], "recordstotal" => $this->person->count_all(), "recordsfiltered" => $this->person->count_filtered(), "data" => $data, ); //output json format echo json_encode($output); }
and last create view. person_view.php @ views directory
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>ajax crud bootstrap modals , datatables</title> <link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css')?>" rel="stylesheet"> <link href="<?php echo base_url('assets/datatables/css/datatables.bootstrap.css')?>" rel="stylesheet"> <!-- html5 shim , respond.js ie8 support of html5 elements , media queries --> <!-- warning: respond.js doesn't work if view page via file:// --> <!--[if lt ie 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="container"> <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%"> <thead> <tr> <th>first name</th> <th>last name</th> <th>gender</th> <th>address</th> <th>date of birth</th> <th style="width:125px;">action</th> </tr> </thead> <tbody> </tbody> <tfoot> <tr> <th>first name</th> <th>last name</th> <th>gender</th> <th>address</th> <th>date of birth</th> <th>action</th> </tr> </tfoot> </table> </div> <script src="<?php echo base_url('assets/jquery/jquery-2.1.4.min.js')?>"></script> <script src="<?php echo base_url('assets/bootstrap/js/bootstrap.min.js')?>"></script> <script src="<?php echo base_url('assets/datatables/js/jquery.datatables.min.js')?>"></script> <script src="<?php echo base_url('assets/datatables/js/datatables.bootstrap.js')?>"></script> <script type="text/javascript"> var save_method; //for save method string var table; $(document).ready(function() { table = $('#table').datatable({ "processing": true, //feature control processing indicator. "serverside": true, //feature control datatables' server-side processing mode. // load data table's content ajax source "ajax": { "url": "<?php echo site_url('person/ajax_list')?>", "type": "post" }, //set column definition initialisation properties. "columndefs": [ { "targets": [ -1 ], //last column "orderable": false, //set not orderable }, ], }); }); function reload_table() { table.ajax.reload(null,false); //reload datatable ajax } </script> </body> </html>
the sql create table :
create database crud; use crud; create table persons
( id
int(11) unsigned not null auto_increment, firstname
varchar(100) default null, lastname
varchar(100) default null, gender
enum('male','female') default null, address
varchar(200) default null, dob
date default null, primary key (id
) ) engine=innodb default charset=utf8;
Comments
Post a Comment