2

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?

1 Answer 1

1

Create the cookies when your page is unloaded:

jQuery(window).unload(RememberClickedState);

Restore them when your page is loaded:

_spBodyOnLoadFunctionNames.push("RestoreClickedState");
2
  • 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, 2012 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. Jun 29, 2012 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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