1

今日は、ツリー内のユーザーナビゲーションを維持するために、jsTreeの組み込みCookieを使用しています。

ツリーのノードクリックで、ユーザーは私のサイトの対応するページにリダイレクトされ、jsTree Cookieの統合により、クリックされたノードが選択/強調表示されます。

ここで、Webサイト間のナビゲーションにも基づいて、ツリー内のノードを選択/強調表示したいと思います。つまり、サイト内のリンクもツリー内のノードである可能性があります。たとえば、行のグリッドも表示されます。木の中。

問題は、この「手動で」ノードの選択/強調表示をどのように行うことができるかということです。また、ユーザーがページにアクセスした場所、ツリー、またはサイト内の他のリンクから知る必要があると思います。

ありがとう、

4

1 に答える 1

3

私はすでに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-12-17T04:32:06.337 に答える