Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Tree (pioz): Tree doesnt render correctly with cookies
    primarykey
    data
    text
    <p>I am using the jQuery tree from here <a href="https://github.com/pioz/jquery-tree#readme" rel="nofollow">https://github.com/pioz/jquery-tree#readme</a></p> <p>The problem is rendering of the tree when I do a cookie save. When it comes time to re-render it, the code doesnt work correctly if I dont follow a specific way of putting the ordered lists. </p> <p>I guess best is to take a look at this demo code I put up.</p> <p><a href="http://jsbin.com/emaku4/23" rel="nofollow">http://jsbin.com/emaku4/23</a></p> <ol> <li>The entire tree is open</li> <li>Click on Level 3 arrow to expand (for both trees)</li> <li>Do a refresh</li> </ol> <p>Now, the first tree renders correctly again with cookies showing what was open previously but the 2nd tree doesnt. I think it's because on the 2nd one, I put the one without sublevels on top of the one with sublevels (the one called THIS IS THE DIFFERENCE).</p> <p>Is there a way to adjust the code in so it can render view correctly using cookies no matter what order I have in any of the sublevels?</p> <pre><code>// Require jQuery // Use jQuery.cookie if you want to restore the previous expansion of the tree jQuery.fn.tree = function(options) { // Setup default options /** Avaiable options are: * - open_char: defeault character to display on open node in tree * - close_char: defeault character to display on close node in tree * - default_expanded_paths_string: if no cookie found the tree will be expand with this paths string **/ if(options === undefined || options === null) options = {}; var open_char = options.open_char; var close_char = options.close_char; var default_expanded_paths_string = options.default_expanded_paths_string; if(open_char === undefined || open_char === null) open_char = '&amp;#9660;'; if(close_char === undefined || close_char === null) close_char = '&amp;#9658;'; if(default_expanded_paths_string === undefined || default_expanded_paths_string === null) default_expanded_paths_string = '0'; // Get the expanded paths from the current state of tree jQuery.fn.save_paths = function() { var paths = []; var path = []; var open_nodes = jQuery(this).find('li span.open'); var last_depth = null; for(var i = 0; i &lt; open_nodes.length; i++) { var depth = jQuery(open_nodes[i]).parents('ul').length-1; if((last_depth == null &amp;&amp; depth &gt; 0) || (depth &gt; last_depth &amp;&amp; depth-last_depth &gt; 1)) continue; var pos = jQuery(open_nodes[i]).parent().prevAll().length; if(last_depth == null) { path = [pos]; } else if(depth &lt; last_depth) { paths.push(path.join('/')); var diff = last_depth - depth; path.splice(path.length-diff-1, diff+1); path.push(pos); } else if(depth == last_depth) { paths.push(path.join('/')); path.splice(path.length-1, 1); path.push(pos); } else if(depth &gt; last_depth) { path.push(pos); } last_depth = depth; } paths.push(path.join('/')); try { jQuery.cookie(this.attr('class'), paths.join(',')); } catch(e) {} }; // This function expand the tree with 'path' jQuery.fn.restore_paths = function() { var paths_string = null; try { paths_string = jQuery.cookie(this.attr('class')); } catch(e) { paths_string = default_expanded_paths_string; } if(paths_string === null || paths_string === undefined) paths_string = default_expanded_paths_string; var paths = paths_string.split(','); for(var i = 0; i &lt; paths.length; i++) { var obj = jQuery(this); var path = paths[i].split('/'); for(var j = 0; j &lt; path.length; j++) { obj = jQuery(obj.children('li').children('ul')[path[j]]); obj.show(); obj.parent().children('span').attr('class', 'open'); obj.parent().children('span').html(open_char); } } }; for(var i = 0; i &lt; this.length; i++) { if(this[i].tagName === 'UL') { // Make a tree jQuery(this[i]).find('li').has('ul').prepend('&lt;span class="close" style="cursor:pointer;"&gt;' + close_char + '&lt;/span&gt;'); jQuery(this[i]).find('ul').hide(); // Restore cookie expand path jQuery(this[i]).restore_paths(); // Click event jQuery(this[i]).find('span').live('click', {tree : this[i]}, function(e) { if (jQuery(this).attr('class') == 'open') { jQuery(this).parent().children('ul').hide('slow'); jQuery(this).attr('class', 'close'); jQuery(this).html(close_char); } else if (jQuery(this).attr('class') == 'close') { jQuery(this).parent().children('ul').show('slow'); jQuery(this).attr('class', 'open'); jQuery(this).html(open_char); } jQuery(e.data.tree).save_paths(); }); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload