eyoku_

Untitled

Jun 16th, 2020
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Kategori extends CI_Controller {
  5. public function __construct() {
  6. parent::__construct();
  7. $this->load->model("ModelKategori");
  8. isLogin();
  9. }
  10.  
  11. public function index() {
  12. $listKategori = $this->ModelKategori->getAll();
  13. $data = array(
  14. "header" => "Kategori",
  15. "page" => "content/kategori/v_list_kategori",
  16. "kats" => $listKategori
  17. );
  18. $this->load->view("layout/main", $data);
  19. }
  20.  
  21. public function tambah() {
  22. $data = array(
  23. "header" => "Kategori",
  24. "page" => "content/kategori/v_form_kategori",
  25. );
  26. $this->load->view("layout/main", $data);
  27. }
  28.  
  29. public function proses_simpan() {
  30. $data = array(
  31. "nama_kategori" => $this->input->post("nama"),
  32. );
  33. $this->ModelKategori->insert($data);
  34. redirect("kategori");
  35. }
  36.  
  37. public function update($idKategori) {
  38. $kategori = $this->ModelKategori->getByPrimaryKey($idKategori);
  39. $data = array(
  40. "header" => "Kategori",
  41. "page" => "content/kategori/v_update_kategori",
  42. "kats" => $kategori
  43. );
  44. $this->load->view("layout/main", $data);
  45. }
  46.  
  47. public function proses_update() {
  48. $id = $this->input->post("id");
  49. $data = array(
  50. "nama_kategori" => $this->input->post("nama"),
  51. );
  52. $this->ModelKategori->update($id, $data);
  53. redirect("kategori");
  54. }
  55.  
  56. public function proses_hapus() {
  57. $id = $this->input->post("id");
  58. $this->ModelKategori->delete($id);
  59. redirect("kategori");
  60. }
  61. }
  62.  
  63. ======================================================
  64.  
  65. <div class="card">
  66. <div class="card-header">
  67. <h4>Daftar Kategori</h4>
  68. </div>
  69. <div class="card-body">
  70. <table class="table">
  71. <thead>
  72. <tr>
  73. <th>#</th>
  74. <th>Nama</th>
  75. <th>Status</th>
  76. <th>Action</th>
  77. </tr>
  78. </thead>
  79. <tbody>
  80. <?php
  81. $no = 1;
  82. foreach ($kats as $row) {
  83. ?>
  84. <tr>
  85. <td><?= $no++ ?></td>
  86. <td><?= $row->nama_kategori ?></td>
  87. <td><?= $row->is_active == "1" ? "Aktif" : "Tidak Aktif" ?></td>
  88.  
  89. <td>
  90. <a href="<?= site_url("kategori/update/$row->id_kategori") ?>" class="btn btn-sm btn-warning">
  91. <i class="fas fa-edit"></i>
  92. </a>
  93. <a href="#" data-id="<?= $row->id_kategori ?>" class="btn btn-sm btn-danger btn-delete-kategori"><i
  94. class="fas fa-trash"></i></a>
  95. </td>
  96. </tr>
  97. <?php
  98. }
  99. ?>
  100. </tbody>
  101. </table>
  102. </div>
  103. <div class="card-footer">
  104. <a href="<?= site_url("kategori/tambah") ?>" class="btn btn-primary">
  105. <i class="fas fa-plus"></i> Tambah Kategori
  106. </a>
  107. </div>
  108. </div>
  109. <div class="modal fade" id="modal-confirm-delete">
  110. <div class="modal-dialog">
  111. <div class="modal-content">
  112. <div class="modal-body">
  113. <h4>Anda Yakin Hapus data ini?</h4>
  114. </div>
  115. <div class="modal-footer">
  116. <button type="button" class="btn btn-info" data-dismiss="modal">Tidak</button>
  117. <button type="button" class="btn btn-danger" id="btn-delete">Hapus</button>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. <form id="form-delete" method="post" action="<?= site_url('kategori/proses_hapus') ?>">
  123.  
  124. </form>
  125. <script>
  126. $(function () {
  127. let idKat = 0;
  128. $(".btn-delete-kategori").on("click", function () {
  129. idKat = $(this).data("id");
  130. console.log(idKat);
  131. $("#modal-confirm-delete").modal("show");
  132. });
  133. $("#btn-delete").on("click", function () {
  134. //panggil url untuk hapus data
  135. let inputId = $("<input>")
  136. .attr("type", "hidden")
  137. .attr("name", "id")
  138. .val(idKat);
  139. let formDelete = $("#form-delete");
  140. formDelete.empty().append(inputId);
  141. formDelete.submit();
  142. $("#modal-confirm-delete").modal("hide");
  143. });
  144. });
  145. </script>
  146.  
  147. ===================================================================
  148.  
  149. <div class="card">
  150. <div class="card-header">
  151. <h4>Tambah Kategori</h4>
  152. </div>
  153. <div class="card-body">
  154. <form id="form-tambah" method="post" action="<?= site_url("kategori/proses_simpan") ?>">
  155. <div class="form-group">
  156. <label for="">Nama Kategori</label>
  157. <input required type="text" maxlength="20" name="nama" class="form-control"/>
  158. </div>
  159. </form>
  160. </div>
  161. <div class="card-footer">
  162. <button id="btn-save-kategori" type="button" class="btn btn-success">
  163. <i class="fas fa-save"></i> Simpan
  164. </button>
  165. </div>
  166. </div>
  167. <script>
  168. $(function () {
  169. $("#btn-save-kategori").on("click", function () {
  170. let validate = $("#form-tambah").valid();
  171. if(validate){
  172. $("#form-tambah").submit();
  173. }
  174. });
  175. $("#form-tambah-kategori").validate({
  176. errorElement: 'span',
  177. errorPlacement: function (error, element) {
  178. error.addClass('invalid-feedback');
  179. element.closest('.form-group').append(error);
  180. },
  181. highlight: function (element, errorClass, validClass) {
  182. $(element).addClass('is-invalid');
  183. },
  184. unhighlight: function (element, errorClass, validClass) {
  185. $(element).removeClass('is-invalid');
  186. }
  187. });
  188. });
  189. </script>
  190.  
  191.  
  192. ===================================================================
  193.  
  194. <div class="card">
  195. <div class="card-header">
  196. <h4>Update Kategori</h4>
  197. </div>
  198. <div class="card-body">
  199. <form id="form-update-kategori" enctype="multipart/form-data" method="post"
  200. action="<?= site_url("kategori/proses_update") ?>">
  201. <div class="form-group">
  202. <label for="nama-kategori">Nama User</label>
  203. <input value="<?= $kats->nama_kategori ?>" type="text" name="nama" id="nama-kategori" class="form-control"/>
  204. </div>
  205. <input type="hidden" name="id" value="<?= $kats->id_kategori ?>" />
  206. </form>
  207. </div>
  208. <div class="card-footer">
  209. <button id="btn-save-kategori" type="button" class="btn btn-success">
  210. <i class="fas fa-save"></i> Simpan
  211. </button>
  212. </div>
  213. </div>
  214. <script>
  215. $(function () {
  216. $("#btn-save-kategori").on("click", function () {
  217. let validate = $("#form-update-kategori").valid();
  218. if(validate){
  219. $("#form-update-kategori").submit();
  220. }
  221. });
  222. $("#form-update-kategori").validate({
  223.  
  224. messages: {
  225. kode: {
  226. alphanumeric: "Hanya Boleh Angka, Huruf dan Undescore"
  227. }
  228. },
  229. errorElement: 'span',
  230. errorPlacement: function (error, element) {
  231. error.addClass('invalid-feedback');
  232. element.closest('.form-group').append(error);
  233. },
  234. highlight: function (element, errorClass, validClass) {
  235. $(element).addClass('is-invalid');
  236. },
  237. unhighlight: function (element, errorClass, validClass) {
  238. $(element).removeClass('is-invalid');
  239. }
  240. });
  241. });
  242. </script>
  243.  
  244. =============================================================
Add Comment
Please, Sign In to add comment