Skip to content

Instantly share code, notes, and snippets.

@kabitacode
Created April 19, 2019 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kabitacode/7fbcfd8d459555bca31e43405af2ed47 to your computer and use it in GitHub Desktop.
Save kabitacode/7fbcfd8d459555bca31e43405af2ed47 to your computer and use it in GitHub Desktop.
<template>
<div>
<form @submit.prevent="add">
<input type="hidden" v-model="form.id">
<input type="text" v-model="form.name">
<button type="submit" v-show="!updateSubmit">add</button>
<button type="button" v-show="updateSubmit" @click="update(form)">Update</button>
</form>
<ul v-for="user in users" :key="user.idd">
<li>
<span>{{user.name}}</span> &#160;
<button @click="edit(user)">Edit</button> || <button @click="del(user)">Delete</button>
</li>
</ul>
</div>
</template>
<script>
/* eslint-disable */
import axios from 'axios'
export default {
data(){
return{
form: {
id: '',
name: ''
},
users: '',
updateSubmit: false
}
},
mounted() {
this.load()
},
methods: {
load(){
axios.get('http://localhost:3000/users').then(res => {
this.users = res.data
}).catch ((err) => {
console.log(err);
})
},
add(){
axios.post('http://localhost:3000/users/', this.form).then(res => {
this.load()
this.form.name = ''
})
},
edit(user){
this.updateSubmit = true
this.form.id = user.id
this.form.name = user.name
},
update(form){
return axios.put('http://localhost:3000/users/' + form.id , {name: this.form.name}).then(res => {
this.load()
this.form.id = ''
this.form.name = ''
this.updateSubmit = false
}).catch((err) => {
console.log(err);
})
},
del(user){
axios.delete('http://localhost:3000/users/' + user.id).then(res =>{
this.load()
let index = this.users.indexOf(form.name)
this.users.splice(index,1)
})
}
}
}
</script>
@DeveloperArthur
Copy link

incredible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment