jstree - 基于当前导航URL的节点选择

时间:2011-12-06 17:31:03

标签: javascript jquery cookies jstree jquery-cookie

今天我正在使用jsTree的内置cookie来保存树中的用户导航。

在节点上单击树中,用户被重定向到我站点中的相应页面,并且由于jsTree cookie集成,所选节点被突出显示。

现在,我想基于网站之间的导航选择/突出显示树中的节点,即,站点中的链接也可能是树中的节点,例如,行的网格也出现在树上。

问题是如何进行“手动”节点选择/突出显示,我还认为我应该知道用户从哪里到达页面,从树中或从站点中的其他链接。

谢谢,

1 个答案:

答案 0 :(得分:3)

我已经使用jsTree,hashchange事件和实际真正的SEO能力网址构建了一个完整的方法,所以这非常适合你的想法,你可以折腾你的cookie,但不是一个坏的方式。 这也适用于书签和从URL到达,因为它查看节点然后匹配链接以选择节点。对于AJAX来说这是最好的,尽管应该尽可能。

我正在为您发表评论,以便您了解它。这里的工作示例www.kitgui.com/docs显示了所有内容。

$(function () {
        // used to remove double reload since hash and click are intertwined
    var cancelHashChange = false,
        // method sets the selector based off of URL input
    setSelector = function (path) {
        var startIndex = path.indexOf('/docs');
        if (startIndex > -1) {
            path = path.substr(startIndex);
        }
        path = path.replace('/docs', '').replace('/', '');
        if ($.trim(path) === '') { path = 'overview'; }
        return '.' + path;
    };
        // sets theme without the folders, plain jane
    $('.doc-nav').jstree({
        "themes": {
            "theme": "classic",
            "dots": true,
            "icons": false
        }
    }).bind("loaded.jstree", function (event, data) {
        // when loaded sets initial state based off of priority hash first OR url
        if (window.location.hash) { // if hash defined then set tree state
            $.jstree._focused().select_node(selector);
            $(setSelector(window.location.hash.substr(1)) + ' a:first').trigger('click');
        } else { // otherwise base state off of URL
            $.jstree._focused().select_node(setSelector(window.location.pathname));
        }
    });
        // all links within the content area if referring to tree will affect tree
        // and jump to content instead of refreshing page
    $('.doc-nav a').live('click', function (ev) {
        var $ph = $('<div />'), href = $(this).attr('href');
        ev.preventDefault();
        cancelHashChange = true;
            // sets state of hash
        window.location = '#' + $(this).attr('href');
        $('.doc-content').fadeOut('fast');
            // jQuery magic load method gets remote content (John Resig is the man!!!)
        $ph.load($(this).attr('href') + ' .doc-content', function () {
            cancelHashChange = false;
            $('.doc-content').fadeOut('fast', function () {
                $('.doc-content').html($ph.find('.doc-content').html()).fadeIn('fast');
            });
        });
    });
        // if doc content is clicked and has referring tree content, 
        // affect state of tree and change tree content instead of doing link
    $('.doc-content a').live('click', function (ev) {
        ev.preventDefault();
        if ($(this).attr('href').indexOf('docs/') > -1) {
            $.jstree._focused().select_node(setSelector($(this).attr('href')));
            $(setSelector($(this).attr('href')) + ' a:first').trigger('click', false);
        }
    });
        // if back/forward are used, maintain state of tree as if it was being clicked
        // refers to previously defined click event to avoid double-duty
        // but requires ensuring no double loading
    window.onhashchange = function () {
        if (cancelHashChange) { cancelHashChange = false; return; }
        $.jstree._focused().select_node(setSelector(window.location.hash.substr(1)));
        $(setSelector(window.location.hash.substr(1)) + ' a:first').trigger('click', false);
    };
    $('#top-doc-link').closest('li').addClass('active');
});

如果您有更多问题,请随时问我。