Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th bgcolor="#BDBDBD">Rotina</th>
- <th bgcolor="#BDBDBD">Descricao</th>
- <th bgcolor="#BDBDBD">Previsto</th>
- <th bgcolor="#BDBDBD">Realizado</th>
- </tr>
- </thead>
- <tbody>
- </tbody>
- <tfoot>
- <tr>
- <th bgcolor="#BDBDBD"></th>
- <th bgcolor="#BDBDBD"></th>
- <th bgcolor="#BDBDBD"></th>
- <th bgcolor="#BDBDBD"></th>
- </tr>
- </tfoot>
- </table>
- $(document).ready(function() {
- //datatables
- table = $('#table').DataTable({
- "processing": true, //Feature control the processing indicator.
- "serverSide": true, //Feature control DataTables' server-side processing mode.
- "order": [], //Initial no order.
- // Load data for the table's content from an Ajax source
- "ajax": {
- "url": "<?php echo site_url('projeto/ajax_list')?>",
- "type": "POST"
- },
- //Set column definition initialisation properties.
- "columnDefs": [
- {
- "targets": [ -1 ], //last column
- "orderable": false, //set not orderable
- },
- ],
- });
- });
- public function ajax_list()
- {
- $list = $this->projeto->get_datatables();
- $data = array();
- $no = $_POST['start'];
- foreach ($list as $projeto) {
- $no++;
- $row = array();
- $row[] = $projeto->rotina;
- $row[] = $projeto->descricao;
- $row[] = $projeto->previsto;
- $row[] = $projeto->realizado;
- $data[] = $row;
- }
- $output = array(
- "draw" => $_POST['draw'],
- "recordsTotal" => $this->projeto->count_all(),
- "recordsFiltered" => $this->projeto->count_filtered(),
- "data" => $data,
- );
- //output to json format
- echo json_encode($output);
- private function _get_datatables_query()
- {
- $this->db->from($this->table);
- $i = 0;
- foreach ($this->column_search as $item) // loop column
- {
- if($_POST['search']['value']) // if datatable send POST for search
- {
- if($i===0) // first loop
- {
- $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
- $this->db->like($item, $_POST['search']['value']);
- }
- else
- {
- $this->db->or_like($item, $_POST['search']['value']);
- }
- if(count($this->column_search) - 1 == $i) //last loop
- $this->db->group_end(); //close bracket
- }
- $i++;
- }
- if(isset($_POST['order'])) // here order processing
- {
- $this->db->order_by($this->column_order[$_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();
- }
Add Comment
Please, Sign In to add comment