【问题标题】:jstree - node selection based on current navigated urljstree - 基于当前导航 url 的节点选择
【发布时间】:2011-12-06 17:31:02
【问题描述】:

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

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

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

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

谢谢,

【问题讨论】:

  • jsTree UI 插件是否没有提供您正在寻找的功能?如果没有,我相信 jsTree 只是简单地添加/删除 css 样式来获得它的视觉效果。您应该能够找出类名是什么并根据需要手动应用它们。
  • 不仅仅是css风格,还有DOM操作,比如打开/关闭节点等
  • 自从我使用 jsTree 已经有一段时间了,但我认为即使是打开/关闭节点也是由 CSS 类处理的。只需选择您想要的并应用正确的类即可。
  • 你能澄清一下这个问题吗?动态或加载时打开、关闭和重新设置 jsTree 节点非常简单。我很乐意举一个详细的例子,但我不确定你在寻找什么——一种从树外的链接控制 jsTree 节点状态的方法?单击链接时,浏览器是导航到新页面还是异步加载内容?或许你可以提供一些代码来看看?
  • @Yair,我回答你的问题了吗?

标签: javascript jquery cookies jstree jquery-cookie


【解决方案1】:

我已经为此使用 jsTree、hashchange 事件和实际的可 SEO 的 URL 构建了一个完整的方法,因此这将非常简单地符合您的想法,您可以扔掉您的 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');
});

如果您还有其他问题,请随时问我。

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多