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 am trying to load a script (that remembers the clicked nodes for a xsltlistview in a jquery cookie) on every postback.

The events when i postback should be: 1. save clicked nodes in cookie 2. postback 3. retrieve clicked nodes from cookie.

I call them like this:

_spBodyOnLoadFunctionNames.push("RememberClickedState");
_spBodyOnLoadFunctionNames.push("RestoreClickedState");

When I do this, the method RememberClickedState() doesnt work - my nodes appear all expanded, which means he did not store the correct values in the cookie.

I assume this happens because on postback, the controls are all rendered, the nodes are then again expanded, and only at the end of the process, the scripts are executed, at which point method RememberClickedState() does not see the correct nodes anymore.

This works when I test with a click event :

<a href="javascript:RememberClickedState()">Test here</a> 

however I need this for every control that posts back, not just one anchor tag.

If it helps, this is the .js script:

<script type="text/javascript">

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);
    //tvisible = $(this).is(':visible');
    //alert($(this).attr('style'));
    items += tid+':'+tvisible+','
})
$.cookie("itemListState", items);
alert(items);
}
function RestoreClickedState() {
string = $.cookie("itemListState")
var string = items; 
var cookies = string.split(',');
$.each(cookies, function(i, val){
    val = val.split(':');
    show = (val[1] == 'true' ? true:false);
    item = $('.ms-listviewtable').find(/*'#'+*/val[0]);
    show ? item.show() : item.hide();
})
}
_spBodyOnLoadFunctionNames.push("RememberClickedState");
_spBodyOnLoadFunctionNames.push("RestoreClickedState");


</script>

Any ideas?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Create the cookies when your page is unloaded:

jQuery(window).unload(RememberClickedState);

Restore them when your page is loaded:

_spBodyOnLoadFunctionNames.push("RestoreClickedState");
share|improve this answer
    
that was perfect, it now retrieves the correct values! but, it seems that on Restore(), the method is called too late, the controls are already rendered on the page if I use _spBodyOnLoadFunctionNames.push("RestoreClickedState"). Is there an event I can use, to hide the nodes, before the control is shown/rendered on page? thank you –  Amc_rtty Jun 28 '12 at 15:06
    
You could wrap your elements (nodes, or may the whole list webpart) into a wrapper. The wrapper could be hidden and when the values are restored, show them... Or you can show some loading image until your nodes state is restored so your users know what is happening. –  Anatoly Mironov Jun 29 '12 at 9:12
add 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.