W3文档有一个前缀为的嵌套列表示例DEPRECATED EXAMPLE:,但它们从未使用非弃用示例对其进行更正,也未解释该示例的确切错误.
那么这些方法中哪一种是编写HTML列表的正确方法?
选项1:嵌套<ul>是父级的子级<ul>
<ul>
<li>List item one</li>
<li>List item two with subitems:</li>
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
<li>Final list item</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
选项2:嵌套<ul>是<li>它所属的子级
<ul>
<li>List item one</li>
<li>List item two with subitems:
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Final list item</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 其中一个答案给出了正确的代码来显示完整的树.我需要的是始终显示第一级(深度= 0)和兄弟姐妹+孩子的活动列表项.目标是当用户选择列表项时扩展树的可见部分,该列表项是更多列表项的父项.
所以,如果我有这个清单:
1. item
2. item
2.1. item
2.2. item
2.2.1. item
2.2.2. item
2.2.3. item
2.3. item
2.4. item
2.4.1. item
2.4.2. item
3. item
4. item
4.1. item
4.2. item
4.2.1. item
4.2.2. item
5. item
Run Code Online (Sandbox Code Playgroud)
如果当前列表项为"2",则列表应如下所示:
1. item
2. item // this needs class .selected
2.1. item
2.2. item
2.3. item
2.4. item
3. item
4. item
5. item
Run Code Online (Sandbox Code Playgroud)
如果当前列表项是"2.2.",列表应如下所示:
1. item
2. item // this needs class .selected
2.1. item
2.2. item // …Run Code Online (Sandbox Code Playgroud) 我正在使用awesome_nested_set我的Rails项目中的插件.我有两个看起来像这样的模型(简化):
class Customer < ActiveRecord::Base
has_many :categories
end
class Category < ActiveRecord::Base
belongs_to :customer
# Columns in the categories table: lft, rgt and parent_id
acts_as_nested_set :scope => :customer_id
validates_presence_of :name
# Further validations...
end
Run Code Online (Sandbox Code Playgroud)
数据库中的树按预期构造.的所有值parent_id,lft以及rgt是否正确.树有多个根节点(当然允许进入awesome_nested_set).
现在,我想在一个正确排序的树中呈现给定客户的所有类别,例如结构:例如嵌套<ul>标签.这不会太难,但我需要它才能有效(sql查询越少越好).
更新:想出可以在没有进一步SQL查询的情况下计算树中任何给定节点的子节点数:number_of_children = (node.rgt - node.lft - 1)/2.这并不能解决问题,但可能会有所帮助.
我有这张桌子:
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`category_id` int(11) default NULL,
`root_id` int(11) default NULL,
`name` varchar(100) collate utf8_unicode_ci NOT NULL,
`lft` int(11) NOT NULL,
`rht` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`),
KEY `lft` (`lft`,`rht`),
KEY `root_id` (`root_id`)
)
Run Code Online (Sandbox Code Playgroud)
基于这个问题: 将修改后的预订树遍历模型(嵌套集)转换为<ul>
不同的是,我在一张桌子上有很多树.每行都有一个代表其父级及其顶级父级的外键:category_id和root_id.我还有基于这个例子的lft和rht字段:http://articles.sitepoint.com/article/hierarchical-data-database/2
基于这些行:
INSERT INTO `categories` VALUES(1, NULL, NULL, 'Fruits', 1, 14);
INSERT INTO `categories` VALUES(2, 1, 1, 'Apple', 2, 3);
INSERT INTO `categories` VALUES(3, 1, 1, 'Orange', 4, 9);
INSERT INTO …Run Code Online (Sandbox Code Playgroud) 我有一些分层数据,我需要在一系列嵌套的UL中显示.对于每个项目,我有一个名称,一个ID和一个深度值.通常我会按深度对这些项进行分组,但实际上我需要使用我的数据创建树结构,如下所示:

这是我的问题:有没有一种很好的方法来生成有效的标记(如果我能用正确的标签打印出来,我会很喜欢它,但这很难)我的数据将被嵌套在UL中?我已经有一个有点工作的解决方案,但我得到一个单一的迷路标签.这是我的代码:
<?php
include("includes/classes/Database.class.php");
$db = new Database();
$query = "SELECT COUNT(parent.Name) - 2 as level, node.Name AS Name, node.ID
FROM Region AS node, Region AS parent
WHERE node.LeftVal BETWEEN parent.LeftVal AND parent.RightVal and node.Name <> 'Earth'
GROUP BY node.ID
ORDER BY node.LeftVal";
$results = $db->executeQuery($query);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$last_level = 0;
?>
<ul id="regionTree">
<?php
while ($row = mysql_fetch_assoc($results)) {
$link = '<li>'.PHP_EOL.'<a href="addChild.php?parentid='.$row["ID"].'">'.$row["Name"]."</a>".PHP_EOL;
$diff = $last_level - $row["level"];
if($diff …Run Code Online (Sandbox Code Playgroud) 这个函数给了我一个无限循环
function getCats($parent,$level){
// retrieve all children of $parent
$result = "";
$query = "SELECT title,parent_id from t_cats where parent_id = '$parent'";
if($rs = C_DB::fetchRecordset($query)){
while($row = C_DB::fetchRow($rs)){
$result .= str_repeat($parent,$level).$row['title']."\n";
getCats($row['parent_id'],$level+1);
}
}
return $result;
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据库表
CREATE TABLE `db`.`t_cats` (
`ID` int(10) unsigned NOT NULL auto_increment,
`datasource_id` int(10) unsigned default '0',
`version` char(10) character set latin1 default 'edit',
`status` char(10) character set latin1 default 'new',
`modified_date` datetime default NULL,
`modified_by` int(10) unsigned default '0',
`title` char(255) character set …Run Code Online (Sandbox Code Playgroud) html ×4
php ×4
nested-sets ×2
sql ×2
activerecord ×1
arrays ×1
html-lists ×1
loops ×1
markup ×1
mysql ×1
nested ×1
nested-lists ×1
recursion ×1
ruby ×1
traversal ×1