You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

3 years ago
<template>
<treeselect
v-model="value1"
:options="menuOptions"
:show-count="true"
placeholder="选择上级菜单"
/>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {mapGetters} from "vuex";
3 years ago
3 years ago
export default {
name: "ProdCategory",
components: {Treeselect},
3 years ago
props: ['value', 'root'],
3 years ago
computed: {
...mapGetters(['productCategories']),
value1: {
get() {
return this.value;
},
set(v) {
this.$emit('input', v);
}
},
menuOptions() {
if (!this.productCategories || this.productCategories.length === 0) {
return [];
}
const map = {}
this.productCategories.forEach(it => {
let list = map[it.parentId]
if (!list) {
list = [];
map[it.parentId] = list;
}
list.push({id: it.id, label: it.name, parentId: it.parentId});
})
const stack = [...map['0']];
while (stack.length > 0) {
const p = stack.shift();
if (!map[p.id]) {
continue
}
p.children = map[p.id];
stack.push(...map[p.id])
}
3 years ago
if (this.root) {
return [{id: 0, label: '根节点', children: map['0']}];
}
return map['0'];
3 years ago
}
},
created() {
this.$store.dispatch('loadProductCategories')
}
}
</script>
<style scoped>
</style>