knockout.js HTML树视图展开行

klsxnrf1  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(150)

我正在使用knockout和dxTreeView,我有树视图和HTML表,我想当单击表中的一行时展开树视图,并基于表和树视图中的相同ID聚焦。如果这不可能,我想通过html和knockout来制作树视图,而不使用devextreme

here is my code

https://codepen.io/anon/pen/qLVPbb

mwg9r5ms

mwg9r5ms1#

我不知道为什么要用表来列出俱乐部。你可以简单地有一个无序的俱乐部列表和一个所有细节的嵌套列表。

<ul id="myTreeView">
    <li>
        <span>Clubs</span>
            <ul>
                <li>
                    <span class="expandable">CO Harrach</span>
                        <ul class="non-expandable">
                            <li>Address: Asia</li>
                            <li>Open: 24x7</li>
                        </ul>
                </li>
            </ul>
            <ul>
                <li>
                    <span class="expandable">Zamalek</span>
                        <ul class="non-expandable">
                            <li>Address: Australia</li>
                            <li>Open: 5 days</li>
                        </ul>
                </li>
            </ul>
  </li>
</ul>

<style>
 #myTreeView {
  list-style-type: none;
}

# myTreeView ul {

list-style-type:none;
}

# myTreeView {

  margin: 0;
  padding: 0;
}
.expandable {
  cursor: pointer;
  -webkit-user-select: none; /* Safari 3.1+ */
  -moz-user-select: none; /* Firefox 2+ */
  -ms-user-select: none; /* IE 10+ */
  user-select: none;
}
.expandable::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}
.expanded::before {
  -ms-transform: rotate(90deg); /* IE 9 */
  -webkit-transform: rotate(90deg); /* Safari */'
  transform: rotate(90deg);  
}
.non-expandable {
  display: none;
}
.active {
  display: block;
}
</style>

<script>
var toggler = document.getElementsByClassName("expandable");
var i;
for (i = 0; i < toggler.length; i++) {
  toggler[i].addEventListener("click", function() {
    this.parentElement.querySelector(".non-expandable").classList.toggle("active");
    this.classList.toggle("expanded");
  });
}
</script>

我刚刚创建了一个示例树视图here
此外,您可以阅读我的完整文章here

相关问题