Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Kategori extends CI_Controller {
- public function __construct() {
- parent::__construct();
- $this->load->model("ModelKategori");
- isLogin();
- }
- public function index() {
- $listKategori = $this->ModelKategori->getAll();
- $data = array(
- "header" => "Kategori",
- "page" => "content/kategori/v_list_kategori",
- "kats" => $listKategori
- );
- $this->load->view("layout/main", $data);
- }
- public function tambah() {
- $data = array(
- "header" => "Kategori",
- "page" => "content/kategori/v_form_kategori",
- );
- $this->load->view("layout/main", $data);
- }
- public function proses_simpan() {
- $data = array(
- "nama_kategori" => $this->input->post("nama"),
- );
- $this->ModelKategori->insert($data);
- redirect("kategori");
- }
- public function update($idKategori) {
- $kategori = $this->ModelKategori->getByPrimaryKey($idKategori);
- $data = array(
- "header" => "Kategori",
- "page" => "content/kategori/v_update_kategori",
- "kats" => $kategori
- );
- $this->load->view("layout/main", $data);
- }
- public function proses_update() {
- $id = $this->input->post("id");
- $data = array(
- "nama_kategori" => $this->input->post("nama"),
- );
- $this->ModelKategori->update($id, $data);
- redirect("kategori");
- }
- public function proses_hapus() {
- $id = $this->input->post("id");
- $this->ModelKategori->delete($id);
- redirect("kategori");
- }
- }
- ======================================================
- <div class="card">
- <div class="card-header">
- <h4>Daftar Kategori</h4>
- </div>
- <div class="card-body">
- <table class="table">
- <thead>
- <tr>
- <th>#</th>
- <th>Nama</th>
- <th>Status</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $no = 1;
- foreach ($kats as $row) {
- ?>
- <tr>
- <td><?= $no++ ?></td>
- <td><?= $row->nama_kategori ?></td>
- <td><?= $row->is_active == "1" ? "Aktif" : "Tidak Aktif" ?></td>
- <td>
- <a href="<?= site_url("kategori/update/$row->id_kategori") ?>" class="btn btn-sm btn-warning">
- <i class="fas fa-edit"></i>
- </a>
- <a href="#" data-id="<?= $row->id_kategori ?>" class="btn btn-sm btn-danger btn-delete-kategori"><i
- class="fas fa-trash"></i></a>
- </td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- <div class="card-footer">
- <a href="<?= site_url("kategori/tambah") ?>" class="btn btn-primary">
- <i class="fas fa-plus"></i> Tambah Kategori
- </a>
- </div>
- </div>
- <div class="modal fade" id="modal-confirm-delete">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-body">
- <h4>Anda Yakin Hapus data ini?</h4>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-info" data-dismiss="modal">Tidak</button>
- <button type="button" class="btn btn-danger" id="btn-delete">Hapus</button>
- </div>
- </div>
- </div>
- </div>
- <form id="form-delete" method="post" action="<?= site_url('kategori/proses_hapus') ?>">
- </form>
- <script>
- $(function () {
- let idKat = 0;
- $(".btn-delete-kategori").on("click", function () {
- idKat = $(this).data("id");
- console.log(idKat);
- $("#modal-confirm-delete").modal("show");
- });
- $("#btn-delete").on("click", function () {
- //panggil url untuk hapus data
- let inputId = $("<input>")
- .attr("type", "hidden")
- .attr("name", "id")
- .val(idKat);
- let formDelete = $("#form-delete");
- formDelete.empty().append(inputId);
- formDelete.submit();
- $("#modal-confirm-delete").modal("hide");
- });
- });
- </script>
- ===================================================================
- <div class="card">
- <div class="card-header">
- <h4>Tambah Kategori</h4>
- </div>
- <div class="card-body">
- <form id="form-tambah" method="post" action="<?= site_url("kategori/proses_simpan") ?>">
- <div class="form-group">
- <label for="">Nama Kategori</label>
- <input required type="text" maxlength="20" name="nama" class="form-control"/>
- </div>
- </form>
- </div>
- <div class="card-footer">
- <button id="btn-save-kategori" type="button" class="btn btn-success">
- <i class="fas fa-save"></i> Simpan
- </button>
- </div>
- </div>
- <script>
- $(function () {
- $("#btn-save-kategori").on("click", function () {
- let validate = $("#form-tambah").valid();
- if(validate){
- $("#form-tambah").submit();
- }
- });
- $("#form-tambah-kategori").validate({
- errorElement: 'span',
- errorPlacement: function (error, element) {
- error.addClass('invalid-feedback');
- element.closest('.form-group').append(error);
- },
- highlight: function (element, errorClass, validClass) {
- $(element).addClass('is-invalid');
- },
- unhighlight: function (element, errorClass, validClass) {
- $(element).removeClass('is-invalid');
- }
- });
- });
- </script>
- ===================================================================
- <div class="card">
- <div class="card-header">
- <h4>Update Kategori</h4>
- </div>
- <div class="card-body">
- <form id="form-update-kategori" enctype="multipart/form-data" method="post"
- action="<?= site_url("kategori/proses_update") ?>">
- <div class="form-group">
- <label for="nama-kategori">Nama User</label>
- <input value="<?= $kats->nama_kategori ?>" type="text" name="nama" id="nama-kategori" class="form-control"/>
- </div>
- <input type="hidden" name="id" value="<?= $kats->id_kategori ?>" />
- </form>
- </div>
- <div class="card-footer">
- <button id="btn-save-kategori" type="button" class="btn btn-success">
- <i class="fas fa-save"></i> Simpan
- </button>
- </div>
- </div>
- <script>
- $(function () {
- $("#btn-save-kategori").on("click", function () {
- let validate = $("#form-update-kategori").valid();
- if(validate){
- $("#form-update-kategori").submit();
- }
- });
- $("#form-update-kategori").validate({
- messages: {
- kode: {
- alphanumeric: "Hanya Boleh Angka, Huruf dan Undescore"
- }
- },
- errorElement: 'span',
- errorPlacement: function (error, element) {
- error.addClass('invalid-feedback');
- element.closest('.form-group').append(error);
- },
- highlight: function (element, errorClass, validClass) {
- $(element).addClass('is-invalid');
- },
- unhighlight: function (element, errorClass, validClass) {
- $(element).removeClass('is-invalid');
- }
- });
- });
- </script>
- =============================================================
Add Comment
Please, Sign In to add comment