2014-11-24 104 views
1

我正在尋找顯示從嵌套集數據的HTML樹。從嵌套集生成樹

由於顯示的樹並不總是顯示所有分支,所以葉節點不能通過減去rgt值來識別。

   1 Drinks 27 
     /  |  \ 
2 Coffee 3  4 Tea 20  21 Milk 26 
      /  \ 
     5 Black 8  9 Green 19 
        /  \ 
      10 China 14  15 Africa 18 

我一直在尋找,以適應下面的代碼: How to render all records from a nested set into a real html tree

解決方法:

高興地收到代碼改進建議:)

def tree_from_set(set, start_level) 
    buf = "<ul>" 
    parent = [] 
    prev = set[start_level] 
    set[start_level+1..-1].each do |node| 

    if node.lft.between?(prev.lft, prev.rgt) 
     # Previous was the parent 
     buf << open_parent_tag(prev) 
     parent.push(prev) 
    else 
     if node.lft.between?(parent.last.lft, parent.last.rgt) 
     #Previous was a child 
     buf << leaf_tag(prev) 
     else 
     buf << leaf_tag(prev) 
     begin 
      buf << "</ul></li>" 
      parent.pop 
     end until parent.empty? or node.lft.between?(parent.last.lft, parent.last.rgt) 
     end 
    end 

    prev = node 
    end 

    buf << leaf_tag(prev) 
    begin 
    buf << "</ul></li>" 
    parent.pop 
    end until parent.empty? 

    buf << "</ul>" 
    buf.html_safe 
end 


def open_parent_tag(node) 
    %{ <li> 
     #{link_to(node.name, node)} 
     <ul> 
    } 
end 


def leaf_tag(node) 
    content_tag(:li, link_to(node.name, node)) 
end 
+0

嘗試使用這個:http://stackoverflow.com/questions/1372366/how-to-render-all-records-from-a-nested-set-into-a-真實的HTML樹 – 2015-04-15 14:26:06

回答

1

我用這個功能,但是在php,不是紅寶石:

<?php 
//nested sets data ordered by left 
$data = array(
array("left" => 1, "right" => 10, "name" => "P0"), 
array("left" => 2, "right" => 7, "name" => "P1"), 
array("left" => 3, "right" => 4, "name" => "P11"), 
array("left" => 5, "right" => 6, "name" => "P12"), 
array("left" => 8, "right" => 9, "name" => "P2") 
); 

//Converter function gets nested sets array and returns nested php array 
function nest($arrData){ 
$stack = array(); 
$arraySet = array(); 
foreach($arrData as $intKey=>$arrValues) { 
    $stackSize = count($stack); 
    while($stackSize > 0 && $stack[$stackSize-1]['right'] < $arrValues['left']) { 
    array_pop($stack); 
    $stackSize--; 
    } 

    $link =& $arraySet; 
    for($i=0;$i<$stackSize;$i++) { 
    $link =& $link[$stack[$i]['id']]["children"]; //navigate to the proper children array 
    } 

    $tmp = array_push($link, array ('item'=>$arrValues,'children'=>array())); 
    array_push($stack, array('id' => $tmp-1, 'right' => $arrValues['right'])); 
} 

return $arraySet; 
} 


//Print result 
printArray(nest($data)); 

function printArray($array){ 
echo "<ul>"; 
foreach ($array as $row){ 
    $children = $row['children']; 
    echo "<li>"; 
    echo $row['item']['name']; 
    if (!empty($children)) printArray($children); 
    echo "</li>"; 
} 
echo "</ul>"; 
} 
?> 
0

管理將它轉換爲C++爲那些正在尋找它像我一樣的人。

readDataBaseData(QVector<NodeData> &data, DBNode *node) 
{ 
    if(data.size() < 1){ 
     return; 
    } 

    QVector<QPair<NodeData, int>> stack; 
    DBNode* arrayRes = node; 

    foreach (NodeData curArrDataItem, data) { 
     int stackSize = stack.size(); 

     while(stackSize > 0 && 
       stack.at(stackSize - 1).first.right < curArrDataItem.left){ 

      stack.pop_back(); 
      stackSize--; 
     } 

     DBNode* link = arrayRes; 
     for(int i = 0; i < stackSize; i++){ 
      link = link->childAt(stack.at(i).second); 
     } 

     link = new DBNode(curArrDataItem, link); 
     stack.push_back(QPair<NodeData, int>(curArrDataItem, link->getParent()->childCount() - 1)); 
    } 
} 

enter image description here