php/mysql 実装を使用する場合、jsTree でルート ノードを表示する必要があります。関連するすべてのコードを、出力のスクリーン ショットと共に以下に示します。
現在のテーブル構造: (申し訳ありませんが、まだ画像を埋め込むことはできません...) 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 のロードを要求しています。ただし、その直下のノードのみが表示されます。ツリーのさらに下にある別のノードを最初にロードすると (事実上、そのノードがルート ノードになります)、その下のすべてのサブ ノードがロードされますが、最初のノードは表示されません。
誰かが私を正しい方向に向けることができれば、本当に感謝しています.
ありがとうアラン