Advertisement
Guest User

DATATABLES CODEIGNITER

a guest
Sep 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1. function load_grid() {
  2.       table = $('#table_grid').DataTable({
  3.         "scrollX": true,
  4.         "processing": true, //Feature control the processing indicator.
  5.         "serverSide": true, //Feature control DataTables' server-side processing mode.
  6.         // Load data for the table's content from an Ajax source
  7.         "ajax": {
  8.             "url": "<?php echo base_url().'Position/ajax_list'?>",
  9.             "type": "POST"
  10.         },
  11.         //Set column definition initialisation properties.
  12.         "columnDefs": [
  13.           {
  14.             "targets": [ -1 ], //last column
  15.             "orderable": false, //set not orderable
  16.             // "className":'mdl-data-table__cell--non-numeric'
  17.           },
  18.         ],
  19.  
  20.       });
  21.     }
  22. -------------------------------------------------------------------------------------
  23. ----------------------------CONTROLLER-----------------------------------------------
  24. public function ajax_list()
  25.     {
  26.     $btn = '';
  27.         $list = $this->m_position->get_datatables();
  28.         $data = array();
  29.         $no = $_POST['start'];
  30.         foreach ($list as $position) {
  31.             $no++;
  32.             $row = array();
  33.             $row[] = $no;
  34.       $row[] = $position->div_code;
  35.             $row[] = $position->position_code;
  36.             $row[] = $position->position_desc;
  37.       $row[] = $position->is_active;
  38.       $row[] = '<a class="btn btn-sm btn-info1" href="javascript:void()" title="Edit" onclick="edit_position('."'".$position->pid."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
  39.                   <a class="btn btn-sm btn-danger1" href="javascript:void()" title="Hapus" onclick="delete_position('."'".$position->pid."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
  40.  
  41.             $data[] = $row;
  42.         }
  43.  
  44.         $output = array(
  45.                         "draw" => $_POST['draw'],
  46.                         "recordsTotal" => $this->m_position->count_all(),
  47.                         "recordsFiltered" => $this->m_position->count_filtered(),
  48.                         "data" => $data,
  49.                 );
  50.         //output to json format
  51.         echo json_encode($output);
  52.     }
  53. ---------------------------------------------------------------------------------
  54. -------------------------MODEL---------------------------------------------------
  55. private function _get_datatables_query()
  56.     {
  57.  
  58.         $this->db2->from($this->table);
  59.  
  60.         $i = 0;
  61.  
  62.         foreach ($this->column as $item)
  63.         {
  64.             if($_POST['search']['value'])
  65.                 ($i===0) ? $this->db2->like($item, $_POST['search']['value']) : $this->db2->or_like($item, $_POST['search']['value']);
  66.             $column[$i] = $item;
  67.             $i++;
  68.         }
  69.  
  70.         if(isset($_POST['order']))
  71.         {
  72.             $this->db2->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  73.         }
  74.         else if(isset($this->order))
  75.         {
  76.             $order = $this->order;
  77.             $this->db2->order_by(key($order), $order[key($order)]);
  78.         }
  79.     }
  80.  
  81.     function get_datatables()
  82.     {
  83.         $this->_get_datatables_query();
  84.         if($_POST['length'] != -1)
  85.         $this->db2->limit($_POST['length'], $_POST['start']);
  86.         $query = $this->db2->get();
  87.         return $query->result();
  88.     }
  89.  
  90.     function count_filtered()
  91.     {
  92.         $this->_get_datatables_query();
  93.         $query = $this->db2->get();
  94.         return $query->num_rows();
  95.     }
  96.  
  97.     public function count_all()
  98.     {
  99.         $this->db2->from($this->table);
  100.         return $this->db2->count_all_results();
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement