如何使引导树视图从父级折叠到子级?

Posted

技术标签:

【中文标题】如何使引导树视图从父级折叠到子级?【英文标题】:How do I make bootstrap tree view collapse from parent to child? 【发布时间】:2021-08-14 17:33:12 【问题描述】:

我在我的 Web 应用程序中使用了 Jon Miles 的引导树视图,但父级没有折叠以显示子级。我确定我正确导入了它。我想要做的是显示父级,它会折叠成不同的子级。但是,没有显示子项的折叠提示。

var listTree = [
    text: "Parent 1",
    nodes: [
        text: "Child 1",
        nodes: [
            text: "Grandchild 1"
          ,
          
            text: "Grandchild 2"
          
        ]
      ,
      
        text: "Child 2"
      
    ]
  ,
  
    text: "Parent 2"
  ,
  
    text: "Parent 3"
  ,
  
    text: "Parent 4"
  ,
  
    text: "Parent 5"
  ,
  
    text: "Parent 6"
  ,
  
    text: "Parent 7"
  ,
  
    text: "Parent 8"
  ,
  
    text: "Parent 9"
  ,
  
    text: "Parent 10"
  ,
  
    text: "Parent 11"
  ,
  
    text: "Parent 12"
  ,
  
    text: "Parent 13"
  ,
  
    text: "Parent 14"
  ,
  
    text: "Parent 15"
  ,
  
    text: "Parent 16"
  
];

$('#tree').treeview(
  data: listTree
);
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js " integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js" integrity="sha512-Hyk+1XSRfagqzuSHE8M856g295mX1i5rfSV5yRugcYFlvQiE3BKgg5oFRfX45s7I8qzMYFa8gbFy9xMFbX7Lqw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.css" integrity="sha512-A81ejcgve91dAWmCGseS60zjrAdohm7PTcAjjiDWtw3Tcj91PNMa1gJ/ImrhG+DbT5V+JQ5r26KT5+kgdVTb5w==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<script src="./app.js "></script>

<div class="col-2 card m-1" id="product-list">
  <input type="search" name="product-search-list" id="product-search-list" placeholder="البحث" style="width: 100%;">
  <div class="rating mt-2 text-center m-auto">
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
  </div>
  <div id="tree">

  </div>
</div>

【问题讨论】:

【参考方案1】:

您可以使用collapseIcon和expandIcon来设置节点的展开和折叠图标。

演示代码

var listTree = [
    text: "Parent 1",
    nodes: [
        text: "Child 1",
        nodes: [
            text: "Grandchild 1"
          ,
          
            text: "Grandchild 2"
          
        ]
      ,
      
        text: "Child 2"
      
    ]
  ,
  
    text: "Parent 2"
  ,
  
    text: "Parent 3"
  ,
  
    text: "Parent 4"
  
];

$('#tree').treeview(
  data: listTree,
  collapseIcon: 'fas fa-minus', //added icon for expand and collapse
  expandIcon: 'fas fa-plus'
);
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js " integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js" integrity="sha512-Hyk+1XSRfagqzuSHE8M856g295mX1i5rfSV5yRugcYFlvQiE3BKgg5oFRfX45s7I8qzMYFa8gbFy9xMFbX7Lqw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.css" integrity="sha512-A81ejcgve91dAWmCGseS60zjrAdohm7PTcAjjiDWtw3Tcj91PNMa1gJ/ImrhG+DbT5V+JQ5r26KT5+kgdVTb5w==" crossorigin="anonymous"
  referrerpolicy="no-referrer" />

<script src="./app.js "></script>

<div class="col-2 card m-1" id="product-list">
  <input type="search" name="product-search-list" id="product-search-list" placeholder="البحث" style="width: 100%;">
  <div class="rating mt-2 text-center m-auto">
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
    <i class="fas fa-star"></i>
  </div>
  <div id="tree">

  </div>
</div>

【讨论】:

以上是关于如何使引导树视图从父级折叠到子级?的主要内容,如果未能解决你的问题,请参考以下文章

将动态插槽从父级传递到子级到孙子级

如何正确使用 Angular 中的 Observable 将数组数据从父级发送到子级?

将父 POM 折叠到子中

VueJS 2.0 从父级到子级的通信

数据未在 VueJS 中使用提供/注入从父级注入到子级

在 AngularJS 组件中从父级到子级通信事件