Guest User

Untitled

a guest
Dec 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  <head>
  4.    <title>!DOCTYPE</title>
  5.    <meta charset="utf-8">
  6.    <style>
  7.    td{
  8.     padding: 10px;
  9.     border: 1px solid black;
  10.     text-align: center;
  11.    }
  12.    td a{
  13.     display: block;
  14.     padding:20px;
  15.    }
  16.    td input{
  17.     margin: 5px;
  18.    }
  19.    </style>
  20.  </head>
  21.  <body>
  22.         <center>
  23.             <div id="app" style="width: 800px;">
  24.                
  25.                     <table>
  26.                         <tr>
  27.                             <th>Товар</th>
  28.                             <th>Цена</th>
  29.                             <th>Количество</th>
  30.                             <th>Сумма</th>
  31.                             <th>Удалить</th>
  32.                         </tr>
  33.                        
  34.                         <template v-for="item, index in items">
  35.                             <tr>
  36.                                 <td v-bind:style="{backgroundColor: item.nameBg}" v-if="!item.editName" v-on:click="editItem(index, 'name')">{{ item.name }}</td>
  37.                                 <td style="background-color: yellow" v-if="item.editName" v-on:keyup.enter="saveItem(index, 'name')"><input type="text" v-model="item.name"><br>press enter</td>
  38.                                
  39.                                 <td v-bind:style="{backgroundColor: item.priceBg}" v-if="!item.editPrice"  v-on:click="editItem(index, 'price')">{{ item.price }}</td>
  40.                                 <td style="background-color: yellow" v-if="item.editPrice" v-on:keyup.enter="saveItem(index, 'price')"><input type="text" v-model="item.price"><br>press enter</td>
  41.                                
  42.                                 <td v-bind:style="{backgroundColor: item.quantityBg}" v-if="!item.editQuantity" v-on:click="editItem(index, 'quantity')">{{ item.quantity }}</td>
  43.                                 <td style="background-color: yellow" v-if="item.editQuantity" v-on:keyup.enter="saveItem(index, 'quantity')"><input type="text" v-model="item.quantity"><br>press enter</td>
  44.                                
  45.                                 <td>{{ item.price * item.quantity }}</td>
  46.                                
  47.                                 <td><a href="#" v-on:click="delItem(index)">удалить</a></td>
  48.                             </tr>
  49.                         </template>
  50.                        
  51.                        
  52.                     </table>
  53.  
  54.                     <div style="border: 1px solid black; margin: 20px;">ИТОГО: {{ summary() }}</div>
  55.            
  56.                     <form action="#">
  57.                     <input type="text" v-model="name" placeholder="name">
  58.                     <input type="text" v-model="price" placeholder="price">
  59.                     <input type="text" v-model="quantity" placeholder="quantity">
  60.                     <button v-on:click="addItem()" v-bind:disabled="name == '' || price == '' || quantity == ''">Добавить</button>
  61.                     <p v-if="name == '' || price == '' || quantity == ''" style="color:red;">Вы не заполнили все поля</p>
  62.                     </form>
  63.             </div>
  64.         </center>
  65.    
  66.     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  67.        <script>
  68.         let app = new Vue({
  69.             el: '#app',
  70.             data: {
  71.                 items: [
  72.                 ],
  73.                 name: "",
  74.                 price: "",
  75.                 quantity: "",
  76.                 change: true,
  77.             },
  78.             methods:{
  79.                 changeBg: function(index,type,color){
  80.                     switch(type){
  81.                         case "name":
  82.                             this.items[index].nameBg = color;
  83.                             break;
  84.                         case "price":
  85.                             this.items[index].priceBg = color; 
  86.                             break;
  87.                         case "quantity":
  88.                             this.items[index].quantityBg = color;  
  89.                             break; 
  90.                     }
  91.                 },
  92.                 editItem: function(index, type){
  93.                     switch(type){
  94.                         case "name":
  95.                             this.items[index].editName = true; 
  96.                             break;
  97.                         case "price":
  98.                             this.items[index].editPrice = true;
  99.                             break;
  100.                         case "quantity":
  101.                             this.items[index].editQuantity = true; 
  102.                             break; 
  103.                     }
  104.                 },
  105.                 saveItem: function(index, type){
  106.                     switch(type){
  107.                         case "name":
  108.                             this.items[index].editName = false;
  109.                             this.changeBg(index, type, "green");
  110.                             setTimeout(this.changeBg, 1000, index, type, "white");
  111.                             break;
  112.                         case "price":
  113.                             this.items[index].editPrice = false;   
  114.                             this.changeBg(index, type, "green");
  115.                             setTimeout(this.changeBg, 1000, index, type, "white");
  116.                             break;
  117.                         case "quantity":
  118.                             this.items[index].editQuantity = false;
  119.                             this.changeBg(index, type, "green");
  120.                             setTimeout(this.changeBg, 1000, index, type, "white");
  121.                             break; 
  122.                     }
  123.                 },
  124.                 delItem: function(index){
  125.                     this.items.splice(index, 1);
  126.                 },
  127.                 addItem: function(){
  128.                     item = {
  129.                         name: this.name,
  130.                         editName: false,
  131.                         nameBg: "white",
  132.                         price: this.price,
  133.                         editPrice: false,
  134.                         priceBg: "white",
  135.                         quantity: this.quantity,
  136.                         editQuantity: false,
  137.                         quantityBg: "white",
  138.                     };
  139.                     this.items.push(item);
  140.                     this.name = "";
  141.                     this.price = "";
  142.                     this.quantity = "";
  143.                 },
  144.                 summary: function(){
  145.                     price = 0;
  146.                     for (let item of this.items)price += item.price*item.quantity;
  147.                     return price;
  148.                 },
  149.             },
  150.         });
  151.    </script>
  152.    
  153.  </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment