Advertisement
azmicolejr

join

Nov 29th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. Controller:
  2. public function ajax_list()
  3. {
  4. $list = $this->Foto_model->get_datatables();
  5. $data = array();
  6. $no = $_POST['start'];
  7.  
  8. foreach ($list as $data_foto) {
  9. $no++;
  10. $row = array();
  11. $row[] = '<p style="text-align: center">'.$no.'</p>';
  12. $row[] = '<p style="text-align: left">'.$data_foto->judul_foto.'</p>';
  13. $row[] = '<p style="text-align: left">'.$data_foto->judul_album.'</p>';
  14. $row[] = '<p style="text-align: center"><img src="'.base_url('assets/images/foto/').$data_foto->ufile.$data_foto->ufile_type.'" width="100px"></p>';
  15. $row[] = "
  16. <p style='text-align: center'>
  17. <a class='btn btn-sm btn-warning' href='".base_url('admin/foto/update/').$data_foto->id_foto."' title='Edit'><i class='fa fa-pencil'></i> EDIT</a>
  18. </p>
  19. <p style='text-align: center'>
  20. <a class='btn btn-sm btn-danger' href='".base_url('admin/foto/delete/').$data_foto->id_foto."', onClick=\"return confirm('Apakah Anda yakin?');\"><i class='glyphicon glyphicon-trash'></i> HAPUS</a>
  21. </p>";
  22.  
  23. $data[] = $row;
  24. }
  25.  
  26. $output = array(
  27. "draw" => $_POST['draw'],
  28. "recordsTotal" => $this->Foto_model->count_all(),
  29. "recordsFiltered" => $this->Foto_model->count_filtered(),
  30. "data" => $data
  31. );
  32. //output to json format
  33. echo json_encode($output);
  34. }
  35.  
  36. Model:
  37. public function get_datatables()
  38. {
  39. $this->_get_datatables_query();
  40. if($_POST['length'] != -1)
  41. $this->db->limit($_POST['length'], $_POST['start']);
  42. $this->db->select('id_foto, judul_foto, judul_album, foto.userfile as ufile,foto.userfile_type as ufile_type');
  43. $this->db->join('album ', 'album.id_album = foto.id_foto','left');
  44.  
  45. $query = $this->db->get();
  46.  
  47. return $query->result();
  48. }
  49.  
  50. View:
  51. <section class="content">
  52. <div class="box box-primary">
  53. <div class="box-body table-responsive padding">
  54. <a href="<?php echo $action_add ?>">
  55. <button class="btn btn-success"><i class="fa fa-plus"></i> Tambah <?php echo $title ?> Baru</button>
  56. </a>
  57. <button class="btn btn-grey" onclick="reload_table()">
  58. <i class="glyphicon glyphicon-refresh"></i> Refresh
  59. </button>
  60.  
  61. <h4 align="center"><?php echo $this->session->userdata('message') <> '' ? $this->session->userdata('message') : ''; ?></h4>
  62.  
  63. <hr/>
  64. <table id="table" class="table table-striped table-bordered">
  65. <thead>
  66. <tr>
  67. <th style="text-align: center">No.</th>
  68. <th style="text-align: center">Judul Foto</th>
  69. <th style="text-align: center">Album</th>
  70. <th style="text-align: center">Foto</th>
  71.  
  72. <th style="text-align: center">Aksi</th>
  73. </tr>
  74. </thead>
  75. <tbody></tbody>
  76. </table>
  77. </div>
  78. </div>
  79. </section>
  80.  
  81. <!-- DATA TABLES SCRIPT -->
  82. <script src="<?php echo base_url('assets/plugins/datatables/jquery.dataTables.min.js') ?>" type="text/javascript"></script>
  83. <script src="<?php echo base_url('assets/plugins/datatables/dataTables.bootstrap.min.js') ?>" type="text/javascript"></script>
  84. <script type="text/javascript">
  85. var table;
  86. $(document).ready(function() {
  87. table = $('#table').DataTable({
  88. "processing": true, //Feature control the processing indicator.
  89. "serverSide": true, //Feature control DataTables' server-side processing mode.
  90. "bPaginate": true,
  91. "bLengthChange": true,
  92. "bFilter": true,
  93. "bSort": true,
  94. "bInfo": true,
  95. "bAutoWidth": false,
  96. "aaSorting": [[0,'desc']],
  97. "lengthMenu": [[10, 25, 50, 100, 500, 1000, -1], [10, 25, 50, 100, 500, 1000, "Semua"]],
  98.  
  99. // Load data for the table's content from an Ajax source
  100. "ajax": {
  101. "url": "<?php echo site_url('admin/foto/ajax_list')?>",
  102. "type": "POST"
  103. },
  104.  
  105. //Set column definition initialisation properties.
  106. "columnDefs": [
  107. {
  108. "targets": [ -1 ], //last column
  109. },
  110. ],
  111. });
  112. });
  113.  
  114. function reload_table(){
  115. table.ajax.reload(null,false); //reload datatable ajax
  116. }
  117. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement