How to edit and update form input values in vue.js?

Viewed 7276

I am getting up the details in a form for a hotel entry with basic details and viewing it in Room.vue. The created values were displayed here but here i need to give edit option for the filled details. When i click the edit, the page should redirect to RoomsEdit.vue and i should get the filled up contents in that page with form inputs. For that i have tried the following codes but nothing solves my issue.. Kindly have a go through and help me in solving the issue.

Room.vue:

<table class="table table-striped table-hover rooms-table">
            <thead>
              <tr>
                <td>Title<i class="fa fa-sort"></i></td>
                <td>Sub Title<i class="fa fa-sort"></i></td>
                <td> Edit </td> 
                <td> Delete </td>
              </tr>
            </thead>

            <tbody>
              <tr v-for="room in items">
                <td>{{ room.title }}</td>
                <td>{{ room.subtitle }}</td>
                <td>
                  <router-link class="btn btn-primary" v-bind:to="'rooms/edit/'+id">Edit</router-link>
                </td>
                <td>
                  <button class="btn btn-primary" v-on:click="deleteRoom(room)">Delete</button>
                </td>
              </tr>
            </tbody>
          </table>

Here i am giving edit option and making a redirect to edit page..

Script of Rooms.vue:

<script>

import config from '../../../config';

export default {
  data(){
    return{
      items: [],
      itemsData:{
        title : '',
        subtitle : '',
      }
    }
  },

  methods: {
    deleteRoom: function (room) {
      var index = this.items.indexOf(room);
      this.items.splice(index, 1);
    }
  },

  mounted() {
    axios.get(config.apiDomain+'/Rooms').then((response)=>this.items = response.data);
  }
}
</script>

RoomsEdit.vue:

  <form @submit.prevent="updateItems" class="form-horizontal" enctype="multipart/form-data">
                <div class="form-group">
                  <label for="title" class="col-sm-2 control-label">Title<span class="red">*</span></label>
                  <div class="col-sm-6">
                    <input type="text" name="title" class="form-control" id="title" v-model="itemsData.title">
                    <span class="text-danger">{{ errors.title?errors.title[0]:"" }}</span>
                  </div>
                </div>
                <div class="form-group">
                  <label for="subtitle" class="col-sm-2 control-label">Subtitle<span class="red">*</span></label>
                  <div class="col-sm-6">
                    <input type="text" name="subtitle" class="form-control" id="subtitle" v-model="itemsData.subtitle">
                    <span class="text-danger">{{ errors.subtitle?errors.subtitle[0]:"" }}</span>
                  </div>
                </div>
</form>

Script of RoomsEdit:

<script>
import config from '../../../config';
export default {
  data(){
    return{
      items: [],
      itemsData:{
        title : '',
        subtitle : '',
      },

      errors: {

      }
    }
  },

  methods:{

    fetchRoom(id){
      axios.get(config.apiDomain+'/Rooms').then((response)=>this.items = response.data);
    },

    updateItems(e){
      axios.put(config.apiDomain+'/Rooms/edit'+this.$route.params.id,this.itemsData).then(response=>{
        this.this.itemsData = "";
        this.$router.push('/admin/rooms');
      }).catch(error=>{
        this.errors = error.response.data;
      });
    }
  },
  created: function() {
    this.fetchRoom(this.$route.params.id);
  }
}
</script>

RoomsCreate.vue:

<form @submit.prevent="addItems" class="form-horizontal" enctype="multipart/form-data">
                <div class="form-group">
                  <label for="title" class="col-sm-2 control-label">Title<span class="red">*</span></label>
                  <div class="col-sm-6">
                    <input type="text" name="title" class="form-control" id="title" v-model="itemsData.title">
                    <span class="text-danger">{{ errors.title?errors.title[0]:"" }}</span>
                  </div>
                </div>
                <div class="form-group">
                  <label for="subtitle" class="col-sm-2 control-label">Subtitle<span class="red">*</span></label>
                  <div class="col-sm-6">
                    <input type="text" name="subtitle" class="form-control" id="subtitle" v-model="itemsData.subtitle">
                    <span class="text-danger">{{ errors.subtitle?errors.subtitle[0]:"" }}</span>
                  </div>
                </div>

Script of RoomsCreate.vue:

<script>
import config from '../../../config';
export default {
  data(){
    return{
      items: [],
      itemsData:{
        title : '',
        subtitle : '',
      },

      errors: {

      }
    }
  },

  methods:{

    addItems(){
      axios.post(config.apiDomain+'/Rooms',this.itemsData).then(response=>{
        this.this.itemsData = "";
        this.$router.push('/admin/rooms');
      }).catch(error=>{
        this.errors = error.response.data;
      });
  }
}
</script>

The issue i am facing is when i click the edit in Room.vue, it redirects to the RoomsEdit.vue where i am not getting the values that was already created, i need to get those value when i go to RoomsEdit.vue there i should edit and update the content.

0 Answers
Related