Advertisement
Radityanugrap

ujilevel JS

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