Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.34 KB | None | 0 0
  1. <script>
  2.  
  3.   var basket = [];
  4.  
  5.   function Pizza() {
  6.     this.id = Math.random();
  7.     this.name = document.getElementById('pizza-name').value || "Name";
  8.     this.desc = document.getElementById('pizza-desc').value || "Desc";
  9.     this.cost = document.getElementById('pizza-cost').value || Math.round(Math.random()*100)%30;
  10.     this.count = 0;
  11.  
  12.     var elem = document.createElement("tr");
  13.  
  14.     var tdName = document.createElement("td");
  15.     tdName.innerHTML = this.name;
  16.     var tdDesc = document.createElement("td");
  17.     tdDesc.innerHTML = this.desc;
  18.     var tdCost = document.createElement("td");
  19.     tdCost.innerHTML = this.cost;
  20.     var tdCount = document.createElement("td");
  21.     tdCount.innerHTML = this.count;
  22.     tdCount.id = this.id;
  23.     var btnAdd = document.createElement("td");
  24.     btnAdd.innerHTML += "<button onclick='Add("+this.id+")'>Add</button>";
  25.     btnAdd.innerHTML += "<button onclick='Remove("+this.id+")'>Remove</button>"
  26.  
  27.     elem.appendChild(tdName);
  28.     elem.appendChild(tdDesc);
  29.     elem.appendChild(tdCost);
  30.     elem.appendChild(tdCount);
  31.     elem.appendChild(btnAdd);
  32.  
  33.     document.getElementById('tbody').appendChild(elem);
  34.   }
  35.  
  36.   function Add(id) {
  37.     for (var i = 0; i < basket.length; i++) {
  38.      if (basket[i].id == id)
  39.      {
  40.        basket[i].count++;
  41.      }
  42.    }
  43.    Refresh();
  44.  }
  45.  
  46.  function Remove(id) {
  47.    for (var i = 0; i < basket.length; i++) {
  48.      if (basket[i].id == id && basket[i].count>0)
  49.       {
  50.         basket[i].count--;
  51.       }
  52.     }
  53.     Refresh();
  54.   }
  55.  
  56.   function CreatePizza() {
  57.     basket.push(new Pizza());
  58.     document.getElementById('pizza-name').value = "";
  59.     document.getElementById('pizza-desc').value = "";
  60.     document.getElementById('pizza-cost').value = "";
  61.   }
  62.  
  63.   function Refresh() {
  64.     basket.forEach(function (item) {
  65.       document.getElementById(item.id).innerHTML = item.count;
  66.     });
  67.   }
  68.  
  69.   window.onload = function () {
  70.  
  71.   };
  72. </script>
  73. <input id="pizza-name"><br>
  74. <input id="pizza-desc"><br>
  75. <input id="pizza-cost" type="number"><br>
  76. <button id="add-pizza" onclick="CreatePizza()">Add neew pizza 2 system</button>
  77. <h3>Basket</h3>
  78. <table>
  79.   <thead>
  80.     <tr>
  81.       <td>Name</td>
  82.       <td>Desc</td>
  83.       <td>Cost</td>
  84.       <td>Count</td>
  85.       <td>Controls</td>
  86.     </tr>
  87.   </thead>
  88.   <tbody id="tbody">
  89.   </tbody>
  90. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement