| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <template>
- <div class="tree-wrapper">
- <el-tree
- :data="treeList"
- :props="defaultProps"
- v-loading="treeLoading"
- :node-key="nodeKey"
- ref="tree"
- :highlight-current="true"
- :expand-on-click-node="false"
- @node-click="handleNodeClick"
- v-bind="$attrs"
- :default-expand-all="defaultExpandAll"
- :default-expanded-keys="defaultExpandedKeys"
- >
- </el-tree>
- </div>
- </template>
- <script>
- import { getProduceTreeByCode, getTreeByPid } from '@/api/custom';
- import { getProjectAndCategoryLevelTree } from '@/api/project-manage/index.js';
- // let originId = '';
- // let originType = '';
- export default {
- props: {
- // treeList私有化处理
- treeFormate: {
- type: Function,
- default: null
- },
- defaultProps: {
- type: Object,
- default: function () {
- return {
- children: 'children',
- value: 'id',
- label: 'name'
- };
- }
- },
- defaultExpandAll: {
- type: Boolean,
- default: function () {
- return true;
- }
- },
- defaultExpandedKeys: {
- type: Array,
- default: () => {
- return [];
- }
- },
- // 初始请求treeList
- init: {
- type: Boolean,
- default: true
- },
- isFirstRefreshTable: {
- type: Boolean,
- default: true
- },
- id: {
- type: String,
- default: '0'
- },
- code: {
- type: String,
- default: ''
- },
- nodeKey: {
- type: String,
- default: 'id'
- },
- isProjects: {
- type: Boolean,
- default: false
- }
- // appendRoot: {
- // type: Boolean,
- // default: false
- // },
- },
- data() {
- return {
- treeList: [],
- treeLoading: false,
- parentName: '',
- parentId: '',
- currentKey: ''
- };
- },
- async mounted() {
- if (this.init) {
- if (this.code) {
- let typeObj = await getProduceTreeByCode(this.code);
- await this.getTreeData(typeObj[0]?.id);
- } else {
- await this.getTreeData(this.id);
- }
- }
- },
- methods: {
- getInstance() {
- return this.$refs.tree;
- },
- // 获取树结构数据
- async getTreeData(id, defChecked) {
- try {
- this.treeLoading = true;
- const res = this.isProjects
- ? await getProjectAndCategoryLevelTree()
- : await getTreeByPid(id);
- this.treeLoading = false;
- if (res?.code === '0') {
- this.treeList = res.data;
- this.$emit('setRootId', res.data[0].id);
- if (this.treeFormate) {
- this.treeList = this.treeFormate(this.treeList);
- }
- this.$nextTick(() => {
- if(this.isProjects){
- this.setCurrentKey(defChecked);
- }
- // 默认高亮第一级树节点
- if (this.treeList[0] && this.isFirstRefreshTable) {
- this.setCurrentKey(this.treeList[0].id);
- this.handleNodeClick(
- this.treeList[0],
- this.$refs.tree.getCurrentNode()
- );
- }
- });
- return this.treeList;
- }
- } catch (error) {}
- this.treeLoading = false;
- },
- handleNodeClick(data, node) {
- this.$emit('handleNodeClick', data, node);
- },
- // 设置默认高亮行
- setCurrentKey(id) {
- this.currentKey = id;
- this.$refs.tree.setCurrentKey(this.currentKey);
- },
- // 获取树的选中状态
- getSelectList() {
- const selectList = this.$refs.tree.getCurrentNode();
- return selectList;
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .tree-wrapper {
- width: 100%;
- height: 100%;
- overflow: auto;
- :deep(.el-tree) {
- display: inline-block;
- min-width: 100%;
- }
- }
- </style>
|