Support category edit.

pull/9/head
ruibaby 2019-04-28 00:14:50 +08:00
parent 8daeaa2b42
commit b926977b12
3 changed files with 62 additions and 11 deletions

View File

@ -36,6 +36,21 @@ categoryApi.delete = categoryId => {
}) })
} }
categoryApi.get = categoryId => {
return service({
url: `${baseUrl}/${categoryId}`,
method: 'get'
})
}
categoryApi.update = (categoryId, category) => {
return service({
url: `${baseUrl}/${categoryId}`,
data: category,
method: 'put'
})
}
function concreteTree(parentCategory, categories) { function concreteTree(parentCategory, categories) {
categories.forEach(category => { categories.forEach(category => {
if (parentCategory.key === category.parentId) { if (parentCategory.key === category.parentId) {

View File

@ -160,7 +160,6 @@ export default {
data() { data() {
return { return {
title: '添加菜单', title: '添加菜单',
data: [],
formType: 'create', formType: 'create',
loading: false, loading: false,
columns, columns,

View File

@ -9,7 +9,7 @@
:xs="24" :xs="24"
:style="{ 'padding-bottom': '12px' }" :style="{ 'padding-bottom': '12px' }"
> >
<a-card title="添加分类目录"> <a-card :title="title">
<a-form layout="horizontal"> <a-form layout="horizontal">
<a-form-item <a-form-item
label="名称:" label="名称:"
@ -21,7 +21,7 @@
label="路径名称:" label="路径名称:"
help="* 这是文章路径上显示的名称,最好为英文" help="* 这是文章路径上显示的名称,最好为英文"
> >
<a-input v-model="categoryToCreate.slugNames" /> <a-input v-model="categoryToCreate.slugName" />
</a-form-item> </a-form-item>
<a-form-item label="上级目录:"> <a-form-item label="上级目录:">
<category-select-tree <category-select-tree
@ -42,8 +42,20 @@
<a-form-item> <a-form-item>
<a-button <a-button
type="primary" type="primary"
@click="handleCreateCategory" @click="handleSaveClick"
v-if="formType==='create'"
>保存</a-button> >保存</a-button>
<a-button-group v-else>
<a-button
type="primary"
@click="handleSaveClick"
>更新</a-button>
<a-button
type="dashed"
@click="handleAddCategory"
v-if="formType==='update'"
>返回添加</a-button>
</a-button-group>
</a-form-item> </a-form-item>
</a-form> </a-form>
</a-card> </a-card>
@ -134,6 +146,8 @@ export default {
components: { CategorySelectTree, CategoryTree }, components: { CategorySelectTree, CategoryTree },
data() { data() {
return { return {
title: '添加分类',
formType: 'create',
categories: [], categories: [],
categoryToCreate: {}, categoryToCreate: {},
loading: false, loading: false,
@ -145,25 +159,48 @@ export default {
}, },
methods: { methods: {
loadCategories() { loadCategories() {
this.loading = true
categoryApi.listAll(true).then(response => { categoryApi.listAll(true).then(response => {
this.categories = response.data.data this.categories = response.data.data
this.loading = false
}) })
}, },
handleCreateCategory() { handleSaveClick() {
categoryApi.create(this.categoryToCreate).then(response => { this.createOrUpdateCategory()
this.$message.success('添加成功!') },
this.loadCategories() handleAddCategory() {
this.categoryToCreate = {} this.title = '添加分类'
}) this.formType = 'create'
this.categoryToCreate = {}
}, },
handleEditCategory(id) { handleEditCategory(id) {
this.$message.success('编辑' + id) categoryApi.get(id).then(response => {
this.categoryToCreate = response.data.data
this.title = '编辑分类'
this.formType = 'update'
})
}, },
handleDeleteCategory(id) { handleDeleteCategory(id) {
categoryApi.delete(id).then(response => { categoryApi.delete(id).then(response => {
this.$message.success('删除成功!') this.$message.success('删除成功!')
this.loadCategories() this.loadCategories()
}) })
},
createOrUpdateCategory() {
if (this.categoryToCreate.id) {
categoryApi.update(this.categoryToCreate.id, this.categoryToCreate).then(response => {
this.$message.success('更新成功!')
this.loadCategories()
this.categoryToCreate = {}
})
} else {
categoryApi.create(this.categoryToCreate).then(response => {
this.$message.success('保存成功!')
this.loadCategories()
this.categoryToCreate = {}
})
}
this.handleAddCategory()
} }
} }
} }