0

How can I go about collapsing all groups in a sharepoint list upon page load?

enter image description here

I tried putting the following in a script editor web part (based on this page http://www.sharepointdiary.com/2014/07/expand-collapse-all-groups-in-sharepoint-2013-list-view-jquery.html#ixzz3Re7hfmvU):

<script type="text/javascript" src="http://code.jquery.com/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
   $(window).load(function () {
        $("img.ms-commentcollapse-icon").click();
   });
</script>

But the groups still stay open after a page reload.

lacrosse1991
  • 215
  • 4
  • 8
  • 2
    Go into the view settings and set the grouping to collapsed? – Eric Alexander Jan 13 '16 at 16:07
  • @EricAlexander it was set to that originally. If I were to clear my cache and load the page, all items will be collapsed, but if I expand an item, it will then stay expanded until I manually collapse it again. This persists through page reloads. – lacrosse1991 Jan 13 '16 at 16:12

1 Answers1

1

So I am not sure if it is because you are using a script editor or because you aren't grabbing the object correctly in jQuery.

I was able to get the script to work in a Content Editor Web Part and then accessed the collapse object via the class (putting a . at the beginning of the object name).

So to accomplish your needs add a CEWP to the list (remove the chrome). Add the following lines to the CEWP to call your scripts (put your script into a file and save to your site):

<script src="/<sitename>/SiteAssets/jquery-1.11.1.min.js"></script>
<script src="/<sitename>/SiteAssets/CollapseAll.js"></script> 

Write your code (in CollapseAll.js) as follows:

    $(window).load(function () {
        $(".ms-commentcollapse-icon").click();
   });

I tested this for both opening and collapsing the view and it worked.

Patrick
  • 3,203
  • 3
  • 24
  • 42
David Drever
  • 1,006
  • 8
  • 17
  • awesome, that did the trick! I actually got it to work in a script editor that was placed below the list view (chrome was left at default). It looks like removing the "img. from img.ms-commentcollapse-icon" did the trick – lacrosse1991 Jan 13 '16 at 17:07