带进度条的Tree

问题来由

工作上业务有个需求,树状图展示数据。这个好办,很多组件,插件都有数状图。比如Ant组件。但是想在tree的文字后面再做扩展却是很难。可以说基本办不到。
在这里插入图片描述
类似于这种树状图。ant基本已经写死了。那么办法只能自己整了。
自己手写的效果:
在这里插入图片描述

上代码

这个就不多说了,用li画就可以了,由于我这里规定好了只有三级目录,所以直接写死了。如果不确定有几级的话再想其他办法,用递归也可以的。

<ul class="treeMain">
                <li *ngFor="let item of treeData;let i=index"   >
                  <span class='icon-open' id="li-{{i}}" (click)="showChild($event)">{{item.name}}</span>
                  <ul *ngIf="item.value && item.value.length!==0 " class="changeNone">
                    <li *ngFor="let elm of item.value ;let j=index" >
                      <span class='icon-open' id="li-{{i}}-{{j}}" (click)="showChild($event)">{{elm.type}}</span> <span>({{elm.count}})</span>
                      <ul *ngIf="elm.data && elm.data.length!==0"  class="changeNone">
                        <li *ngFor="let elemt of elm.data;let k=index" >
                          <span class='icon-open' id="li-{{i}}-{{j}}-{{k}}" (click)="showChild($event)">{{elemt.key}}</span> 
                          <div class="jd-box" *ngIf="elemt.value&&elemt.value.indexOf('%')>-1">
                          <div  class="jd">
                            <div  id="fill" [ngStyle]="{'width': elemt.value}">&emsp;</div>
                              <div class="item" *ngFor="let item of jdLine;let i=index" [ngStyle]="{'left':4*(i+1)+'px'}"></div>
                            </div>
                            <span style="font-size: 12px;line-height: 34px;">{{elemt.value}}</span>
                          </div>  
                          <span *ngIf="elemt.value&&elemt.value.indexOf('%')===-1">({{elemt.value}})</span>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>

.treeMain ul {
    margin-left: 7px;
    border-left: 1px solid #ccc;
    
}
.treeMain span{
    width: 56px;
    height: 14px;
    font-size: 14px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 30px;
    letter-spacing: 0px;
    color: #686868;
}
.treeMain ul li{
    margin-left: 8px;
}
.icon-open:before {
    margin-right: 11px;
}
.icon-close:before {
    margin-right: 11px;
}


.jd-box{
    display: inline;
}
.jd{
    width: 56px;
    height: 6px;
    margin: 0 8px;
    display: inline-block;
    background-color: rgb(199,199,199);
    /* display: flex; */
    border: 1px solid rgb(180,196,200);
    position: relative;
}

.jd  .item{
    z-index: 2;
    position: absolute;
    display: inline-block;
    width: 1px;
    height: 100%;
    background-color:#fff;
    /* margin-left: 3px; */
}
 #fill{
     position: absolute;
    /* width: 55%; */
        height: 100%;
    background-color: rgb(0,204,240);
}
 showChild(e) {
        console.log(e);
        let dom = document.getElementById(e.target.id);
        let className = e.target.className;
        if (className === 'icon-open') {
            dom.classList.replace("icon-open", "icon-close");
        } else {
            dom.classList.replace("icon-close", "icon-open");
        }
        console.log(dom.parentNode.children)
        let ulDom;
        for (let i = 0; i < dom.parentNode.children.length; i++) {
            if (dom.parentNode.children[i].tagName === "UL") {
                ulDom = dom.parentNode.children[i];
                break;
            }
        }
        console.log(ulDom, className);
        if (ulDom) {
            ulDom.removeAttribute('style');
            if (className === 'icon-open') {
                ulDom.setAttribute('style', 'display:block');
            } else {
                ulDom.setAttribute('style', 'display:none');
            }
        }
    }

单要进度条的话就拉取class为jd的部分就好了。
进度条的长度可以直接和百分比挂钩,用起来相对比较轻松。
不同的框架除了语法不同基本其他都一样的。实在不行用原生的也可以很快弄好。

最后再提供另一个进度条比较常规了,思路很简单。

在这里插入图片描述

 <div class="back-div">
     <div class="fill-div" [ngStyle]="{'width':bindWidth}"></div>
</div>
.back-div{
    margin: 0 5px;
    display: inline-block;
    width:  48%;
    height: 5px;
    border-radius: 5px;
    background-color: rgb(240,242,244);
}
.fill-div{
    height: 5px;
    border-radius: 5px;
}

绑定width就好了,width为百分比就OK。

简单好用哈…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要使用HTML中的`<ul>`和`<li>`标签来创建树形结构,然后使用CSS来实现进度条效果。以下是一个简单的示例代码: ```html <ul class="tree"> <li> <span>节点1</span> <ul> <li> <span>节点1.1</span> <div class="progress-bar"></div> </li> <li> <span>节点1.2</span> <div class="progress-bar"></div> </li> </ul> </li> <li> <span>节点2</span> <ul> <li> <span>节点2.1</span> <div class="progress-bar"></div> </li> <li> <span>节点2.2</span> <div class="progress-bar"></div> </li> </ul> </li> </ul> ``` 上面的代码创建了一个简单的树形结构,并为每个节点添加了一个进度条。接下来,我们需要使用CSS来定义进度条的样式: ```css .tree li { list-style-type: none; position: relative; } .tree li:before { content: ""; display: block; position: absolute; top: 0; bottom: 0; left: -20px; border-left: 1px solid black; } .tree li:last-child:before { height: 50%; } .tree li:first-child:before { top: 50%; height: 50%; } .tree li:only-child:before { display: none; } .tree li > span { display: inline-block; padding-left: 5px; } .tree li > ul { margin: 0; padding: 0; padding-left: 20px; } .progress-bar { height: 5px; background-color: gray; position: relative; margin-top: 5px; } .progress-bar:before { content: ""; display: block; height: 100%; width: 50%; background-color: green; position: absolute; top: 0; left: 0; } ``` 上面的CSS代码会给每个节点添加一个竖线,并使用`before`伪元素来实现。进度条则是使用一个`div`元素,并设置`position: relative`来让其内部的`before`伪元素定位。进度条的样式可以按照自己的需求来修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值