Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Contoh Operasi CRUD Codeigniter :
- VIEW
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
- <div class="page-title">
- <div class="title_left">
- <h2>
- <i><a href="#"><?php echo $kategori?></a></i>
- <i class="fa fa-angle-double-right"></i>
- <a href="<?php echo site_url($alamat)?>"><?php echo $title?></a>
- </h2>
- </div>
- <div class="title_right">
- <div class="col-md-8 col-sm-8 col-xs-8">
- <button type="button" id="btn-tambah" onclick="add()" class="btn btn-success pull-right">
- <span class="glyphicon glyphicon-plus"></span> Tambah Data
- </button>
- </div>
- </div>
- </div>
- <div class="clearfix"></div>
- <br />
- <div class="row">
- <div class="col-md-10 col-sm-10 col-xs-10">
- <div class="x-panel">
- <div class="x_title">
- <h2><?php echo $title?></h2>
- <div class="clearfix"></div>
- </div>
- <div class="x_content">
- <div id="view">
- <table id="brand" class="table table-bordered table-striped table-hover">
- <thead>
- <tr class="headings">
- <th>ID</th>
- <th>Brand</th>
- <th>Buatan</th>
- <th>Updated</th>
- <th>Keterangan</th>
- <th colspan = "2" class="text-center"><span class="glyphicon glyphicon-cog"></span></th>
- </tr>
- </thead>
- <tbody>
- <?php
- if ($data_brand->num_rows() > 0)
- {
- $i=1;
- foreach($data_brand->result_array() as $keys=>$key)
- {
- if ($i%2 == 0)
- $class = "odd pointer";
- else $class = "even pointer";
- ?>
- <tr class=<?php echo $class?>>
- <td class="align-middle"><?php print $key['id_brand']?></td>
- <td class="align-middle"><?php print $key['brand']?></td>
- <td class="align-middle"><?php print $key['made']?></td>
- <td class="align-middle"><?php print $key['updated']?></td>
- <td class="align-middle"><?php print $key['keterangan']?></td>
- <td class="align-middle text-center">
- <a href="javascript:void();" onclick="edit(<?php echo $key['id_brand']; ?>);" class="btn btn-default" title="Edit"><span class="glyphicon glyphicon-pencil"></span></a>
- </td>
- <td class="align-middle text-center">
- <a href="javascript:void();" onclick="hapus(<?php echo $key['id_brand']; ?>);" class="btn btn-danger" title="Delete"><span class="glyphicon glyphicon-erase"></span></a>
- </td>
- </tr>
- <?php
- $i++;
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- <script>
- $(document).ready( function () {
- $('#brand').DataTable();
- });
- var save_method;
- var table;
- function add()
- {
- save_method = 'add';
- $('#form')[0].reset(); // reset form on modals
- $('#form-modal').modal('show'); // show bootstrap modal
- //$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal title
- }
- function edit(id)
- {
- save_method = 'update';
- $('#form')[0].reset(); // reset form on modals
- //Ajax Load data from ajax
- $.ajax({
- url : "<?php echo site_url('brand/ajax_edit/')?>/" + id,
- type: "GET",
- dataType: "JSON",
- success: function(data)
- {
- $('[name="id_brand"]').val(data.id_brand).attr("readonly","readonly");
- $('[name="brand"]').val(data.brand).attr("readonly","readonly");
- $('[name="made"]').val(data.made);
- $('[name="keterangan"]').val(data.keterangan);
- $('#form-modal').modal('show'); // show bootstrap modal when complete loaded
- $('.modal-title').text('Edit Brand'); // Set title to Bootstrap modal title
- },
- error: function (jqXHR, textStatus, errorThrown)
- {
- alert('Error get data from ajax');
- }
- });
- }
- function save()
- {
- var url;
- if(save_method == 'add')
- {
- url = "<?php echo site_url('brand/tambah')?>";
- }
- else
- {
- url = "<?php echo site_url('brand/edit')?>";
- }
- // ajax adding data to database
- $.ajax({
- url : url,
- type: "POST",
- data: $('#form').serialize(),
- dataType: "JSON",
- success: function(data)
- {
- //if success close modal and reload ajax table
- $('#form_modal').modal('hide');
- location.reload();// for reload a page
- },
- error: function (jqXHR, textStatus, errorThrown)
- {
- alert('Error adding / update data');
- }
- });
- }
- function hapus(id)
- {
- if(confirm('delete this data?'))
- {
- // ajax delete data from database
- $.ajax({
- url : "<?php echo site_url('brand/konfirm')?>/"+id,
- type: "POST",
- dataType: "JSON",
- beforeSend: function(e) {
- if(e && e.overrideMimeType) {
- e.overrideMimeType("application/json;charset=UTF-8");
- }
- },
- success: function(response)
- {
- alert(response.pesan);
- $("#view").html(response.html);
- location.href = location.pathname;
- },
- error: function (jqXHR, textStatus, errorThrown)
- {
- alert('Error deleting data');
- }
- });
- }
- }
- $(document).ready(function(){
- $('#form-modal').on('hidden.bs.modal', function (e){
- $("#btn-reset").click();
- $("#brand").removeAttr('readonly');
- });
- });
- </script>
- <!-- Bootstrap modal -->
- <div class="modal fade" id="form-modal" role="dialog">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- <h3 class="modal-title">Form Data Brand</h3>
- </div>
- <div class="modal-body form">
- <form action="#" id="form" class="form-horizontal">
- <input type="hidden" value="" name="id_brand"/>
- <div class="form-body">
- <div class="form-group">
- <label class="control-label col-md-3">Brand</label>
- <div class="col-md-9">
- <input name="brand" placeholder="Nama Brand" class="form-control" type="text">
- </div>
- </div>
- <div class="form-group">
- <label class="control-label col-md-3">Made/Buatan</label>
- <div class="col-md-9">
- <input name="made" placeholder="Made/Buatan" class="form-control" type="text">
- </div>
- </div>
- <div class="form-group">
- <label class="control-label col-md-3">Keterangan</label>
- <div class="col-md-9">
- <textarea rows="6" cols="3" name="keterangan" class="form-control"></textarea>
- </div>
- </div>
- </div>
- </form>
- </div>
- <div class="modal-footer">
- <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
- <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
- </div>
- </div><!-- /.modal-content -->
- </div><!-- /.modal-dialog -->
- </div><!-- /.modal -->
- <!-- End Bootstrap modal -->
- CONTROLLER
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Brand extends CI_Controller
- {
- private $table = 'tb_brand';
- private $primary = 'id_brand';
- function __construct()
- {
- parent::__construct();
- $this->load->library(array('template'));
- $user = $this->session->userdata('username');
- if ($user)
- {
- $lvl = $this->session->userdata('level');
- if($lvl != '1')
- {
- redirect('dashboard/index');
- }
- }
- else redirect('web');
- }
- function index()
- {
- $lvl = $this->session->userdata('level');
- $kategori = $this->_get_nama($lvl);
- $data_brand = $this->m_all->semua($this->table);
- $data['kategori'] = $kategori;
- $data['alamat'] = 'brand/index';
- $data['data_brand'] = $data_brand;
- $data['title']="List Brand";
- $this->template->display('brand/index',$data);
- }
- function tambah()
- {
- $brand = db_clean($this->input->post('brand'));
- $made = db_clean($this->input->post('made'));
- if ($brand and $made)
- {
- $cek = $this->m_all->cari($brand, $made, '','','','brand','made','',$this->table);
- if ($cek->num_rows() > 0)
- $response = array('status'=>'gagal','pesan'=>'Data brand telah tersedia');
- else
- {
- $info = array(
- 'id_brand' => $this->auth->get_jumlah($this->primary, $this->table) + 1,
- 'brand' => $brand,
- 'made' => $made,
- 'keterangan' => db_clean($this->input->post('keterangan')),
- 'updated' => $this->session->userdata('nama').'-'.date('Y-m-d H:i:s')
- );
- $this->m_all->simpan($info, $this->table);
- ob_start();
- $html = ob_get_contents();
- ob_end_clean();
- $response = array('status'=>'true','pesan'=>'Data berhasil disimpan', 'html'=>$html);
- }
- }
- else $response = array('status'=>'false','pesan'=>'Data brand dan Made kosong');
- echo json_encode($response);
- }
- function ajax_edit($id)
- {
- $data = $this->m_all->get_by_id($this->table, $this->primary, $id);
- echo json_encode($data);
- }
- function edit()
- {
- $kode = $this->input->post('id_brand');
- $made = db_clean($this->input->post('made'));
- if ($kode and $made)
- {
- $cek = $this->m_all->cek($kode, $this->primary, $this->table);
- foreach ($cek->result() as $row)
- {
- $updated = $row->updated;
- }
- $info = array(
- 'made' => $made,
- 'keterangan' => db_clean($this->input->post('keterangan')),
- 'updated' => $updated.';'.$this->session->userdata('nama').'-'.date('Y-m-d H:i:s')
- );
- $this->m_all->update($kode, $info, $this->primary, $this->table);
- ob_start();
- $html = ob_get_contents();
- ob_end_clean();
- $response = array('status'=>'true','pesan'=>'Data berhasil di-ubah', 'html'=>$html);
- }
- else $response = array('status'=>'false','pesan'=>'Data gagal di-ubah');
- echo json_encode($response);
- }
- function konfirm($id)
- {
- $cek = $this->m_all->cek($id, 'brand', 'tb_itemofbasic');
- if ($cek->num_rows() > 0)
- {
- foreach ($cek->result() as $row)
- {
- $aktif = $row->aktif;
- }
- }
- else $aktif = 0;
- if ($aktif == 0)
- {
- $this->m_all->hapus($id, '', $this->primary, '',$this->table);
- ob_start();
- $html = ob_get_contents();
- ob_end_clean();
- $response = array('status'=>'true', 'pesan'=> 'Data berhasil di-hapus','html'=>$html);
- }
- else $response = array('status'=>'false','pesan'=>'Data product atas brand, aktif');
- echo json_encode($response);
- }
- function _get_nama($lvl)
- {
- $get_nm_lvl = $this->m_all->getNama('level', $lvl, null, 'level_nama', 'id_level');
- if ($get_nm_lvl->num_rows() > 0)
- {
- foreach ($get_nm_lvl->result() as $gnl)
- {
- $p = $gnl->level_nama;
- }
- }
- else $p = '';
- return $p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement