From cd63058ac0636f15163fa080218db766a771ec33 Mon Sep 17 00:00:00 2001 From: johnniang Date: Thu, 11 Apr 2019 19:44:33 +0800 Subject: [PATCH] Add CategoryTree component --- src/api/category.js | 30 ++++- src/views/post/PostEdit.vue | 109 +++++++++++------- .../post/components/CategorySelectTree.vue | 4 +- src/views/post/components/CategoryTree.vue | 35 ++++++ 4 files changed, 135 insertions(+), 43 deletions(-) create mode 100644 src/views/post/components/CategoryTree.vue diff --git a/src/api/category.js b/src/api/category.js index 61c26f84..6f5bfff1 100644 --- a/src/api/category.js +++ b/src/api/category.js @@ -18,7 +18,7 @@ categoryApi.listTree = () => { }) } -categoryApi.create = (category) => { +categoryApi.create = category => { return service({ url: baseUrl, data: category, @@ -33,4 +33,32 @@ categoryApi.delete = categoryId => { }) } +function concreteTree(parentCategory, categories) { + categories.forEach(category => { + if (parentCategory.key === category.parentId) { + if (!parentCategory.children) { + parentCategory.children = [] + } + parentCategory.children.push({ + key: category.id, + title: category.name + }) + } + }) + + if (parentCategory.children) { + parentCategory.children.forEach(category => concreteTree(category, categories)) + } +} + +categoryApi.concreteTree = categories => { + const topCategoryNode = { + key: 0, + title: 'top', + children: [] + } + concreteTree(topCategoryNode, categories) + return topCategoryNode.children +} + export default categoryApi diff --git a/src/views/post/PostEdit.vue b/src/views/post/PostEdit.vue index 4153a7c1..18b8441e 100644 --- a/src/views/post/PostEdit.vue +++ b/src/views/post/PostEdit.vue @@ -1,7 +1,13 @@ + +