鉴于我有这个结果集结构(多余的字段已被剥离)
Id | ParentId | Name | Depth
----------------------------
Run Code Online (Sandbox Code Playgroud)
是有可能有树的顺序返回即记录Parent,然后Children,如果Child是Parent,那么他们的Children,如果没有的话Sibling,等?例如,
Id | ParentId | Name | Depth
----------------------------
1 NULL Major 1
2 1 Minor 2
3 1 Minor 2
4 3 Build 3
5 3 Build 3
6 1 Minor 2
/* etc, etc */
Run Code Online (Sandbox Code Playgroud)
我能想到这样做的唯一方法就是遵循这篇文章 -
并包含每个记录的字段[LeftExtent]和[RightExtent]字段.现在文章中的SQL在Ids唯一时工作正常,但在这个特定的树结构中,具有相同记录的记录Id可以出现在树内的不同位置(ParentId显然字段不同).我认为问题出在本文的SQL中 -
INSERT INTO @tmpStack
(
EmployeeID,
LeftExtent
) …Run Code Online (Sandbox Code Playgroud) 我正在构建一个任务列表应用程序,并希望任务能够具有子任务.
我还希望任务能够同时存在于树中的多个位置,例如,如果我有2个任务:
如果我计划用与围栏相同的材料建造狗窝,这两项任务都会有一个"购买栅栏"的子任务.
我有2个型号:
这意味着树(允许我有子任务)不会自己存储任务,只是对任务对象的引用.
以下是使用rails控制台的示例:
t1 = Task.create :name => "Build dog kennel"
n1 = Node.create :task => t1
t2 = Task.create :name => "Put up new fence"
n2 = Node.create :task => t2
t3 = Task.create :name => "Buy fence palings"
n11 = Node.create :task => t3, :parent => n1
n21 = Node.create :task => t3, :parent => n2
t4 = Task.create :name => "Construct the fence"
n22 = Node.create :task => …Run Code Online (Sandbox Code Playgroud) 给定一个数组:
$arrData = array(
0 => array (
'uid' => 1,
'name' => 'label',
'open' => 0,
'close' => 9
),
1 => array (
'uid' => 2,
'name' => 'label',
'open' => 1,
'close' => 2
),
2 => array (
'uid' => 3,
'name' => 'label',
'open' => 3,
'close' => 8
),
3 => array (
'uid' => 4,
'name' => 'label',
'open' => 4,
'close' => 5
),
4 => array (
'uid' => 5,
'name' …Run Code Online (Sandbox Code Playgroud) 如果我有这样的树:
Page1
---Page1.1
---Page1.2
---Page1.3
Run Code Online (Sandbox Code Playgroud)
使用awesome_nested_set,如何在Page1节点内向上或向下移动Page1.x?
谢谢.
我有一个Elements数组,每个元素都有一个属性:image.
我想要一系列:图像,所以最快和最便宜的方式来实现这一目标.它只是迭代数组并将每个元素推送到一个新数组,如下所示:
images = []
elements.each {|element| images << element.image}
Run Code Online (Sandbox Code Playgroud) 我在理解在嵌套集模型上使用什么索引时遇到了一些麻烦.查询是:
SELECT `node`.`id`,(COUNT(parent.id) - 1) AS `depth`,`name` FROM `categories` AS `parent`
INNER JOIN `categories` AS `node` ON (`node`.`lft` BETWEEN parent.lft AND parent.rgt)
INNER JOIN `filebank_categories` ON (`node`.`id` = `filebank_categories`.`category_id` AND `filebank_categories`.`filebank_id` = 136)
INNER JOIN `categories_names` ON (`categories_names`.`category_id` = `node`.`id` AND `categories_names`.`language_id` = 1)
WHERE `node`.`system_id` = parent.system_id
GROUP BY node.id
ORDER BY `node`.`lft` ASC
Run Code Online (Sandbox Code Playgroud)
这个查询需要大约350毫秒,大约有5000行categories.EXPLAIN给出了这个:
1 SIMPLE filebank_categories ref fk_filebank_categories_categories1,filebank_id filebank_id 5 const 474 Using where; Using temporary; Using filesort 1 SIMPLE node eq_ref PRIMARY,lft,category,cat,lft,rgt,system,id,lft,system PRIMARY 4 …
在嵌套集模型中,我们有LEFT和Right列
第一次表是空的,我需要插入到RIGHT列,如果我不知道我有多少个孩子
左一 - 永远
对 ? - 这里有什么价值?
如何让它充满活力?不是静止的.
ps:使用php
我想对多个树中的对象进行分类以反映其特征并构建导航.
所以,考虑到以下树木:
Category1
-Category-1-1
-Category-1-2
Category2
-Category-2-1
-Category-2-2
--Category-2-2-1
Run Code Online (Sandbox Code Playgroud)
对象可以例如属于类别1-2和类别-2-2-1.
目标是能够从数据库中获取所有对象
一个更实际的例子:
类别可能具有"工具>园艺工具>切割器"的层次结构.
第二类:'硬物>金属物>小金属物'
对象'Pruners'将被归类为属于'Cutters'以及'Small metal objects'.
我希望能够
我正在尝试为节点中的MongoDB中存储的页面生成URL.
使用以下函数,我想遍历一个javascript对象,并显示每个元素的路径.
我几乎在那里,但我被卡住了 - 使用Async甚至可能有更好的方法来做到这一点(我必须承认,让我有点困惑).
功能:( 演示)
function printTree(people, slug) {
for (var p = 0; p < people.length; p++) {
var root = people[p];
slug = slug + root.name + "/";
console.log(slug);
if (root.children.length > 0) {
var childrenCount = root.children.length;
for (var c = 0; c < childrenCount; c++) {
if (root.children[c].children.length > 0) {
printTree(root.children[c].children, slug + root.children[c].name + "/");
}
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
输出:
/michael/
/michael/angela/oscar
/michael/meredith/creed
/michael/meredith/creed/kelly
Run Code Online (Sandbox Code Playgroud)
预期产出:
/michael/
/michael/angela/
/michael/angela/oscar/
/michael/meredith/
/michael/meredith/creed/ …Run Code Online (Sandbox Code Playgroud) 有以下数据.
[
{"no":1, "name":"ELECTRONICS", "depth":0},
{"no":2, "name":"TELEVISIONS", "depth":1},
{"no":3, "name":"TUBE", "depth":2},
{"no":4, "name":"LCD", "depth":2},
{"no":5, "name":"PLASMA", "depth":2},
{"no":6, "name":"PORTABLE ELECTRONICS", "depth":1},
{"no":7, "name":"MP3 PLAYERS", "depth":2},
{"no":8, "name":"FLASH", "depth":3},
{"no":9, "name":"CD PLAYERS", "depth":2},
{"no":10, "name":"2 WAY RADIOS", "depth":2}
]
Run Code Online (Sandbox Code Playgroud)
我想获得如下数据.
[
{
"no":1,
"name":"ELECTRONICS",
"depth":0,
"child_nodes":[
{
"no":2,
"name":"TELEVISIONS",
"depth":1
"child_nodes":[
{
"no":3,
"name":"TUBE",
"depth":2
},
...
]
},
{
"no":6,
"name":"PORTABLE ELECTRONICS",
"depth":1
"child_nodes":[ ... ]
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我正在递归地尝试它,但它并不好.由于我使用的是babel,因此对javascript的新功能没有很大的限制.如果你有个好主意,请告诉我.谢谢!