vue2 + echats树状图 点击按钮 展开所有节点/收起所有节点

vue2 + echats树状图 点击按钮 展开所有节点/收起所有节点

<template>
    <div>
        <button @click="expandAll(isExpanded)">
            {{ isExpanded ? '一键收起' : '一键展开' }}
        </button>
        <div ref="echartsTree" style="width: 600px; height: 400px;"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
    data() {
        return {
            isExpanded: true,
            myChart: null,
            option: {}
        };
    },
    methods: {
        getList() {
            this.option = {
                series: [
                    {
                        type: 'tree',
                        data: [
                            {
                                name: 'root',
                                children: [
                                    { name: 'child1' },
                                    { name: 'child2', children: [{ name: 'grandchild1' }] }
                                ]
                            }
                        ],
                        expandAndCollapse: true // 开启节点展开折叠
                    }
                ]
            }
        },
        // 一键展开
        expandAll(e) {
            if (e == false) {
                this.handleClose();
                this.isExpanded = !e;
            } else {
                this.option.series = {
                    type: 'tree',
                    data: [{
                        name: 'root'
                    }]
                }
                this.myChart.setOption(this.option);
                this.isExpanded = !e;
            }
        },
        // 一键关闭
        handleClose() {
            this.getList();
            this.myChart.setOption(this.option);
        },
        initChart() {
            this.myChart = echarts.init(this.$refs.echartsTree);
            this.myChart.setOption(this.option);
        }
    },
    mounted() {
        this.getList();
        this.$nextTick(() => {
            this.initChart()
        })
    }
};
</script>

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 23:48:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 23:48:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 23:48:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 23:48:05       18 阅读

热门阅读

  1. Html_Css问答集(2)

    2024-06-15 23:48:05       7 阅读
  2. 在面试中展示自己的系统架构设计能力

    2024-06-15 23:48:05       7 阅读
  3. 网络安全练气篇——OWASP TOP 10

    2024-06-15 23:48:05       8 阅读
  4. 什么是js防抖节流?

    2024-06-15 23:48:05       7 阅读
  5. Spring框架的原理及应用详解(二)

    2024-06-15 23:48:05       7 阅读