Advertisement
Radityanugrap

Uji Level

Sep 14th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function showData() {
  3.     tampil.innerHTML = ""
  4.  
  5.     for (i = 0; i < nama.length; i++) {
  6.         nodata = i + 1
  7.         tampil.innerHTML += "<tr>" +
  8.             "<th>" + nodata + "</th>" +
  9.             "<td>" + kodeproduk[i] + "</td>" +
  10.             "<td>" + nama[i] + "</td>" +
  11.             "<td>" + kategori[i] + "</td>" +
  12.             "<td id='stock"+ nodata +"'>" + stok[i] + "</td>" +
  13.             "<td><img src=" + gambar[i] + " id='image' width='100' height=110'></td>" +
  14.             "<td>" +
  15.             "<button class='btn btn-warning mr-2' onclick='editdata(" +
  16.             i +
  17.             ")'>Edit</button>" +
  18.             "<button class='btn-danger btn' onclick='hapusdata(" + i +
  19.             ")'>Hapus</button>";
  20.         var stocks = document.getElementById('stock' + nodata + '')
  21.         if (Number(stocks.innerHTML) < 10) {
  22.             stocks.style.backgroundColor = "red"
  23.             stocks.style.color = "white"
  24.         } else {
  25.             stocks.style.backgroundColor = ""
  26.             stocks.style.color = ""
  27.         }
  28.     }
  29. }
  30.  
  31. function batal() {
  32.     document.getElementById('kodeProduk').value  = ""
  33.     document.getElementById('namaProduk').value  = ""
  34.     document.getElementById('hargaProduk').value = ""
  35.     document.getElementById('satuan').value      = ""
  36.     document.getElementById('kategori').value    = ""
  37.     document.getElementById('stokAwal').value    = ""
  38.     document.getElementById('urlGambar').value   = ""
  39.     document.getElementById("button9").style.display = "inline"
  40.     document.getElementById("button5").style.display = "none"
  41.     document.getElementById("button").style.display  = "none"
  42. }
  43.  
  44. function add() {
  45.     var newkode = document.getElementById('kodeProduk').value
  46.     var newnama = document.getElementById('namaProduk').value
  47.     var newharga = document.getElementById('hargaProduk').value
  48.     var newsatuan = document.getElementById('satuan').value
  49.     var newkategori = document.getElementById('kategori').value
  50.     var newstok = document.getElementById('stokAwal').value
  51.     var newgambar = document.getElementById('urlGambar').value
  52.  
  53.     if (newnama == '') {
  54.         alert('silahkan masukkan data terlebih dahulu!')
  55.     } else {
  56.         kodeproduk.push(newkode)
  57.         nama.push(newnama)
  58.         harga.push(newharga)
  59.         satuan.push(newsatuan)
  60.         gambar.push(newgambar)
  61.         kategori.push(newkategori)
  62.         stok.push(newstok)
  63.         showData()
  64.         document.getElementById('kodeProduk').value = ""
  65.         document.getElementById('namaProduk').value = ""
  66.         document.getElementById('hargaProduk').value = ""
  67.         document.getElementById('satuan').value = ""
  68.         document.getElementById('kategori').value = ""
  69.         document.getElementById('stokAwal').value = ""
  70.         document.getElementById('urlGambar').value = ""
  71.     }
  72. }
  73. var index = []
  74.  
  75. function editdata(i) {
  76.     document.getElementById('kodeProduk').value = kodeproduk[i]
  77.     document.getElementById('namaProduk').value = nama[i]
  78.     document.getElementById('urlGambar').value = gambar[i]
  79.     document.getElementById('satuan').value = satuan[i]
  80.     document.getElementById('hargaProduk').value = harga[i]
  81.     document.getElementById('kategori').value = kategori[i]
  82.     document.getElementById('stokAwal').value = stok[i]
  83.  
  84.     document.getElementById("button9").style.display = "none"
  85.     document.getElementById("button6").style.display = "inline"
  86.     document.getElementById("button").style.display = "inline"
  87.     index.push(i)
  88. }
  89.  
  90. function editSimpan() {
  91.     var newkode = document.getElementById('kodeProduk').value
  92.     var newnama = document.getElementById('namaProduk').value
  93.     var newharga = document.getElementById('hargaProduk').value
  94.     var newsatuan = document.getElementById('satuan').value
  95.     var newkategori = document.getElementById('kategori').value
  96.     var newstok = document.getElementById('stokAwal').value
  97.     var newgambar = document.getElementById('urlGambar').value
  98.  
  99.     if (newnama == '') {
  100.         alert('silahkan masukkan data terlebih dahulu!')
  101.     } else {
  102.         kodeproduk[index] = newkode
  103.         nama[index] = newnama
  104.         harga[index] = newharga
  105.         satuan[index] = newsatuan
  106.         gambar[index] = newgambar
  107.         kategori[index] = newkategori
  108.         stok[index] = newstok
  109.         showData()
  110.         index.pop()
  111.         document.getElementById('kodeProduk').value = ""
  112.         document.getElementById('namaProduk').value = ""
  113.         document.getElementById('hargaProduk').value = ""
  114.         document.getElementById('satuan').value = ""
  115.         document.getElementById('kategori').value = ""
  116.         document.getElementById('stokAwal').value = ""
  117.         document.getElementById('urlGambar').value = ""
  118.         document.getElementById("button9").style.display = "inline"
  119.         document.getElementById("button6").style.display = "none"
  120.         document.getElementById("button").style.display = "none"
  121.     }
  122. }
  123.  
  124. function hapusdata(i) {
  125.     if (confirm('apakah kamu yakin ingin menghapus data?')) {
  126.         nama.splice(i, 1);
  127.         kodeproduk.splice(i, 1);
  128.         kategori.splice(i, 1);
  129.         stok.splice(i, 1);
  130.         gambar.splice(i, 1);
  131.  
  132.         showData()
  133.     }
  134. }
  135.  
  136. function kodep(value) {
  137.     var idkode = []
  138.     var kodekatagori = value.split(" ").filter(function (e) {
  139.         return e.trim().length > 0;
  140.     });
  141.     for (i = 0; i < kodekatagori.length; i++) {
  142.         if (kodekatagori.length == 1) {
  143.             var code = kodekatagori[i].slice(0, 2)
  144.         } else {
  145.             var code = kodekatagori[i].slice(0, 1)
  146.         }
  147.         idkode.push(code)
  148.     }
  149.     var kodepr = idkode.join("")
  150.     kodepr = kodepr.toUpperCase()
  151.     if (idkode.length == 0) {
  152.         document.getElementById("kodeProduk").value = ''
  153.     } else {
  154.         var count = 1;
  155.         for (i = 0; i < kategori.length; ++i) {
  156.             if (kategori[i].toLowerCase() == value.toLowerCase()) {
  157.                 count++;
  158.             }
  159.         }
  160.         if (count < 10) {
  161.             document.getElementById("kodeProduk").value = kodepr + '-00' + count
  162.         } else if (count < 100) {
  163.             document.getElementById("kodeProduk").value = kodepr + '-0' + count
  164.         } else {
  165.             document.getElementById("kodeProduk").value = kodepr + '-' + count
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement