简体   繁体   English

HTML + CSS的TreeTable

[英]TreeTable by HTML+CSS

Stumbled upon a problem with TreeTable... Help to understand, how to make the second level and further down will not appear when the upper td input is checked. 偶然发现了TreeTable的问题...帮助理解,当选中较高的td输入时,将不会出现如何使第二层及更下层的内容出现。 Unfortunately I can use only HTML+CSS. 不幸的是,我只能使用HTML + CSS。 how it looks like now on jsfiddle jsfiddle现在看起来如何

<table class="table-tree" cellspacing="0">
        <tr>
            <td>
                <label for="folder2">Folder 1</label>
                <input type="checkbox" id="folder2" />
                <table class="table-wrapper">
                    <tr>
                        <td class="file"><a href="">Subfile 1</a></td>
                        <td class="file"><a href="">Subfile 2</a></td>
                        <td>
                            <label for="folder3">Subfolder 1</label>
                            <input type="checkbox" id="folder3" />
                            <table class="table-wrapper">
                                <tr>
                                    <td class="file"><a href="">Subsubfile 1</a></td>
                                    <td class="file"><a href="">Subsubfile 2</a></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

It would be great if layout was like this 如果布局是这样,那就太好了

在此处输入图片说明

Can anyone help me, please? 有人可以帮我吗?

You need to replace table by ul (or ol ) and td by li (remove tr ) in HTML and CSS and it will look like the disered layout on your image. 您需要在HTMLCSS中用ul (或ol )替换table ,并用li (删除tr )替换td ,这看起来像是图像上的扭曲布局。

An other reason to do this is that the content you are displaying is not tabular data, it is a list of files so you shouldn't use tables but list type layout. 这样做的另一个原因是,您显示的内容不是表格数据,而是文件列表,因此您不应使用表,而应使用列表类型布局。

FIDDLE 小提琴

HTML: HTML:

<ul class="table-tree" cellspacing="0">
    <li>
        <label for="folder2">Folder 1</label>
        <input type="checkbox" id="folder2" />
        <ul class="table-wrapper">
            <li class="file"><a href="">Subfile 1</a>

            </li>
            <li class="file"><a href="">Subfile 2</a>

            </li>
            <li>
                <label for="folder3">Subfolder 1</label>
                <input type="checkbox" id="folder3" />
                <ul class="table-wrapper">
                    <li class="file"><a href="">Subsubfile 1</a>

                    </li>
                    <li class="file"><a href="">Subsubfile 2</a>

                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

CSS: CSS:

*, html {
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
body, form, ul, li, p, h1, h2, h3, h4, h5 {
    margin: 0;
    padding: 0;
}
body {
    background-color: #fafafa;
    color: rgba(0, 0, 0, 0.7);
    margin: 0;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
/**
 * Tree Table CSS
**/
 .table-tree {
    display: block;
    padding: 30px 0 0 30px;
}
li {
    display: block;
    position: relative;
}
li label {
    background: url(http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png) 15px 1px no-repeat;
    cursor: pointer;
    display: block;
    padding-left: 37px;
    width: 100%;
}
li input {
    position: absolute;
    left: 0;
    margin-left: 0;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
    height: 1em;
    width: 1em;
    top: 0;
}
li input + ul {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small-expand.png) -3px -1px no-repeat;
    margin: -0.938em 0 0 0;
    height: 1em;
    width: 1em;
}
li input:checked + ul {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small.png) 41px 4px no-repeat;
    display: block;
    margin: -1.25em 0 0 -44px;
    padding: 1.25em 0 0 80px;
    height: auto;
    width: 100%;
}
li.file {
    margin-left: -1px !important;
}
li input + ul > li {
    display: none;
    margin-left: -14px !important;
    padding-left: 1px;
}
li input:checked + ul > li {
    display: block;
    margin: 0 0 0.125em;
}
li input:checked + ul > li:last-child {
    margin: 0 0 0.063em;
    /* 1px */
}

I hope my example helped you demo 我希望我的例子可以帮助您演示

<ol class="tree">
        <li>
            <label for="folder1">Folder 1</label> <input type="checkbox" checked disabled id="folder1" /> 
            <ol>
                <li class="file"><a href="document.html.pdf">File 1</a></li>
                <li>
                    <label for="subfolder1">Subfolder 1</label> <input type="checkbox" id="subfolder1" /> 
                    <ol>
                        <li class="file"><a href="">Filey 1</a></li>
                        <li>
                            <label for="subsubfolder1">Subfolder 1</label> <input type="checkbox" id="subsubfolder1" /> 
                            <ol>
                                <li class="file"><a href="">File 1</a></li>
                                <li>
                                    <label for="subsubfolder2">Subfolder 1</label> <input type="checkbox" id="subsubfolder2" /> 
                                    <ol>
                                        <li class="file"><a href="">Subfile 1</a></li>
                                        <li class="file"><a href="">Subfile 2</a></li>
                                        <li class="file"><a href="">Subfile 3</a></li>
                                        <li class="file"><a href="">Subfile 4</a></li>
                                        <li class="file"><a href="">Subfile 5</a></li>
                                        <li class="file"><a href="">Subfile 6</a></li>
                                    </ol>
                                </li>
                            </ol>
                        </li>
                        <li class="file"><a href="">File 3</a></li>
                        <li class="file"><a href="">File 4</a></li>
                        <li class="file"><a href="">File 5</a></li>
                        <li class="file"><a href="">File 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder2">Folder 2</label> <input type="checkbox" id="folder2" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder2">Subfolder 1</label> <input type="checkbox" id="subfolder2" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder3">Folder 3</label> <input type="checkbox" id="folder3" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder3">Subfolder 1</label> <input type="checkbox" id="subfolder3" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder4">Folder 4</label> <input type="checkbox" id="folder4" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder4">Subfolder 1</label> <input type="checkbox" id="subfolder4" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>
            <label for="folder5">Folder 5</label> <input type="checkbox" id="folder5" /> 
            <ol>
                <li class="file"><a href="">File 1</a></li>
                <li>
                    <label for="subfolder5">Subfolder 1</label> <input type="checkbox" id="subfolder5" /> 
                    <ol>
                        <li class="file"><a href="">Subfile 1</a></li>
                        <li class="file"><a href="">Subfile 2</a></li>
                        <li class="file"><a href="">Subfile 3</a></li>
                        <li class="file"><a href="">Subfile 4</a></li>
                        <li class="file"><a href="">Subfile 5</a></li>
                        <li class="file"><a href="">Subfile 6</a></li>
                    </ol>
                </li>
            </ol>
        </li>
    </ol>

Your CSS has two errors. 您的CSS有两个错误。

First, you should select unchecked checkbox like this: 首先,您应该选中未选中的复选框,如下所示:

input:not(:checked)

and not relying on a fallback in rules application. 并且不依赖于规则应用中的后备广告。

Second and more important, the rule td input:checked + table > tbody td is wrong. 其次,更重要的是,规则td input:checked + table > tbody td是错误的。

Infact, if you have your outer checkbox checked but the nested one unchecked, that rule will still match nested table td because the first part td input:checked + table > tbody matches the outer table body and then the second part td looks for td that have that body as ancestor . 实际上,如果您选中了外部复选框,但未选中嵌套复选框,则该规则仍将与嵌套表td匹配,因为第一部分td input:checked + table > tbody与外部表主体匹配,然后第二部分td查找td以那个身体为祖先

You should check for direct children instead. 您应该改为检查直系子女。

Summing up, you can change your css to something like this: 总结一下,您可以将CSS更改为以下内容:

*, html {
    font-family: Verdana, Arial, Helvetica, sans-serif;
}

body, form, ul, li, p, h1, h2, h3, h4, h5 {
    margin: 0;
    padding: 0;
}

body {
    background-color: #fafafa;
    color: rgba(0, 0, 0, 0.7);
    margin: 0;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

/**
 * Tree Table CSS
**/

.table-tree {
    display: block;
    padding: 30px 0 0 30px;
}

td {
    display: block;
    position: relative;
}

td label {
    background: url(http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png) 15px 1px no-repeat;
    cursor: pointer;
    display: block;
    padding-left: 37px;
    width: 100%;
}

td input {
    position: absolute;
    left: 0;
    margin-left: 0;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
    height: 1em;
    width: 1em;
    top: 0;
}
// CHANGED HERE
td input:not(:checked) + table {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small-expand.png) -3px -1px no-repeat;
    margin: -0.938em 0 0 0;
    height: 1em;
    width: 1em;
}

td input:checked + table {
    background: url(http://www.webmasters.by/images/articles/css-tree/toggle-small.png) 41px 4px no-repeat;
    display: block;
    margin: -1.25em 0 0 -44px;
    padding: 1.25em 0 0 80px;
    height: auto;
    width: 100%;
}

td.file {
    margin-left: -1px !important;
}
// CHANGED HERE
td input:not(:checked) + table > tbody > tr > td {
    display: none;
    margin-left: -14px !important;
    padding-left: 1px;
}
// CHANGED HERE
td input:checked + table > tbody > tr > td {
    display: block;
    margin: 0 0 0.125em;
}
// CHANGED HERE
td input:checked + table > tbody > tr > td:last-child {
    margin: 0 0 0.063em; /* 1px */
}

Always use the most specific CSS rule you can because when HTML gets huge you can have some surprises. 始终使用最具体的CSS规则,因为当HTML变得庞大时,您可能会感到意外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM