<script>
export default {
data: () => ({
dialog: false,
search: '',
headers: [
{
text: 'Question',
align: 'left',
value: 'title',
sortable: false
},
{ text: 'User', value: 'user' },
{ text: 'Category', value: 'category.name' },
{ text: 'Body', value:'body'},
{ text: 'Tags', value:'tags'},
{ text: 'Actions', value: 'name', sortable: false }
],
categories:[],
items: [],
editedIndex: -1,
editedItem: {
title: '',
category: '',
body:''
},
defaultItem: {
title: '',
user: 0,
category: '',
body:''
}
}),
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
}
},
watch: {
dialog (val) {
val || this.close()
}
},
created () {
this.initialize()
this.getCategories()
},
methods: {
initialize () {
axios.get('/api/question')
.then(res => this.items = res.data.data)
},
editItem (item) {
this.editedIndex = this.items.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialog = true
},
deleteItem (item) {
const index = this.items.indexOf(item)
confirm('Are you sure you want to delete this item?') && this.items.splice(index, 1)
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.items[this.editedIndex], this.editedItem)
} else {
this.items.push(this.editedItem)
}
this.close()
},
getCategories(){
axios.get('/api/category')
.then(res => this.categories = res.data.data)
}
}
}
</script>