2012-11-12 67 views
0

當使用php/mysql實現時,我有要求在jsTree中顯示根節點。下面的所有相關代碼以及輸出的屏幕快照。jsTree使用PHP從MySQL填充時顯示根節點

當前的表結構:(抱歉不能嵌入圖像尚未...) http://i.imgur.com/RpDZw.jpg

電流輸出: http://i.imgur.com/5Gh6M.jpg

正如你可以看到名爲ROOT實際的根節點沒有在顯示樹。顯示的上面的代碼中的各種片段:

的javascript:

.jstree({ 
    // List of active plugins 
    "plugins" : [ 
     "themes","json_data","ui","crrm","dnd","search","types","hotkeys","contextmenu" 
    ], 

    // I usually configure the plugin that handles the data first 
    // This example uses JSON as it is most common 
    "json_data" : { 
     // This tree is ajax enabled - as this is most common, and maybe a bit more complex 
     // All the options are almost the same as jQuery's AJAX (read the docs) 
     "ajax" : { 
      // the URL to fetch the data 
      "url" : "_demo/server2.php", 
      // the `data` function is executed in the instance's scope 
      // the parameter is the node being loaded 
      // (may be -1, 0, or undefined when loading the root nodes) 
      "data" : function (n) { 
       // the result is fed to the AJAX request `data` option 
       return { 

        "operation" : "get_children", 
        "id" : n.attr ? n.attr("id").replace("node_","") : 1 

       }; 
      } 
     } 
    }, 

PHP:

function get_children($data) { 
    $tmp = $this->_get_children((int)$data["id"]); 
    if((int)$data["id"] === 1 && count($tmp) === 0) { 
     ## - No data returned 
    } 
    $result = array(); 
    if((int)$data["id"] === 0) return json_encode($result); 
    foreach($tmp as $k => $v) { 
     $result[] = array(
      "just_id" => $k, 
      "attr" => array("id" => "node_".$k, "rel" => $v[$this->fields["type"]]), 
      "data" => $v[$this->fields["title"]], 
      "owner" => $v[$this->fields["owner"]], 
      "state" => ((int)$v[$this->fields["right"]] - (int)$v[$this->fields["left"]] > 1) ? "closed" : "" 
     ); 
    } 
    return json_encode($result); 
} 

function _get_children($id, $recursive = false) { 
    $hbhbhbh = fSession::get('nodes_allowed[nodes]'); 
    if (in_array($id, $hbhbhbh)) { 

     $children = array(); 
     if($recursive) { 
      $node = $this->_get_node($id); 
      $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["left"]."` >= ".(int) $node[$this->fields["left"]]." AND `".$this->fields["right"]."` <= ".(int) $node[$this->fields["right"]]." ORDER BY `".$this->fields["left"]."` ASC"); 
     } 
     else { 
      $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["parent_id"]."` = ".(int) $id." AND FIND_IN_SET(".$this->fields["owner"].", '".implode(',', fSession::get('nodes_allowed[nodes_visible]'))."') ORDER BY `".$this->fields["position"]."` ASC"); 
     } 
     while($this->db->nextr()) $children[$this->db->f($this->fields["id"])] = $this->db->get_row("assoc"); 
     return $children; 
    } else { 
     $children = array(); 
     return $children; 
    } 
} 

你可以從JavaScript的看到,我最初請求節點1至負載。但它只顯示直接在它下面的節點。如果我最初在樹下進一步加載另一個節點(有效地使該節點成爲根節點),它會加載其下的所有子節點,但不會顯示初始節點。

如果有人能指引我正確的方向,我會非常感激。

感謝 艾倫

回答

0

爲別人有同樣的問題..

加載樹時,我最終只是增加根節點細節由PHP腳本返回的JSON字符串的前面。然後它只顯示根節點,並在用戶展開它時通過ajax加載葉子。比我想象的更簡單....