[Html-笔记 001]Html+JS+CSS最简单的自定义Tree树

这是一个关于使用jQuery创建树形标签的示例,包括CSS样式和JavaScript交互。数据以JSON格式组织,用于构建多级树结构,支持折叠和展开节点。组件通过点击事件处理节点的显示和隐藏,并提供了回调函数进行节点状态检查。

样式CSS:


.tree {
    list-style: none;
    /* border-right: solid 1px #e5e5e5; */
    /* border-left: solid 1px #e5e5e5; */
    padding: 0px;
    color: #4d6878;
}

.tree a {
    text-decoration: none;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    display: block;
}

.tree a:hover {
    background: #f0f7fc;
}

.tree li {
    /*border-left: 1px dashed #a4c2f3;*/
    /*border-bottom: 1px dashed #a4c2f3;*/
    /* box-shadow: 0px 0px 9px #c9c9c9; */
    margin: 2px 0;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    position: relative;
}

.tree-dir {
    display: block;
    padding: 1px 4px;
    font-size: 15px;
}

.tree-dir > a.tree-dir-title > i {
    padding: 0 10px 0 0;
    color: #de892f;
}


.tree-dir > a.tree-dir-title > i {
    padding: 0 10px 0 0;
    color: #de892f;
}

.tree-dir > ul {
    display: block;
    padding-left: 12px;
    border-left: 1px dashed #67b2dd;
}


.tree-file {
    display: block;
    padding: 1px 4px;
    font-size: 15px;
}

/*.tree .tree-dir:before, */
.tree .tree-file:before {
    display: inline-block;
    content: "";
    position: absolute;
    top: 10px;
    left: -8px;
    width: 10px;
    height: 0px;
    border-top: 1px dotted #67b2dd;
    z-index: 1;
}

JS控制:


///////////////////////////////////////////////
/// 树型标签
/// V 1.0
/// creat by revivec
/// 运行库 juqery
/// //////////////////////////////////////////
//  components_map 原生集合
var components_map = {};

;(function ($) {

    var defaults = {
        id: "",
        data: [],
        fold: true,
        multiple: false,
        showIndex: 0,
        check: function () {
        },
        done: function () {
        }
    };
    //$this == defaults 传递的对象
    $.fn.componentTree = function ($this) {
        var $compTree = $("#" + $this.id);
        $compTree.empty();
        var $rootUl = $('<ul class="tree" id="tree_ul_id"></ul>');
        $compTree.append($rootUl);
        $.each($this.data, function (index, value) {
            if (value.level == 0) {
                var $pli;
                if (value.type == 1) {
                    $pli = $('<li class="tree-dir" id="tree_' + value.id + '"><a class="tree-dir-title active"><i class="glyphicon glyphicon-folder-close "></i>' + value.name + '</a><ul></ul></li>');
                } else if (value.type == 2) {
                    $pli = $('<li class="tree-file" id="tree_' + value.id + '"><a><i class="glyphicon glyphicon-file"></i>' + value.name + '</a></li>');
                }

                $rootUl.append($pli);
            }
        });
        $.each($this.data, function (index, value) {
            console.log(value.id);
            console.log(value.pId);
            console.log(value.name);
            console.log(value.open);
            if (value.level > 0) {
                var pliId = "#tree_" + value.pId + ">ul";
                var $pli = $(pliId);
                if ($pli != null && $pli != undefined) {
                    var $li;
                    if (value.type == 1) {
                        $li = $('<li class="tree-dir" id="tree_' + value.id + '" ><a class="tree-dir-title active"><i class="glyphicon glyphicon-folder-close "></i>' + value.name + '</a><ul></ul></li>');
                    } else if (value.type == 2) {
                        $li = $('<li class="tree-file" id="tree_' + value.id + '"><a><i class="glyphicon glyphicon-file"></i>' + value.name + '</a></li>');
                    }
                    if (!value.open) {
                        $pli.css('display', 'none');
                    }
                    $pli.append($li);
                }
            }
        });

        $(".tree-dir > .tree-dir-title").nextAll().hide();
        $(".tree-dir > .tree-dir-title").click(function () {
            $(this).nextAll().toggle();
        });
    }
})(jQuery);

调用Html:

   <div class="panel-body" id="tree_div_id">
        </div>

数据格式:


[
    {"id":1,"name":"长沙","pId":"","open":false,"level":0},
    {"id":2,"name":"天心区","pId":1,"open":false,"level":1},
    {"id":3,"name":"芙蓉区","pId":1,"open":false,"level":1},
    {"id":4,"name":"天心区2","pId":2,"open":false,"level":2},
]

id:自身
pId:父级
name:名称
open:是否打开
level:树层级(0 顶级是必须的,其他可以根据自己需求)

初始化加载:

 $("#tree_div_id").componentTree({
                                id: "tree_div_id",
                                data: data.data,
                                fold: false,
                                multiple: false,
                                check: function (val) {
                                    console.log('chekc:' + val);
                                    console.log($(this).tagTreeValues());
                                },
                                done: function () {
                                    console.log('tagTree is ok!');
                                }
                            });

范例图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值