0

使用 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 加载。但它只显示它正下方的节点。如果我最初在树的下方加载另一个节点(实际上使该节点成为根节点),它会加载它下面的所有子节点,但不显示初始节点。

如果有人能指出我正确的方向,我将不胜感激。

谢谢艾伦

4

1 回答 1

0

对于其他有同样问题的人..

我最终只是将根节点详细信息添加到加载树时 PHP 脚本返回的 json 字符串的前面。然后它只显示根节点,并在用户展开它时通过 ajax 加载叶子。比我想象的要简单......

于 2012-11-16T10:44:58.307 回答