azmicolejr

datatable server-side

Feb 1st, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.91 KB | None | 0 0
  1. ---------------------------------------------
  2. controller: Produk.php
  3. ---------------------------------------------
  4. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  5. class Produk extends CI_Controller {
  6.     public function __construct()
  7.     {
  8.         parent::__construct();
  9.         $this->load->model('Admin/m_produk','produk');
  10.     }
  11.  
  12.     public function index()
  13.     {
  14.         $this->load->helper('url');
  15.         $this->load->view('Admin/produk/v_lihat');
  16.     }
  17.  
  18.     public function ajax_list()
  19.     {
  20.         $list = $this->produk->get_datatables();
  21.         $data = array();
  22.         $no = $_POST['start'];
  23.         foreach ($list as $produk) {
  24.             $no++;
  25.             $row = array();
  26.             $row[] = $produk->nama_produk;
  27.             $row[] = $produk->kat;
  28.             $row[] = $produk->subkat;
  29.             $row[] = $produk->supersubkat;
  30.  
  31.             //add html for action
  32.             $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void()" title="Edit" onclick="edit_produk('."'".$produk->id_produk."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
  33.                  <a class="btn btn-sm btn-danger" href="javascript:void()" title="Hapus" onclick="delete_produk('."'".$produk->id_produk."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
  34.        
  35.             $data[] = $row;
  36.         }
  37.  
  38.         $output = array(
  39.                         "draw" => $_POST['draw'],
  40.                         "recordsTotal" => $this->produk->count_all(),
  41.                         "recordsFiltered" => $this->produk->count_filtered(),
  42.                         "data" => $data,
  43.                 );
  44.         //output to json format
  45.         echo json_encode($output);
  46.     }
  47.  
  48. ---------------------------------------------
  49. model: m_produk.php
  50. ---------------------------------------------
  51. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  52. class M_produk extends CI_Model {
  53.     var $table = 'produk';
  54.     //set column field database for order and search
  55.     var $column = array('nama_produk','kat','subkat','supersubkat');
  56.     var $order = array('id_produk' => 'desc'); // default order
  57.  
  58.     private function _get_datatables_query()
  59.     {
  60.         $this->db->from($this->table);
  61.  
  62.         $i = 0;
  63.    
  64.         foreach ($this->column as $item) // loop column
  65.         {
  66.             if($_POST['search']['value']) // if datatable send POST for search
  67.             {
  68.                 if($i===0) // first loop
  69.                 {
  70.                     $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
  71.                     $this->db->like($item, $_POST['search']['value']);
  72.                 }
  73.                 else
  74.                 {
  75.                     $this->db->or_like($item, $_POST['search']['value']);
  76.                 }
  77.  
  78.                 if(count($this->column) - 1 == $i) //last loop
  79.                     $this->db->group_end(); //close bracket
  80.             }
  81.             $column[$i] = $item; // set column array variable to order processing
  82.             $i++;
  83.         }
  84.        
  85.         if(isset($_POST['order'])) // here order processing
  86.         {
  87.             $this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  88.         }
  89.         else if(isset($this->order))
  90.         {
  91.             $order = $this->order;
  92.             $this->db->order_by(key($order), $order[key($order)]);
  93.         }
  94.     }
  95.  
  96.     function get_datatables()
  97.     {
  98.         $this->_get_datatables_query();
  99.         if($_POST['length'] != -1)
  100.         $this->db->limit($_POST['length'], $_POST['start']);
  101.        
  102.         $this->db->join('kategori', 'kategori.id_kat = produk.kat','left');
  103.         $this->db->join('subkat', 'subkat.id_subkat  = produk.subkat','left');
  104.         $this->db->join('supersubkat', 'supersubkat.id_supersubkat  = produk.supersubkat','left');
  105.          
  106.         $query = $this->db->get();
  107.  
  108.         return $query->result();
  109.     }
  110.  
  111.     function count_filtered()
  112.     {
  113.         $this->_get_datatables_query();
  114.         $query = $this->db->get();
  115.         return $query->num_rows();
  116.     }
  117.  
  118.     public function count_all()
  119.     {
  120.         $this->db->from($this->table);
  121.         return $this->db->count_all_results();
  122.     }
  123.  
  124.     public function get_by_id($id_produk)
  125.     {
  126.         $this->db->from($this->table);
  127.         $this->db->where('id_produk',$id_produk);
  128.         $query = $this->db->get();
  129.  
  130.         return $query->row();
  131.     }
  132.  
  133. ---------------------------------------------
  134. views: v_lihat.php
  135. ---------------------------------------------
  136. <!DOCTYPE html>
  137. <html>
  138.   <head>
  139.     <meta charset="UTF-8">
  140.     <title>Daftar Produk | Admin Panel</title>
  141.     <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
  142.     <!-- Bootstrap 3.3.4 -->
  143.     <link href="<?php echo base_url() ?>assets/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />    
  144.     <!-- FontAwesome 4.3.0 -->
  145.     <link href="<?php echo base_url() ?>assets/font-awesome-4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  146.     <!-- Theme style -->
  147.     <link href="<?php echo base_url() ?>assets/dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />
  148.     <link href="<?php echo base_url() ?>assets/dist/css/skins/skin-blue.min.css" rel="stylesheet" type="text/css" />
  149.     <!-- DATA TABLES -->
  150.     <link href="<?php echo base_url() ?>assets/plugins/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
  151.     <link href="<?php echo base_url() ?>img/fav.ico" rel="shortcut icon"/>
  152.   </head>
  153.   <body class="skin-blue sidebar-mini">
  154.     <div class="wrapper">
  155.  
  156.       <?php $this->load->view('Admin/header'); ?>
  157.  
  158.       <div class="content-wrapper">
  159.         <section class="content-header">
  160.           <h1>Daftar Produk <small>| <?php echo anchor('Admin/produk/tambah/','Tambah Produk') ?></small></h1>
  161.           <ol class="breadcrumb">
  162.             <li><a href="#"><i class="fa fa-dashboard"></i> Produk</a></li>
  163.             <li class="active">Daftar Produk</li>
  164.           </ol>
  165.         </section>
  166.  
  167.         <section class="content">
  168.           <div class="box">
  169.             <div class="box-body table-responsive padding">
  170.               <button class="btn btn-success" onclick="add_person()"><i class="glyphicon glyphicon-plus"></i> Add Person</button>
  171.               <button class="btn btn-default" onclick="reload_table()"><i class="glyphicon glyphicon-refresh"></i> Reload</button>
  172.               <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
  173.                 <thead>
  174.                   <tr>
  175.                     <th>Nama Produk</th>
  176.                     <th>Kategori</th>
  177.                     <th>Sub Kategori</th>
  178.                     <th>Supersub Kategori</th>
  179.                     <th style="width:125px;">Action</th>
  180.                   </tr>
  181.                 </thead>
  182.                 <tbody></tbody>
  183.                 <tfoot>
  184.                   <tr>
  185.                     <th>Nama Produk</th>
  186.                     <th>Harga</th>
  187.                     <th>Warna</th>
  188.                     <th>Action</th>
  189.                   </tr>
  190.                 </tfoot>
  191.             </table>
  192.             </div>
  193.           </div>
  194.         </section>
  195.  
  196.       </div>
  197.  
  198.       <?php $this->load->view('Admin/footer'); ?>
  199.  
  200.     </div>
  201.    
  202.     <!-- jQuery 2.1.4 -->
  203.     <script src="<?php echo base_url() ?>assets/plugins/jQuery/jQuery-2.1.4.min.js"></script>
  204.     <!-- jQuery UI 1.11.2 -->
  205.     <script src="<?php echo base_url() ?>assets/plugins/jQueryUI/jquery-ui-1.10.3.min.js" type="text/javascript"></script>
  206.     <!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
  207.     <script>$.widget.bridge('uibutton', $.ui.button);</script>
  208.     <!-- Bootstrap 3.3.2 JS -->
  209.     <script src="<?php echo base_url() ?>assets/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>    
  210.     <!-- Slimscroll -->
  211.     <script src="<?php echo base_url() ?>assets/plugins/slimScroll/jquery.slimscroll.min.js" type="text/javascript"></script>
  212.     <!-- FastClick -->
  213.     <script src="<?php echo base_url() ?>assets/plugins/fastclick/fastclick.min.js"></script>
  214.     <!-- AdminLTE App -->
  215.     <script src="<?php echo base_url() ?>assets/dist/js/app.min.js" type="text/javascript"></script>    
  216.     <!-- DATA TABLES SCRIPT -->
  217.     <script src="<?php echo base_url() ?>assets/plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
  218.     <script src="<?php echo base_url() ?>assets/plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>
  219.    
  220.     <script type="text/javascript">
  221.     var save_method; //for save method string
  222.     var table;
  223.  
  224.     $(document).ready(function() {
  225.  
  226.         //datatables
  227.         table = $('#table').DataTable({
  228.  
  229.             "processing": true, //Feature control the processing indicator.
  230.             "serverSide": true, //Feature control DataTables' server-side processing mode.
  231.             "order": [], //Initial no order.
  232.  
  233.             // Load data for the table's content from an Ajax source
  234.             "ajax": {
  235.                 "url": "<?php echo site_url('Admin/produk/ajax_list')?>",
  236.                 "type": "POST"
  237.             },
  238.  
  239.             //Set column definition initialisation properties.
  240.             "columnDefs": [
  241.             {
  242.                 "targets": [ -1 ], //last column
  243.                 "orderable": false, //set not orderable
  244.             },
  245.             ],
  246.  
  247.         });
  248.  
  249.         //datepicker
  250.         $('.datepicker').datepicker({
  251.             autoclose: true,
  252.             format: "yyyy-mm-dd",
  253.             todayHighlight: true,
  254.             orientation: "top auto",
  255.             todayBtn: true,
  256.             todayHighlight: true,  
  257.         });
  258.  
  259.         //set input/textarea/select event when change value, remove class error and remove text help block
  260.         $("input").change(function(){
  261.             $(this).parent().parent().removeClass('has-error');
  262.             $(this).next().empty();
  263.         });
  264.         $("textarea").change(function(){
  265.             $(this).parent().parent().removeClass('has-error');
  266.             $(this).next().empty();
  267.         });
  268.         $("select").change(function(){
  269.             $(this).parent().parent().removeClass('has-error');
  270.             $(this).next().empty();
  271.         });
  272.  
  273.     });
  274.     </script>
Advertisement
Add Comment
Please, Sign In to add comment