3

I have a list that is grouped by date: Normally the list is collapsed, when I open one date, items under it are shown, like: [+] 2010-01-01 [+] 2010-01-02 [-] 2010-01-03 item1 item2 ...

I have also a $(document).ready( function() { ... }); event to handle. If a page is loaded event is fired before the items under the [-] 2010-01-03 are loaded - because there are being loaded for a while.

I wish make sure that function is executed after everything is loaded.

Is it possible to force it?

5 Answers 5

2

SharePoint internally uses the function ExpGroupRenderData to expand or collapse a group of a grouped view. You need to overwrite this function to fire your custom JavaScript code.

UPDATE: Here's the original script code taken from init.js:

    function ExpGroupRenderData(htmlToRender, groupName, isLoaded)
    {ULSxSy:;
        var tbody=document.getElementById("tbod"+groupName+"_");
        var wrapDiv=document.createElement("DIV");
        var rg=groupName.split("-");
        wrapDiv.innerHTML="<TABLE><TBODY id=\"tbod"+            groupName+"_\" isLoaded=\""+isLoaded+           "\">"+htmlToRender+"</TBODY></TABLE>";
        tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);

// Add your custom script code here

    }

UPDATE 2: I'm sorry if my answer implied to modify internal SharePoint files. It wasn't meant like that. In fact I strongly recommend against modifying such files for reasons @Colin laid out.

As @webdes03 mentioned, you can "overwrite" a built-in function by simply putting the above code into your own file e.g. "myscript.js" and include it into your environment via a feature (my preferred method) or using SharePoint Designer (shudder) to add it to your masterpage.

That's how I usually handle such overwrites without requiring jQuery with a plugin which is not always available in projects I'm involved into.

1
  • +1 for posting the function so that the OP doesn't need to fish it out of Sharepoint.
    – bgmCoder
    Dec 17, 2012 at 16:30
1

We had a similar problem and also found the ExpGroupRenderData function to be the culprit.

But Editing OOTB Js files (shipped with SharePoint) is IMHO, not an option though.

  1. it is unsupported
  2. if MS decides to change that file than the next time you role out a CU, your changes will be gone.

We circumvented this by using the amazing jQuery.aop plugin.

Using this plugin, you can add before and after calls to existing javascript functions, WITHOUT having to rewrite those functions!

All in all, very clean, supported and no need to remind yourself (or the ITPro managing the farm) not to forget to backup the edited OOTB JS file before installing an upgrade :-D!

6
  • 2
    Just to be sure it's mentioned, you can override an OOTB function without modifying the default JS files. Simply include your own ExpGroupRenderData function after the default MS one is loaded, much like the way we override the OOTB CSS--the last one referenced will be used. Certainly the risk that MS changes what that function does in the future is still there, but your changes won't be overridden if you're using your own script file.
    – webdes03
    Feb 17, 2012 at 22:56
  • @Colin - Would you be willing to post an example of your usage of the jQuery.aop plugin?
    – bgmCoder
    Dec 17, 2012 at 16:37
  • 1
    An example would be for instance calling extra code AFTER a function has been called:jQuery.aop.after( {target: window, method: 'ExpGroupRenderData'}, function(result) { alert('Returned: ' + result); return result; } );
    – Colin
    Dec 17, 2012 at 20:07
  • Will look for the actual code tomorrow
    – Colin
    Dec 17, 2012 at 20:08
  • And I'll start a new question: sharepoint.stackexchange.com/questions/54186/…
    – bgmCoder
    Dec 17, 2012 at 20:31
0

Sounds like the expansion of the group is in itself a jscript piece of code that is happening on load.

It might be possible to locate that function and override it with your own code (a copy of that function loaded after the, most liekly, core.js, that contains some code to set a var of your in the window object)

You doc ready code could have fired off a timer event to poll that var and execute when that var is ready.

Or there is probably something in jQuery that you can "bind" to.

Or you function might be at a point in the lifecycle it could just execute the routine, depending on what you want to do of course.

It might be possible to bind to the expansion of the group, the existing function may already set a value/element that you can poll in your doc load.

whatever you do were are into hack territory.

0

In jQuery set a timer to check every second for an object in the part of the page that is still loading, once there run your code.

2
  • 2
    While this is possible (and I'm all for some jQuery action), it's a pretty inefficient way to do this... constantly scanning the DOM every second looking for an element.
    – webdes03
    Feb 17, 2012 at 22:57
  • Agreed... I wonder if there's an event you could attach to? like a div on show or something? I haven't tried. Feb 18, 2012 at 14:38
0

There is another option to do it. Open the list settings and check the "Expanded" radiobutton under "By default, show groupings:". You can then manipulate these items in document ready function.

Your Answer

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

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