欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Vue实现一个树组件

程序员文章站 2022-07-04 22:26:05
HTML代码: Vue Demo

html代码:

<!doctype html>
<html>
<head>
    <title>vue demo</title>

    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style type="text/css">
        /* span样式 */
        .treeviewitem-span {
            font-size: 14px;
        }

        /* 箭头样式 */
        .treeviewitem-arrow-icon {
            margin-top: 3px;
            margin-left: 3px;
            margin-right: 3px;
            float: left;
            width: 0;
            height: 0;
            border-top-width: 6px;
            border-right-width: 0px;
            border-bottom-width: 6px;
            border-left-width: 6px;
            border-style: solid;
            border-color: transparent transparent transparent #666666;
            transform: rotate(0deg);
        }

        /* 90度旋转箭头样式 */
        .treeviewitem-arrow-icon90 {
            margin-top: 3px;
            margin-left: 3px;
            margin-right: 3px;
            float: left;
            width: 0;
            height: 0;
            border-top-width: 6px;
            border-right-width: 0px;
            border-bottom-width: 6px;
            border-left-width: 6px;
            border-style: solid;
            border-color: transparent transparent transparent #666666;
            transform: rotate(90deg);
        }

        /* 模板隐藏 */
        template {
            display: none;
        }
    </style>

    <script type="text/javascript" src="vue.js"></script>
    <script type="text/javascript">

    </script>
</head>
<body>
    <!-- treeviewitem模板 -->
    <template id="treeviewitem">
        <div>
            <div style="background-color:transparent; cursor:default; height:25px;"><div v-on:click="expand" style="width:16px; height:16px; float:left; background-color:transparent;"><span v-bind:class="[self.expand ? 'treeviewitem-arrow-icon90':'treeviewitem-arrow-icon']" v-if="isleaf"></span></div><input type="checkbox" v-on:click="checkboxclick" v-model="self.selected" /><span v-on:click="itemclick" class="treeviewitem-span">{{ self.name }}</span></div>
            <div v-if="self.expand" v-for="item in items" style="margin-left:20px;">
                <treeviewitem v-bind:items="item.items" v-bind:self="item"></treeviewitem>
            </div>
        </div>
    </template>

    <!-- treeview模板 -->
    <template id="treeview">
        <div>
            <div v-for="item in items">
                <treeviewitem v-bind:items="item.items" v-bind:self="item"></treeviewitem>
            </div>
        </div>
    </template>

    <div id="app">
        <!-- 使用treeview组件 -->
        <treeview v-bind:items="items"></treeview>
        <br />
        <button onclick="showselectedresult()">勾选结果</button>
    </div>

    <script type="text/javascript">
        //定义treeviewitem组件
        vue.component('treeviewitem', {
            props: ['items', 'self'],
            template: '#treeviewitem',
            methods: {
                itemclick: function (d) {
                    alert("您单击了节点:" + this.self.name);
                },
                checkboxclick: function (e) {
                    var checkchild;
                    checkchild = function (items, checked) {
                        for (var i = 0; i < items.length; i++) {
                            var item = items[i];
                            item.selected = checked;
                            if (item.items) {
                                checkchild(item.items, checked)
                            }
                        }
                    };

                    if (e.target.checked) {
                        checkchild(this.items, true);
                    } else {
                        checkchild(this.items, false);
                    }
                },
                expand: function (e) {
                    if (this.self.expand) {
                        this.self.expand = false;
                    } else {
                        this.self.expand = true;
                    }
                }
            },
            computed: {
                isleaf: function () {
                    if (this.items && this.items.length > 0) {
                        return true;
                    }
                    return false;
                }
            }
        });

        //定义treeview组件
        vue.component('treeview', {
            props: ['items'],
            template: '#treeview'
        });

        //定义vm
        var vm = new vue({
            el: '#app',
            methods: {
                /**
                 * @description 获取勾选结果
                 */
                getselected: function (items) {
                    if (!items) items = this.items;

                    var result = [];
                    for (var i = 0; i < items.length; i++) {
                        var item = items[i];
                        if (item.selected) {
                            result.push(item.name);
                        }
                        if (item.items) {
                            var childselected = this.getselected(item.items);
                            for (var k = 0; k < childselected.length; k++) {
                                result.push(childselected[k]);
                            }
                        }
                    }
                    return result;
                }
            },
            data: {
                items: [
                    {
                        name: '条目1',
                        expand: true,
                        selected: false,
                        items: [
                            {
                                name: '条目11',
                                expand: true,
                                selected: false,
                                items: [
                                    {
                                        name: '条目111',
                                        expand: true,
                                        selected: true,
                                    }
                                ]
                            },
                            {
                                name: '条目12',
                                expand: false,
                                selected: false,
                                items: [
                                    {
                                        name: '条目121',
                                        expand: true,
                                        selected: false,
                                    },
                                    {
                                        name: '条目122',
                                        expand: true,
                                        selected: false,
                                        items: [
                                            {
                                                name: '条目1221',
                                                expand: true,
                                                selected: false
                                            },
                                            {
                                                name: '条目1222',
                                                expand: true,
                                                selected: false
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                name: '条目13',
                                expand: true,
                                selected: false
                            }
                        ]
                    },
                    {
                        name: '条目2',
                        expand: true,
                        selected: false
                    },
                    {
                        name: '条目3',
                        expand: true,
                        selected: false,
                        items: [
                            {
                                name: "条目31",
                                expand: true,
                                selected: false
                            }
                        ]
                    }
                ]
            }
        })

        //显示勾选结果
        function showselectedresult() {
            var selected = vm.getselected();
            alert("您勾选了:" + selected.join(', '));
        }
    </script>
</body>
</html>

效果图:

使用Vue实现一个树组件