Skip to content

Instantly share code, notes, and snippets.

@Terumi
Created July 13, 2018 14:35
Show Gist options
  • Save Terumi/42bf118f0f32469c6aeadf83200969d7 to your computer and use it in GitHub Desktop.
Save Terumi/42bf118f0f32469c6aeadf83200969d7 to your computer and use it in GitHub Desktop.
Vue component for inline editing
<template>
<div class="editable">
<div class="" v-if="!removed">
<div v-if="!edit">
{{newtext}}
<button class="btn btn-xs btn-info btn-editable" @click="edittext">
<i class="fa fa-edit"></i>
</button>
</div>
<div v-if="edit">
<div class="" v-if="inputype != 'textarea'">
<div class="input-group input-group-sm">
<input type="text" class="text-editable input-sm form-control" v-model="newtext">
<span class="input-group-btn">
<button class="btn btn-warning" @click="canceledit"><i class="fa fa-times"></i></button>
<button v-if="deleteUrl" class="btn btn-danger" onclick="return confirm('Είστε σίγουροι οτι θέλετε να διαγράψετε αυτή την εγγραφή;');" @click="remove"><i class="fa fa-trash"></i></button>
<button class="btn btn-success" @click="update"><i class="fa fa-check"></i></button>
</span>
</div>
</div>
<div class="" v-if="inputype == 'textarea'">
<textarea class="text-editable form-control mb15" v-model="newtext" style="display: block;"></textarea>
<button class="btn btn-secondary btn-warning btn-xs" @click="canceledit"><i class="fa fa-times"></i>
</button>
<button class="btn btn-secondary btn-success btn-xs" @click="update"><i class="fa fa-check"></i>
</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: ['url', 'text', 'inputype', 'deleteUrl'],
data: function () {
return {
edit: false,
newtext: this.text
};
},
mounted() {
this.removed = false
},
methods: {
success(response){
//var response_data = JSON.parse(response.data);
var response_data = response.data;
if (parseInt(response_data.status) != 200) {
//console.log(response_data.errors[Object.keys(response_data.errors)[0]]);
alert('error');
} else {
this.edit = false;
}
},
error(response){
//console.log(response.errors[Object.keys(response.errors)[0]]);
alert(response.body.errors[Object.keys(response.body.errors)[0]]);
},
edittext(){
this.edit = true;
},
canceledit(){
this.newtext = this.text;
this.edit = false;
},
update(){
var data = {name: this.newtext};
this.$http.post(this.url, data).then(this.success, this.error);
},
remove(){
this.$http.post(this.deleteUrl).then(this.success, this.error);
this.removed = true;
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment