Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a XsltListViewWebpart that shows a tree structure with nodes. I made a jquery script that sets a cookie which persists the state of the nodes (expanded/collapsed) between postbacks.

I added the script with:

_spBodyOnLoadFunctionNames.push("RestoreClickedState");

...so that the script is the last to run on the page.

The problem is that I get inconsistent results:

between postback and my script there is something else that collapses the nodes: for example, if I expand a node and leave 9 closed, after postback my 9 nodes are closed but my expanded is closed as well and I cant seem to figure out why.

For other nodes, if I expand a node and leave the others collapsed, sometimes after postback another node is incorrectly expanded instead of the correct one.

Here is my script:

<script>
$(window).unload(function()
{  
RememberClickedState();
});


var items = '';
function RememberClickedState() {
$('.ms-listviewtable tbody[id^="tbod"]').each(function(){
    tid = $(this).attr('id');
    tvisible = ($(this).attr('style') == undefined ||
       $(this).attr('style').indexOf('display: none;') == -1);
    items += tid+':'+tvisible+','
})
$.cookie("itemListState", items);
}
function RestoreClickedState() {
//alert("beginne");
string = $.cookie("itemListState");
//alert(string);
var cookies = string.split(',');
//alert(cookies.length);
$.each(cookies, function(i, val){
    val = val.split(':');
    show = (val[1] == 'true' ? true:false);
    var test = val[0].substring(val[0].length -1, 1);
    var thisID = test.replace("bod", "");  
    var thisImgID = "img_" + thisID;
    if(!show && thisID)
    {
        //alert(thisID + " - " + thisImgID + " hiding");
        ExpCollGroup(thisID, thisImgID);// ,null, show); 
    }       
}); 

//$.cookie("itemListState", null);

} 

_spBodyOnLoadFunctionNames.push("RestoreClickedState");

</script>

When I use alert to check the contents of the cookies after each postback, it correctly shows all cookies, so the cookie part of the script is working, but the expanding/collapsing according to the cookie is not.

Has anyone any idea, because I have been dealing with this for about 4 days now.

Thank you very much in advance.

Best regards, Andrei

EDIT just realised that everything works but for the SECOND postback. so, the first postback after the cookie is created the nodes are read correctly from the cookie but incorrectly expanded/collapsed, but on the postback after that, the nodes are correctly expanded/collapsed. which leads me to believe that somehow there is a problem with _spBodyOnLoadFunctionNames.push.

it cannot be a problem with the other event $(window).unload because the cookies are read correctly, so the first event works as expected.


edit 30.07.2012:

I will try to be more clear about the treeview. By "treeview" I mean, the component itself is a WebPartPages:XsltListViewWebPart, and yes this is a standard view, with grouping options. Please let me know if further information is required.

share|improve this question
    
How did you create the tree view in the first place? Are you actually talking about a standard view with grouping option(s)? –  Christophe Jul 25 '12 at 4:50
    
I have edited the question to try to answer your comment. Thank you –  Amc_rtty Jul 30 '12 at 10:33
add comment

1 Answer 1

up vote 2 down vote accepted
+100

Assuming that you used the default grouping options available in SharePoint lists: SharePoint already has this memorization behavior built in, and you are duplicating it.

Here is what could possibly happen:

  • when the page loads, SharePoint expands the node because this is the stored state
  • your script does the same interpretation and collapses it back

Here is what you could try:

  • remove your script and see if the default behavior is just what you need
  • instead of a script that switches states (ExpCollGroup), write a script that explicitly sets the state to expanded or collapsed. This way there is no risk of double switching.
share|improve this answer
1  
+1, absolutely correct. The SharePoint javascript function ExpDataViewGroupOnPageLoad executes on page load (coming from _spBodyOnLoadFunctionNames), and after some cascade calls it passes the execution to ExpGroupParseCookieForWebPart function from the core.debug.js file, which finally calls ExpCollGroup for each saved node. SharePoint uses cookies for saving state of expanded nodes. Please, feel free to include these additional details in your answer. –  Andrey Markeev Jul 25 '12 at 16:40
    
thank you for your answer. i made the script in the first place because sharepoint simply was NOT remembering the state of the nodes between postbacks - it was simply expanding all nodes at each postback, regardless of what was expanded or collapsed. upon research on web i found out this can only be achieved with jQuery. "write a script that explicitly sets the state to expanded or collapsed" - that is exactly what i wanted to do in the first place - please advise how can it be achieved without using ExpCollGroup. I tried setting style visible:none but had no effect. Only ExpCollGroup worked. –  Amc_rtty Jul 30 '12 at 0:34
    
@AndreiCristof strange, Andrey confirmed my reply (cf. above comment). I don't have a script to show, but here is how I would do it: find the +/- buttons; check the state (image src contains plus.gif or minus.gif); if not the correct state, trigger a click on the button (jQuery click). –  Christophe Jul 30 '12 at 1:08
    
your last comment was the solution. all this time I should have used jQuery .click() instead of the ExpCollGroup method. thank you SO MUCH for the suggestion. –  Amc_rtty Jul 30 '12 at 11:58
    
seems this works perfectly fine in Firefox, Chrome, practically any browser except for IE9. Back to square one... –  Amc_rtty Aug 7 '12 at 11:18
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.