我尝试使用来自 Mysql 的动态数据创建一个树表。
它可以工作(最终经过长时间的试验),当我单击一个父级时,它会从 Mysql 读取数据并显示该特定父级的子级。
但无论我点击哪个父母,孩子总是显示在最后一行之后。我需要孩子们出现在他们的父母和其他下一个父母之后的行中。
有2个文件:
主文件
<html> <head> <style type="text/css"> .toggle { color: #000000; } .tog { height: 16px; width: 16px; display: inline-block; } .parent .last { background-image: url(02_files/01_images/paper_small.png); } .parent .tog { background-image: url(02_files/01_images/folder_green.png); } .expand .tog { background-image: url(02_files/01_images/folder_green_open.png); } </style> <script type="text/javascript" src="jquery-2.1.4.js"></script> <script type="text/javascript"> $('document').ready(function(){ $('.toggle').click(function() { $('#plant').html('<img src="02_files/01_images/loading.gif">'); var area = $(this).parent().find('.toggle').text();<? $areaqtyquery = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT(Business_Area)) AS areaqty FROM b01_functional_location")); $areaqty = $areaqtyquery['areaqty'];?> $.ajax({ type: 'POST', url: 'Action.php', data: 'area='+area, success: function(data){ $('#plant').html(data); } }); }); }); </script> </head> <body> <form> <table> <tr><th>No</th> <th>Area & Plant</th> <th>Location</th> <th>Main Group</th> <th>Sub Group</th> <th>Function Group</th> <th>Functional Location</th> <th>Equipment</th> </tr><? mysql_connect("localhost","root",""); mysql_select_db("sfer"); $area = 1; $sql = mysql_query("SELECT DISTINCT(Business_Area) FROM b01_functional_location"); while($data=mysql_fetch_array($sql)){?> <tr id="area" class='parent'> <td align='center'> <?=$area?></td> <td align='left'> <a class="tog"></a> <a href="javascript:void(0)" class="toggle"><?=$data['Business_Area']?></a></td></tr><? $area++; }?> <tr id="plant" class='parent'></tr> </table> </form> </body>
动作.php
<?php mysql_connect("localhost","root",""); mysql_select_db("sfer"); if (!empty($_POST['area'])){ $area = 1; $sql = mysql_query("select DISTINCT(Plant), Plant_Description from b01_functional_location where Business_Area='$_POST[area]'"); while($data=mysql_fetch_array($sql)){?> <tr id='plant' class='parent' data-level="1"> <td><?=$area?></td> <td><?=$data['Plant'].' : '.$data['Plant_Description']?></td> </tr><? } $area++;}?>
需要您帮助如何让子级在父级下方切换,就像普通的树表一样。非常感谢您的帮助。