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

    前端 未结 7 1215
    傲寒
    傲寒 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 = '<ul>';
      
      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 .= '</li>';            
          }
          elseif($node_depth > $current_depth){
              $result .= '<ul>';
              $current_depth = $current_depth + ($node_depth - $current_depth);
          }
          elseif($node_depth < $current_depth){
              $result .= str_repeat('</li></ul>',$current_depth - $node_depth).'</li>';
              $current_depth = $current_depth - ($current_depth - $node_depth);
          }
          $result .= '<li id="c'.$node_id.'"';
          $result .= $node_depth < 2 ?' class="open"':'';
          $result .= '><a href="#"><ins>&nbsp;</ins>'.$node_name.'</a>';
          ++$counter;
      }
       $result .= str_repeat('</li></ul>',$node_depth).'</li>';
      
      $result .= '</ul>';
      
      return $result;}
      

      Result HTML:

      <ul>
          <li id="c1" class="open"><a href="#"><ins>&nbsp;</ins>ELECTRONICS</a>
              <ul>
                  <li id="c2" class="open"><a href="#"><ins>&nbsp;</ins>TELEVISIONS</a>
                      <ul>
                          <li id="c3"><a href="#"><ins>&nbsp;</ins>TUBE</a></li>
                          <li id="c4"><a href="#"><ins>&nbsp;</ins>LCD</a></li>
                          <li id="c5"><a href="#"><ins>&nbsp;</ins>PLASMA</a>
                              <ul>
                                  <li id="c14"><a href="#"><ins>&nbsp;</ins>PLASMA1</a></li>
                                  <li id="c15"><a href="#"><ins>&nbsp;</ins>PLASMA2</a></li>
                              </ul>
                          </li>
                      </ul>
                  </li>
                  <li id="c6" class="open"><a href="#"><ins>&nbsp;</ins>PORTABLE ELECTRONICS</a>
                      <ul>
                          <li id="c7"><a href="#"><ins>&nbsp;</ins>MP3 PLAYERS</a>
                              <ul>
                                  <li id="c8"><a href="#"><ins>&nbsp;</ins>FLASH</a></li>
                              </ul>
                          </li>
                          <li id="c9"><a href="#"><ins>&nbsp;</ins>CD PLAYERS</a></li>
                          <li id="c10"><a href="#"><ins>&nbsp;</ins>2 WAY RADIOS</a></li>
                      </ul>
                  </li>
              </ul>
          </li>
      </ul>
      
      0 讨论(0)
    提交回复
    热议问题