Advertisement
Radityanugrap

Uji Level

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