我需要在使用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。但它只显示它下面的节点。如果我最初在树下进一步加载另一个节点(有效地使该节点成为根节点),它将加载其下的所有子节点,但不显示初始节点。
如果有人能指出我正确的方向,我会非常感激。
由于 艾伦
答案 0 :(得分:0)
对于其他有同样问题的人..
我最终只是在加载树时将根节点详细信息添加到PHP脚本返回的json字符串的前面。然后它只显示根节点,并在用户展开时通过ajax加载叶子。比我想象的简单......