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.