Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div class="Places">
  3.     <v-subheader>Place</v-subheader>
  4.     <v-data-table
  5.       :headers="headers"
  6.       :items="places"
  7.       :items-per-page="5"
  8.       :search="search"
  9.       sort-by="place"
  10.       class="elevation-1"
  11.     >
  12.       <template v-slot:item.placeImage="{ item }">
  13.         <v-img aspect-ratio="1" style="width: 200px; height:auto;" :src="item.placeImage"></v-img>
  14.       </template>
  15.       <template v-slot:top>
  16.         <v-toolbar flat color="white">
  17.           <v-toolbar-title>Places</v-toolbar-title>
  18.           <v-divider class="mx-4" inset vertical></v-divider>
  19.           <v-spacer></v-spacer>
  20.           <v-text-field
  21.             v-model="search"
  22.             append-icon="search"
  23.             label="Search"
  24.             single-line
  25.             hide-details
  26.           ></v-text-field>
  27.           <v-divider class="mx-4" inset vertical></v-divider>
  28.           <v-dialog v-model="dialog" max-width="500px">
  29.             <template v-slot:activator="{ on }">
  30.               <v-btn color="secondary" dark class="mb-2" v-on="on">New Item</v-btn>
  31.             </template>
  32.             <v-card>
  33.               <v-card-title>
  34.                 <span class="headline">{{ formTitle }}</span>
  35.               </v-card-title>
  36.               <v-card-text>
  37.                 <v-container grid-list-md>
  38.                   <v-layout wrap>
  39.                     <v-flex xs12 sm6 md4>
  40.                       <v-text-field v-model="editedItem.place" label="Place"></v-text-field>
  41.                     </v-flex>
  42.                     <v-flex xs12 sm6 md4>
  43.                       <v-text-field v-model="editedItem.city" label="City"></v-text-field>
  44.                     </v-flex>
  45.                     <v-flex xs12 sm6 md4>
  46.                       <!-- <v-text-field v-model="editedItem.placeImage" label="Image"></v-text-field> -->
  47.                       <v-file-input
  48.                         :rules="rules"
  49.                         accept="image/png, image/jpeg, image/bmp"
  50.                         placeholder="Pick an image"
  51.                         prepend-icon="mdi-camera"
  52.                         v-model="editedItem.placeImage"
  53.                         label="Image"
  54.                       ></v-file-input>
  55.                       <!-- <v-file-input
  56.                         :rules="rules"
  57.                         v-model="editedItem.placeImage"
  58.                         accept="image/png, image/jpeg, image/bmp"
  59.                         placeholder="Pick an image"
  60.                         prepend-icon="mdi-camera"
  61.                         label="Image"
  62.                       ></v-file-input>-->
  63.                     </v-flex>
  64.                   </v-layout>
  65.                 </v-container>
  66.               </v-card-text>
  67.  
  68.               <v-card-actions>
  69.                 <v-spacer></v-spacer>
  70.                 <v-btn color="secondary darken-1" text @click="close">Cancel</v-btn>
  71.                 <v-btn color="secondary darken-1" text @click="save">Save</v-btn>
  72.               </v-card-actions>
  73.             </v-card>
  74.           </v-dialog>
  75.         </v-toolbar>
  76.       </template>
  77.       <template v-slot:item.action="{ item }">
  78.         <v-icon small class="mr-2" @click="editItem(item)">edit</v-icon>
  79.         <v-icon small @click="deleteItem(item)">delete</v-icon>
  80.       </template>
  81.       <template v-slot:no-data>
  82.         <v-btn color="secondary" @click="initialize">Reset</v-btn>
  83.       </template>
  84.     </v-data-table>
  85.   </div>
  86. </template>
  87.  
  88. <script>
  89. import { mapState } from "vuex";
  90. import axios from "axios";
  91.  
  92. export default {
  93.   data: () => ({
  94.     search: "",
  95.     dialog: false,
  96.     headers: [
  97.       { text: "Place", value: "place" },
  98.       { text: "City", value: "city" },
  99.       { text: "Image", value: "placeImage", sortable: false },
  100.       { text: "Actions", value: "action", sortable: false }
  101.     ],
  102.     places: [],
  103.     rules: [
  104.       value =>
  105.         !value || value.size < 2000000 || "Image size should be less than 2 MB!"
  106.     ],
  107.     editedIndex: -1,
  108.     editedItem: {
  109.       place: "",
  110.       city: "",
  111.       placeImage: []
  112.     },
  113.     defaultItem: {
  114.       place: "",
  115.       city: "",
  116.       placeImage: []
  117.     }
  118.   }),
  119.  
  120.   computed: {
  121.     formTitle() {
  122.       return this.editedIndex === -1 ? "New Item" : "Edit Item";
  123.     },
  124.     ...mapState(["placesData"])
  125.   },
  126.   mounted: function() {
  127.     this.$store.dispatch("loadPlacesData");
  128.   },
  129.   watch: {
  130.     placesData: function() {
  131.       this.places = this.placesData.data;
  132.     },
  133.     dialog(val) {
  134.       val || this.close();
  135.     }
  136.   },
  137.   created() {
  138.     this.initialize();
  139.   },
  140.   methods: {
  141.     initialize() {
  142.       this.places;
  143.     },
  144.     editItem(item) {
  145.       this.editedIndex = this.places.indexOf(item);
  146.       this.editedItem = Object.assign({}, item);
  147.       this.dialog = true;
  148.     },
  149.     deleteItem(item) {
  150.       const index = this.places.indexOf(item);
  151.       confirm("Are you sure you want to delete this item?") &&
  152.         this.places.splice(index, 1);
  153.       axios.delete("/api/places/" + item._id).then(response => {
  154.         console.log(response);
  155.       });
  156.     },
  157.     close() {
  158.       this.dialog = false;
  159.       setTimeout(() => {
  160.         this.editedItem = Object.assign({}, this.defaultItem);
  161.         this.editedIndex = -1;
  162.       }, 300);
  163.     },
  164.     save() {
  165.       if (this.editedIndex > -1) {
  166.         axios
  167.           .patch("/api/places/" + this.editedItem._id, {
  168.             place: this.editedItem.place,
  169.             city: this.editedItem.city,
  170.             placeImage: this.editedItem.placeImage
  171.           })
  172.           .then(response => {
  173.             console.log(reseponse);
  174.           });
  175.         Object.assign(this.places[this.editedIndex], this.editedItem);
  176.       } else {
  177.         // axios
  178.         //   .post("/api/places/", {
  179.         //     place: this.editedItem.place,
  180.         //     city: this.editedItem.city,
  181.         //     image: this.editedItem.image
  182.         //   })
  183.         //   .then(response => {
  184.         //     console.log(reseponse);
  185.         //   });
  186.         // this.places.push(this.editedItem);
  187.         axios
  188.           .post(
  189.             "/api/places",
  190.             {
  191.               place: this.editedItem.place,
  192.               city: this.editedItem.city,
  193.               placeImage: this.editedItem.placeImage
  194.             },
  195.             {
  196.               headers: {
  197.                 "Content-Type": "multipart/form-data"
  198.               }
  199.             }
  200.           )
  201.           .then(response => {
  202.             console.log(response);
  203.           });
  204.         this.places.push(this.editedItem);
  205.       }
  206.       this.close();
  207.     }
  208.   }
  209. };
  210. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement