在Angular 6中折叠/展开嵌套div

时间:2018-10-17 09:14:36

标签: html angular angular6 ngfor

我正在使用div开发类似于结构的日程表。我的设计看起来像这样。enter image description here

我想要的是在加载时,仅应显示1级(所有场所),并且在单击场所时应显示其直接子级,依此类推。有没有办法做到这一点。我的html是:

<div class="div-table">
    <div class="div-table-row">
        <div class="div-header-col" style="visibility: hidden;">A</div>
        <div *ngFor="let date of dates" class="div-date-col">{{date | date:'d E'}}</div>
    </div>


    <!-- level1 -->
    <div *ngFor="let venue of venues" class="level1" style="color: red">
        <div class="div-table-row-level1" >
            <div class="div-header-col">{{venue.name}}</div>
            <div *ngFor="let x of dates" class="div-event-level1-col"></div>
        </div>

        <!-- level2 -->
        <div *ngFor="let category of venue.categories" class="level2" style="color: blue">
            <div class="div-table-row-level2">
                <div class="div-header-col" style="padding-left: 10px">{{category.name}}</div>
                <div *ngFor="let x of dates" class="div-event-level2-col"></div>
            </div>

            <!-- level3 -->
            <div *ngFor="let asset of category.assets" class="level3" style="color: green">
                <div class="div-table-row-level3">
                    <div class="div-header-col" style="padding-left: 20px">{{asset.name}}</div>
                    <div *ngFor="let x of dates" class="div-event-level3-col assest-hover" "></div>
                </div>
            </div>

        </div>
    </div>
</div>

我表(div)的数据是:

[  
   {  
      "id":1,
      "name":"venue1",
      "categories":[  
         {  
            "id":1,
            "name":"cat1",
            "assets":[  
               {  
                  "id":1,
                  "name":"assest1"
               },
               {  
                  "id":2,
                  "name":"assest2"
               }
            ]
         },
         {  
            "id":2,
            "name":"cat2",
            "assets":[  
               {  
                  "id":3,
                  "name":"assest3"
               },
               {  
                  "id":4,
                  "name":"assest4"
               }
            ]
         }
      ]
   },
   {  
      "id":2,
      "name":"venue2",
      "categories":[  
         {  
            "id":3,
            "name":"cat3",
            "assets":[  
               {  
                  "id":5,
                  "name":"assest5"
               },
               {  
                  "id":6,
                  "name":"assest6"
               }
            ]
         },
         {  
            "id":4,
            "name":"cat4",
            "assets":[  
               {  
                  "id":7,
                  "name":"assest7"
               },
               {  
                  "id":8,
                  "name":"assest8"
               }
            ]
         }
      ]
   },{  
      "id":3,
      "name":"venue3",
      "categories":[  
         {  
            "id":5,
            "name":"cat5",
            "assets":[  
               {  
                  "id":9,
                  "name":"assest9"
               },
               {  
                  "id":10,
                  "name":"assest10"
               }
            ]
         },
         {  
            "id":6,
            "name":"cat6",
            "assets":[  
               {  
                  "id":11,
                  "name":"assest11"
               },
               {  
                  "id":12,
                  "name":"assest12"
               }
            ]
         }
      ]
   }
]

2 个答案:

答案 0 :(得分:4)

如果您将全部数据加载在一起,那么您就有个好消息,可以用非常简单的方法来实现它-

您可以玩Element Reference。无需从ts文件进行管理。

in html

   <div *ngFor="let venue of venues" class="level1" style="color: red" 
      (click)="ele.class = ele.class == 'showChildren' ? '' :  'showChildren'"
  [ngClass]="{ hideChildren : ele.class !== 'showChildren' }">

对所有parent div

重复相同的操作

在CSS

.hideChildren>div{
   display: none;
}

这是工作副本-https://stackblitz.com/edit/angular-n43ihd

如果以异步方式获取数据,还有其他一些处理方法。然后,这些逻辑将移至ts文件。

答案 1 :(得分:-2)

您可以尝试隐藏组件的负载,并将其设置为可见的onClick

$scope.$on('$viewContentLoaded', function() {
    // Hide all unwanted div's here
});