Advertisement
Guest User

Untitled

a guest
May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <table class="table table-striped">
  3.         <tbody>
  4.         <tr>
  5.             <th style="width: 10px"><input type="checkbox" name="all_elements" id="all_elements" class="" v-model="allSelected"></th>
  6.             <th style="width: 10px">ID</th>
  7.             <th>Город</th>
  8.             <th>Создан</th>
  9.             <th>Название</th>
  10.             <th>Количество</th>
  11.             <th>Артикул</th>
  12.             <th>Статус</th>
  13.             <th>Тип</th>
  14.         </tr>
  15.         <tr v-for="(element, index) in elements" :key="element.id">
  16.             <td><input type="checkbox" :name="'checked' + element.id" :id="'checked' + element.id" class="" v-model="checked[index]" :key="'checkbox' + element.id"></td>
  17.             <td>{{ element.id }}.</td>
  18.             <td>{{ element.returnable && element.returnable.location.name || element.regradingable.location.name }}</td>
  19.             <td>{{ element.created_at }}</td>
  20.             <td>{{ element.product.name_ikea }} {{ element.product.name }}</td>
  21.             <td>
  22.                 <span v-if="!checked[index]" :key="'quantity' + element.id">{{ element.quantity }}</span>
  23.                 <input v-else v-model="element.quantity" :id="element.id" type="text" class="form-control" style="width:50px" :key="'input_quantity' + element.id">
  24.             </td>
  25.             <td>{{ element.product.code_dots }}</td>
  26.             <td>
  27.                 <span v-if="!checked[index]" :key="'status' + element.id">{{ element.status.name }}</span>
  28.                 <select v-else v-model="element.status.id" class="form-control input-group-sm" :id="'statuses' + element.id" :key="'select_status' + element.id">
  29.                     <option v-for="status in statuses" :key="status.id" :value="status.id">{{
  30.                         status.name}}
  31.                     </option>
  32.                 </select>
  33.             </td>
  34.             <td>{{ element.returnable_type && element.returnable_type || element.regradingable_type }}</td>
  35.         </tr>
  36.         <tr>
  37.             <td colspan="9">
  38.                 <button type="button" id="save" class="btn btn-primary" @click="saveChanges">Сохранить изменения</button>
  39.             </td>
  40.         </tr>
  41.  
  42.        </tbody>
  43.     </table>
  44.  
  45. </template>
  46.  
  47. <script>
  48.  
  49.     export default {
  50.         props: ['elements', 'statuses', 'type'],
  51.         mounted() {
  52.             this.elements.forEach((item, index) => {
  53.                 this.checked[index] = false
  54.             })
  55.                     //console.log(order.status)
  56.         },
  57.         methods: {
  58.             saveChanges(){
  59.                 this.checked.forEach((item, index) => {
  60.                     if (item === true) {
  61.                         axios.put('/' + this.type + '/' + this.elements[index].id, {
  62.                             quantity: this.elements[index].quantity,
  63.                             product_return_status_id: this.elements[index].status.id,
  64.                             product_regrading_status_id: this.elements[index].status.id,
  65.                         })
  66.                             .then(response => {
  67.                                 console.log(this.checked[index])
  68.                                 this.checked[index] = false
  69.                                 console.log(this.checked[index])
  70.                                 this.$toast.open({
  71.                                     message: 'Элемент успешно сохранен!',
  72.                                     type: 'success',
  73.                                     position: 'top'
  74.                                 })
  75.                             })
  76.                             .catch(function (error) {
  77.                                 console.log(error)
  78.                             });
  79.                     }
  80.                 })
  81.  
  82.             },
  83.         },
  84.         computed: {
  85.         },
  86.         data() {
  87.             return {
  88.                 checked: [],
  89.                 allSelected: false
  90.             };
  91.         },
  92.         watch: {
  93.             allSelected(newValue, oldValue){
  94.                 this.elements.forEach((item, index) => {
  95.                     this.checked[index] = newValue
  96.                 })
  97.             },
  98.             checked: {
  99.                 handler: function (after, before) {
  100.                     // Return the object that changed
  101.                     console.log(after)
  102.                     if (after.indexOf(true) == -1 ) this.allSelected = false
  103.                     if (after.indexOf(false) == -1 ) this.allSelected = true
  104.                 },
  105.                 deep: true,
  106.             }
  107.         }
  108.     }
  109. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement