vue-树组件

话不多说,直接上代码
tree.vue:

<template>
<ul class="tree__ul">
    <treeItem
            v-for="(treeData,index) in treeDatas"
            :treeData="treeData"
            :key="index"
            @nodeItemClick="nodeClick"
            :options="options"
            :level="1"></treeItem>
</ul>
</template>
<script>
export default {
    data(){
        return{
            
        }
    },
    props:{
        treeDatas:Array,
        options:Object,
    },
    methods:{
        nodeClick(value){
            if(this._events["nodeClick"]){
                this.$emit('nodeClick', value)
            }
        },
        getSelectNode(){
            let nodes=[];
            nodes=this.getChildrenSelect(this.treeDatas);
            return nodes;
        },
        getChildrenSelect(children){
            let nodes=[];
            children.forEach((item,index)=>{
                if(item.children&&item.children.length){
                    let childNodes=this.getChildrenSelect(item.children);
                    childNodes.forEach((childItem,childIndex)=>{
                        nodes.push(childItem);
                    })
                }else if(item.checked){
                    nodes.push(item);
                }
            });
            return nodes;
        }
    },
    components:{
        treeItem:{
            name:"treeItem",
            data(){
                return{
                    isOpen:false,
                    isUnAllCheck:false
                }
            },
            props:{
                treeData:Object,
                options:Object,
                level:Number,
            },
            computed:{
                isFolder(){
                    return this.treeData.children&&this.treeData.children.length;
                }

            },
            created(){
                //默认全选
                if(this.options.isSelected){
                    this.treeData.checked=true;
                }
            },
            mounted(){
                this.isOpen=this.options.expandNum>=this.level;
            },
            methods:{
                changeType(){
                    if(this.isFolder){
                        this.isOpen=!this.isOpen;
                        //异步树展开节点的时候调用回调
                        if(this.isOpen&&this._events["expandNode"]){
                            this.$emit('expandNode', this.treeData)
                        }
                    }
                },
                nodeClick(value,level){
                    if(level){
                        this.currentNodeClick();
                    }else{
                        this.parentNodeState();
                    }

                    //是否有回调函数
                    if(this._events["nodeItemClick"]){
                        this.$emit('nodeItemClick', value)
                    }
                },
                currentNodeClick(){
                    this.isUnAllCheck=false;
                    this.treeData.checked=!this.treeData.checked;
                    let state=this.treeData.checked;
                    if(this.treeData.children&&this.treeData.children){
                        this.childStateChange(this.treeData.children,state);
                    }

                },
                childStateChange(children,state){
                    if(children&&children.length){
                        children.forEach((item,index)=>{
                            item.checked=state;
                            if(item.children&&item.children){
                                this.childStateChange(item.children,state);
                            }
                        });
                    }
                },
                parentNodeState(){
                    let state=this.getChildrenState(this.treeData.children);
                    if(state===null){
                        this.isUnAllCheck=true;
                    }else{
                        this.isUnAllCheck=false;
                        this.treeData.checked=state;
                    }
                },
                getChildrenState(children){
                    var state=false;
                    if(children&&children.length>0){
                        for(let i=0;i<children.length;i++){
                            let item=children[i];
                            //子节点没有选全
                            if(state!=item.checked&&i>0){
                                return null;
                            }
                            state=item.checked;
                            if(item.children&&item.children.length){
                                let childState=this.getChildrenState(item.children);
                                //子节点没有选全
                                if(childState==null||state!=childState)
                                    return null;
                            }
                        }
                    }
                    return state;
                }
            },
            template:`<li class="tree__li">
        <div :class="['tree__item--arrow',isOpen?'tree__item--arrow-down':'']" @click="changeType" v-if="isFolder"><i></i></div>
        <div class="tree__item" @click="nodeClick(treeData,level)">
            <span :class="['tree-icon', isFolder&&isOpen?'tree-folder_open':isFolder?'tree-folder':'tree-file']" v-if="options.hasicon"></span>
            <span :class="['tree-checkbox', isUnAllCheck?'tree-checkbox_unallchecked':treeData.checked?'tree-checkbox_checked':'tree-checkbox_unchecked']"></span>
            <span class="tree-text" >{{treeData.name}}</span>
            <span title="删除" class="glyphicon glyphicon-remove tree-delete" v-if="treeData.candelete">x</span>

        </div>
        <ul class="tree__ul" v-show="isOpen" v-if="isFolder">
            <treeItem v-for="(item,index) in treeData.children"
                  :treeData="item"
                  :key="index"
                  :options="options"
                  :level="level+1"
                  @nodeItemClick="nodeClick"
                  ></treeItem>
        </ul>
    </li>`
        }
    }
}
</script>

css:

body{
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}
 .header{
  height: 50px;
  border-bottom: 1px solid hsla(0,0%,100%,.15);
  background-color: #373d41;
}
.body{
  position: absolute;
  top:50px;
  left: 220px;
  right:0;
  bottom: 0;
  background-color: #fff;
}
.menu{
  position: absolute;
  top:50px;
  left: 0;
  bottom: 0;
  width: 220px;
}

.tree{
  position: relative;
  width: 100%;
  height: 100%;
  padding: 15px 10px;
  overflow-x: hidden;
  overflow-y: auto;
}
.tree__ul{
  margin: 0;
  padding: 0;
  list-style: none;
}
.tree__li{
  position: relative;
  padding-left: 12px;

}
.tree__item{
  margin: 0;
  padding: 2px 12px 2px 6px;
  line-height: 20px;
  font-size: 12px;
  color: #222;
  background-color: transparent;
  cursor: pointer;
}
.tree__item:hover{
  background-color: #F0F8FD;
}
.tree__item_selected{
  background-color: #F0F8FD;
}
.tree__item--arrow{
  display: inline-block;
  position: absolute;
  top: 0;
  left: 2px;
  width: 12px;
  height: 24px;
  overflow: hidden;
  cursor: pointer;
}
.tree__item--arrow i{
  display: inline-block;
  position: absolute;
  top: 7px;
  left: 4px;
  border-width: 5px;
  border-style: dashed dashed dashed solid;
  border-color: transparent transparent transparent #222;
  font-size: 0;
  line-height: 0;
  cursor: pointer;
}
.tree__item--arrow:hover i{
  border-color: transparent transparent transparent #999;
}

.tree__item--arrow-down{

}
.tree__item--arrow-down i{
  display: inline-block;
  position: absolute;
  top: 10px;
  left: 0px;
  border-width: 5px;
  border-style: solid dashed dashed dashed;
  border-color: #222 transparent transparent transparent;
  font-size: 0;
  line-height: 0;
}
.tree__item--arrow-down:hover i{
  border-color: #999 transparent transparent transparent;
}
.tree__item--arrow + .tree__ul{
  display: none;
}
.tree__item--arrow-down + .tree__ul{
  display: block;
}
/*树样式*/
.tree-icon,.tree-checkbox {
  display: inline-block;
  width: 16px;
  height: 16px;
  vertical-align: top;
  overflow: hidden;
}
.tree-icon{
  background-image: url('../img/treeicon.png');

}
.tree-folder {
  background-position: 52px -3px;
}
.tree-folder_open {
  background-position: 52px 47px;
}
.tree-file {
  background-position: 52px 21px;
}
.tree-checkbox {
  background-image: url('../img/treeicon.png');
}
.tree-checkbox_checked {
  background-position: 25px 47px;
}
.tree-checkbox_unchecked {
  background-position: 25px -2px;
}
.tree-checkbox_unallchecked {
  background-position: 25px 22px;
}

.tree-text {
  display: inline-block;
  vertical-align: top;
}

.tree-delete {
  padding-left: 10px;
  color: red;
}

样式图标:


treeicon.png

调用:

<tree
                    ref="testTree"
                    :treeDatas="treeDatas"
                    @nodeClick="nodeClick"
                    @expandNode="expandClick"
                    :options="options"></tree>

数据格式:

var options = {
        expandNum: 4,
        isAllexpand: true,
        hascheckbox: true,
        hasicon: true,
        folderClass: "",
        folderopenClass: "",
        fileClass: "",
        candelete: true,
        checkedCallback: false,
        removeCallback: false,
        checkedSelectChild: true,
    };
    var treeDatas = [{
        "id": 4,
        "value": "省XX",
        "name": "省XX",
        "checked": false,
        "children": [{"id": 1026, "value": "chenXX", "name": "陈XX","checked": false}, {
            "id": 2427,
            "value": "zhuangXX",
            "name": "庄XX", "checked": false
        }, {"id": 100730, "value": "dwliuXX", "name": "刘XX", "checked": false}, {
            "id": 100906,
            "value": "linXX",
            "name": "林XX", "checked": true
        }, {"id": 212, "value": "zhangXX", "name": "张XX", "checked": false}, {
            "id": 214,
            "value": "wangXX",
            "name": "王XX", "checked": false
        }, {"id": 236, "value": "huangXX", "name": "黄XX", "checked": false}, {
            "id": 265,
            "value": "caihouen",
            "name": "蔡XX", "checked": false,
            "children":[{"id": 1026, "value": "chenXX", "name": "陈XX", "checked": true}, {
                "id": 212,
                "value": "zhangXX",
                "name": "张XX","checked": true
            }, {"id": 531, "value": "liuXX", "name": "刘XX", "checked": true}, {
                "id": 4477,
                "value": "liXX",
                "name": "李XX",
                "checked": true
            }]
        }]
    }, {
        "id": 55,
        "value": "省XX分析组",
        "name": "省XX分析组",
        "checked": true,
        "children": [
            {"id": 1026, "value": "chenXX", "name": "陈XX", "checked": true}, {
                "id": 212,
                "value": "zhangXX",
                "name": "张XX","checked": true
            }, {"id": 531, "value": "liuXX", "name": "刘XX", "checked": true}, {
                "id": 4477,
                "value": "liXX",
                "name": "李XX",
                "checked": true
            }]
    }];

获取树数据的方法:

           //数结构的回调
            nodeClick(value) {
                console.log(value);
            },
            expandClick(value){
                console.log(value);
            },
            getSelectNode(){
                let nodes=this.$refs.testTree.getSelectNode();
                console.log(nodes);
             }

以上,一个功能还不够完善的树组件(一直没时间完善功能)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容