Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------
- controller: Produk.php
- ---------------------------------------------
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Produk extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->model('Admin/m_produk','produk');
- }
- public function index()
- {
- $this->load->helper('url');
- $this->load->view('Admin/produk/v_lihat');
- }
- public function ajax_list()
- {
- $list = $this->produk->get_datatables();
- $data = array();
- $no = $_POST['start'];
- foreach ($list as $produk) {
- $no++;
- $row = array();
- $row[] = $produk->nama_produk;
- $row[] = $produk->kat;
- $row[] = $produk->subkat;
- $row[] = $produk->supersubkat;
- //add html for action
- $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>
- <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>';
- $data[] = $row;
- }
- $output = array(
- "draw" => $_POST['draw'],
- "recordsTotal" => $this->produk->count_all(),
- "recordsFiltered" => $this->produk->count_filtered(),
- "data" => $data,
- );
- //output to json format
- echo json_encode($output);
- }
- ---------------------------------------------
- model: m_produk.php
- ---------------------------------------------
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class M_produk extends CI_Model {
- var $table = 'produk';
- //set column field database for order and search
- var $column = array('nama_produk','kat','subkat','supersubkat');
- var $order = array('id_produk' => 'desc'); // default order
- private function _get_datatables_query()
- {
- $this->db->from($this->table);
- $i = 0;
- foreach ($this->column 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) - 1 == $i) //last loop
- $this->db->group_end(); //close bracket
- }
- $column[$i] = $item; // set column array variable to order processing
- $i++;
- }
- if(isset($_POST['order'])) // here order processing
- {
- $this->db->order_by($column[$_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']);
- $this->db->join('kategori', 'kategori.id_kat = produk.kat','left');
- $this->db->join('subkat', 'subkat.id_subkat = produk.subkat','left');
- $this->db->join('supersubkat', 'supersubkat.id_supersubkat = produk.supersubkat','left');
- $query = $this->db->get();
- return $query->result();
- }
- function count_filtered()
- {
- $this->_get_datatables_query();
- $query = $this->db->get();
- return $query->num_rows();
- }
- public function count_all()
- {
- $this->db->from($this->table);
- return $this->db->count_all_results();
- }
- public function get_by_id($id_produk)
- {
- $this->db->from($this->table);
- $this->db->where('id_produk',$id_produk);
- $query = $this->db->get();
- return $query->row();
- }
- ---------------------------------------------
- views: v_lihat.php
- ---------------------------------------------
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Daftar Produk | Admin Panel</title>
- <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
- <!-- Bootstrap 3.3.4 -->
- <link href="<?php echo base_url() ?>assets/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <!-- FontAwesome 4.3.0 -->
- <link href="<?php echo base_url() ?>assets/font-awesome-4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
- <!-- Theme style -->
- <link href="<?php echo base_url() ?>assets/dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />
- <link href="<?php echo base_url() ?>assets/dist/css/skins/skin-blue.min.css" rel="stylesheet" type="text/css" />
- <!-- DATA TABLES -->
- <link href="<?php echo base_url() ?>assets/plugins/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
- <link href="<?php echo base_url() ?>img/fav.ico" rel="shortcut icon"/>
- </head>
- <body class="skin-blue sidebar-mini">
- <div class="wrapper">
- <?php $this->load->view('Admin/header'); ?>
- <div class="content-wrapper">
- <section class="content-header">
- <h1>Daftar Produk <small>| <?php echo anchor('Admin/produk/tambah/','Tambah Produk') ?></small></h1>
- <ol class="breadcrumb">
- <li><a href="#"><i class="fa fa-dashboard"></i> Produk</a></li>
- <li class="active">Daftar Produk</li>
- </ol>
- </section>
- <section class="content">
- <div class="box">
- <div class="box-body table-responsive padding">
- <button class="btn btn-success" onclick="add_person()"><i class="glyphicon glyphicon-plus"></i> Add Person</button>
- <button class="btn btn-default" onclick="reload_table()"><i class="glyphicon glyphicon-refresh"></i> Reload</button>
- <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th>Nama Produk</th>
- <th>Kategori</th>
- <th>Sub Kategori</th>
- <th>Supersub Kategori</th>
- <th style="width:125px;">Action</th>
- </tr>
- </thead>
- <tbody></tbody>
- <tfoot>
- <tr>
- <th>Nama Produk</th>
- <th>Harga</th>
- <th>Warna</th>
- <th>Action</th>
- </tr>
- </tfoot>
- </table>
- </div>
- </div>
- </section>
- </div>
- <?php $this->load->view('Admin/footer'); ?>
- </div>
- <!-- jQuery 2.1.4 -->
- <script src="<?php echo base_url() ?>assets/plugins/jQuery/jQuery-2.1.4.min.js"></script>
- <!-- jQuery UI 1.11.2 -->
- <script src="<?php echo base_url() ?>assets/plugins/jQueryUI/jquery-ui-1.10.3.min.js" type="text/javascript"></script>
- <!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
- <script>$.widget.bridge('uibutton', $.ui.button);</script>
- <!-- Bootstrap 3.3.2 JS -->
- <script src="<?php echo base_url() ?>assets/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
- <!-- Slimscroll -->
- <script src="<?php echo base_url() ?>assets/plugins/slimScroll/jquery.slimscroll.min.js" type="text/javascript"></script>
- <!-- FastClick -->
- <script src="<?php echo base_url() ?>assets/plugins/fastclick/fastclick.min.js"></script>
- <!-- AdminLTE App -->
- <script src="<?php echo base_url() ?>assets/dist/js/app.min.js" type="text/javascript"></script>
- <!-- DATA TABLES SCRIPT -->
- <script src="<?php echo base_url() ?>assets/plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
- <script src="<?php echo base_url() ?>assets/plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- var save_method; //for save method string
- var 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('Admin/produk/ajax_list')?>",
- "type": "POST"
- },
- //Set column definition initialisation properties.
- "columnDefs": [
- {
- "targets": [ -1 ], //last column
- "orderable": false, //set not orderable
- },
- ],
- });
- //datepicker
- $('.datepicker').datepicker({
- autoclose: true,
- format: "yyyy-mm-dd",
- todayHighlight: true,
- orientation: "top auto",
- todayBtn: true,
- todayHighlight: true,
- });
- //set input/textarea/select event when change value, remove class error and remove text help block
- $("input").change(function(){
- $(this).parent().parent().removeClass('has-error');
- $(this).next().empty();
- });
- $("textarea").change(function(){
- $(this).parent().parent().removeClass('has-error');
- $(this).next().empty();
- });
- $("select").change(function(){
- $(this).parent().parent().removeClass('has-error');
- $(this).next().empty();
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment