Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
  2. <thead>
  3. <tr>
  4. <th bgcolor="#BDBDBD">Rotina</th>
  5. <th bgcolor="#BDBDBD">Descricao</th>
  6. <th bgcolor="#BDBDBD">Previsto</th>
  7. <th bgcolor="#BDBDBD">Realizado</th>
  8. </tr>
  9. </thead>
  10. <tbody>
  11. </tbody>
  12.  
  13. <tfoot>
  14. <tr>
  15. <th bgcolor="#BDBDBD"></th>
  16. <th bgcolor="#BDBDBD"></th>
  17. <th bgcolor="#BDBDBD"></th>
  18. <th bgcolor="#BDBDBD"></th>
  19. </tr>
  20. </tfoot>
  21. </table>
  22.  
  23. $(document).ready(function() {
  24.  
  25. //datatables
  26. table = $('#table').DataTable({
  27.  
  28. "processing": true, //Feature control the processing indicator.
  29. "serverSide": true, //Feature control DataTables' server-side processing mode.
  30. "order": [], //Initial no order.
  31.  
  32. // Load data for the table's content from an Ajax source
  33. "ajax": {
  34. "url": "<?php echo site_url('projeto/ajax_list')?>",
  35. "type": "POST"
  36. },
  37.  
  38. //Set column definition initialisation properties.
  39. "columnDefs": [
  40. {
  41. "targets": [ -1 ], //last column
  42. "orderable": false, //set not orderable
  43. },
  44. ],
  45.  
  46. });
  47. });
  48.  
  49. public function ajax_list()
  50. {
  51. $list = $this->projeto->get_datatables();
  52. $data = array();
  53. $no = $_POST['start'];
  54. foreach ($list as $projeto) {
  55. $no++;
  56. $row = array();
  57. $row[] = $projeto->rotina;
  58. $row[] = $projeto->descricao;
  59. $row[] = $projeto->previsto;
  60. $row[] = $projeto->realizado;
  61.  
  62. $data[] = $row;
  63. }
  64.  
  65. $output = array(
  66. "draw" => $_POST['draw'],
  67. "recordsTotal" => $this->projeto->count_all(),
  68. "recordsFiltered" => $this->projeto->count_filtered(),
  69. "data" => $data,
  70. );
  71. //output to json format
  72. echo json_encode($output);
  73.  
  74.  
  75. private function _get_datatables_query()
  76. {
  77.  
  78. $this->db->from($this->table);
  79.  
  80. $i = 0;
  81.  
  82. foreach ($this->column_search as $item) // loop column
  83. {
  84. if($_POST['search']['value']) // if datatable send POST for search
  85. {
  86.  
  87. if($i===0) // first loop
  88. {
  89. $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
  90. $this->db->like($item, $_POST['search']['value']);
  91. }
  92. else
  93. {
  94. $this->db->or_like($item, $_POST['search']['value']);
  95. }
  96.  
  97. if(count($this->column_search) - 1 == $i) //last loop
  98. $this->db->group_end(); //close bracket
  99. }
  100. $i++;
  101. }
  102.  
  103. if(isset($_POST['order'])) // here order processing
  104. {
  105. $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  106. }
  107. else if(isset($this->order))
  108. {
  109. $order = $this->order;
  110. $this->db->order_by(key($order), $order[key($order)]);
  111. }
  112. }
  113.  
  114. function get_datatables()
  115. {
  116. $this->_get_datatables_query();
  117. if($_POST['length'] != -1)
  118. $this->db->limit($_POST['length'], $_POST['start']);
  119. $query = $this->db->get();
  120. return $query->result();
  121. }
Add Comment
Please, Sign In to add comment