使用隐藏创建动态树并取消隐藏该子项

时间:2016-11-15 07:26:49

标签: php jquery css mysql

我使用 php mysql 中的代码在mysql上创建动态树

这棵树是动态的,每次都可以在mysql上改变

我的问题是:如何添加+ - 在此树视图中隐藏取消隐藏?? !!

当我点击孩子出现的父母的+时,当我点击时 - 孩子会消失,如果孩子有子孩子,那么同样的事情......和所有人

这是php代码

<?php
$parentID = !empty($_GET['parentID']) ? $_GET['parentID'] : 0;

if ($parentID) {
    $currentFolder=mysqli_query($conn,"SELECT * FROM `tbl_folders` WHERE `db_id` ='$parentID'")or die(mysqli_error($conn));
    while($currentFolderArray=mysqli_fetch_array($currentFolder)){
        $currentFolderName = $currentFolderArray['name'];
    }
}
$currentFolderName = empty($currentFolderName)? 'root': $currentFolderName;
function buildTreeHTML(&$list, $parentID = 0) {
    $isBranch = false;
    foreach ($list as $index => $element) {
        if($list[$index]['parent_id'] != $parentID) continue;
        if(!$isBranch) {
            echo '<ul class="main-ul">';
            $isBranch = true;
        }
       echo"<div class='edit'><a href='edit.php?id={$list[$index]['db_id']}'><img src='../img/edit.png'></a></div>";
        echo"<div class='delete'><a class='del'href='edit.php?delete={$list[$index]['db_id']}&parent_id={$list[$index]['parent_id']}' data-confirm='Are you sure to delete this item?'><img src='../img/delete.png'></a></div>";
echo "<li ><label><a href='dms.php?parentID={$list[$index]['db_id']}'>" . $list[$index]['name'] . '</a></label></li>';
        buildTreeHTML($list, $list[$index]['db_id']);
    }
    if ($isBranch) {
        echo '</ul>';
    }
}
?>

<ul>
    <li><a href="dms.php">root</a></li>
    <?php
    $treeObject = $conn->query("SELECT * FROM `tbl_folders`order by name asc")or die(mysqli_error($conn));
    $treeArray = [];
    while ($result = $treeObject->fetch_array()) {
    $treeArray[] = $result;
    }
    buildTreeHTML($treeArray);
    ?>
</ul>

浏览器的Html输出

<ul>
<li>
<a href="dms.php">root</a>
</li>
<ul class="main-ul">
<div class="edit">
<div class="delete">
<li>
<label>
<a href="dms.php?parentID=1">Accounting</a>
</label>
</li>
<div class="edit">
<div class="delete">
<li>
<label>
<a href="dms.php?parentID=7">GM</a>
</label>
</li>
<div class="edit">
<div class="delete">
<li>
<label>
<a href="dms.php?parentID=2">HR</a>
</label>
</li>
<div class="edit">
<div class="delete">
<li>
<label>
<a href="dms.php?parentID=3">IT</a>
</label>
</li>
<ul class="main-ul">
<div class="edit">
<div class="delete">
<li>
<label>
<a href="dms.php?parentID=4">Network</a>
</label>
</li>
</ul>
</ul>
</ul>

图片 enter image description here

0 个答案:

没有答案