Getting a modified preorder tree traversal model (nested set) into a

    前端 未结 7 1323
    傲寒
    傲寒 2020-11-27 10:56

    I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site.

    Here is my code:

    
    
            
    7条回答
    •  时光取名叫无心
      2020-11-27 11:35

      Better Render Tree Function that worked for me (php function to prepare html source for use in jsTree jQuery plugin) instead of the Henrik Opel's one:

      function MyRenderTree ( $tree = array(array('name'=>'','depth'=>'')) ){
      
      $current_depth = 0;
      $counter = 0;
      
      $result = '
        '; foreach($tree as $node){ $node_depth = $node['depth']; $node_name = $node['name']; $node_id = $node['category_id']; if($node_depth == $current_depth){ if($counter > 0) $result .= ''; } elseif($node_depth > $current_depth){ $result .= '
          '; $current_depth = $current_depth + ($node_depth - $current_depth); } elseif($node_depth < $current_depth){ $result .= str_repeat('
        ',$current_depth - $node_depth).''; $current_depth = $current_depth - ($current_depth - $node_depth); } $result .= '
      • '; ++$counter; } $result .= str_repeat('
      ',$node_depth).'
    • '; $result .= '
    '; return $result;}

    Result HTML:

    
    

    提交回复
    热议问题