asimryu

addItemForm.vue

May 12th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.82 KB | None | 0 0
  1. <template>
  2.     <div class="addItem">
  3.         <input type="text" v-model="item.name" />
  4.         <input type="file" @change="onChange" accept="image/jpeg,image/gif,image/png" />
  5.         <img id="image" :src="item.image" alt="uploadimage" :class="[ item.image ? 'show' : 'hide']">
  6.         <button
  7.             @click="addItem()"
  8.             class="btn btn-danger btn-sm align-middle" v-bind:disabled="item.name === ''">
  9.             <i class="fas fa-plus"></i> ADD
  10.         </button>
  11.     </div>
  12. </template>
  13. <script>
  14. export default {
  15.     data: function () {
  16.         return {
  17.             item: {
  18.                 name: "",
  19.                 image: ""
  20.             }
  21.         }
  22.     },
  23.     methods: {
  24.         onChange(e) {
  25.             let imgfile = e.target.files[0];
  26.             this.getBase64Img(imgfile);
  27.         },
  28.         getBase64Img(imgfile) {
  29.             let reader = new FileReader();
  30.             reader.onload = (e) => {
  31.                 this.item.image = e.target.result;
  32.                 console.log(this.item.image);
  33.             };
  34.             reader.readAsDataURL(imgfile);
  35.         },
  36.         addItem() {
  37.             let headers = {
  38.                     'headers': {
  39.                         'Content-Type': 'application/json;charset=utf-8',
  40.                         'Accept': 'application/json',
  41.                         'Authorization': 'Bearer ' + localStorage.token                    
  42.                     }
  43.                 };         
  44.             if( this.item.name == '' ) return;
  45.             axios.post('api/item/store', {
  46.                 item: this.item
  47.             }, headers)
  48.             .then( response => {
  49.                 if( response.status == 201 ) {
  50.                     this.item.name = "";
  51.                     this.$emit('reloadlist');
  52.                 }
  53.             })
  54.             .catch(error => {
  55.                 console.log(error);
  56.             })
  57.         }
  58.     }
  59. }  
  60. </script>
  61. <style scoped>
  62. .addItem {
  63.     display: flex;
  64.     justify-content: center;
  65.     align-items: center;
  66. }
  67. input {
  68.     background: #f7f7f7;
  69.     border: 0px;
  70.     outline: none;
  71.     padding: 5px;
  72.     margin-right: 10px;
  73. }
  74. .plus {
  75.     font-size: 30px;
  76. }
  77. .active {
  78.     color: #00ce25;
  79. }
  80. .inactive {
  81.     color: #999999;
  82. }
  83. #image {
  84.     max-width: 100px;
  85.     max-height: 100px;
  86. }
  87. .hide { display: none; }
  88. .show { display: inline; }
  89. </style>
Add Comment
Please, Sign In to add comment