This is a short one on counting groups in a grouped list view like this (group count in the list view toolbar):
Note: This counts only visible groups. If you set the “Number of groups to display per page” in the list view settings you will get max that number of groups when counting. An alternate approach would be to query the list for the total number of unique items based on the items the view is grouped by.
Add a CEWP below the list view with this code (alter the location of jQuery as needed):
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript"> // Grouping on level 1 var CountGrouping1 = $(".ms-listviewtable td.ms-gb").length; var NameGrouping1 = $(".ms-listviewtable td.ms-gb:first a:eq(1)").text(); // Grouping on level 2 var CountGrouping2 = $(".ms-listviewtable td.ms-gb2").length; var NameGrouping2 = $(".ms-listviewtable td.ms-gb2:first a:eq(1)").text(); var str = NameGrouping1 + "'s: " + CountGrouping1 + " | " + NameGrouping2 + "'s: " + CountGrouping2; $("td.ms-toolbar[width='99%']").append("<div class='ms-listheaderlabel' style='text-align:center;margin-top:-15px'>" + str + "</div>"); </script>
The jQuery-library is found here. The sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
Ask if something is unclear.
Regards
Alexander
Another great, really usable solution!
Thanks Alexander-
Charlie Epes
hey – will this count get displayed on all the views configured once enabled. i want this for only one of the views.
Hi,
It is per view, but if your new view is created from “Start from an existing view”, and the existing view has the script active – it is also active in the new view.
Alexander
I would like to get the count of 3 List located on the same page each list Count should be unique to that list. Would someone please help? Thanks
This code is very useful. How can I modify it to also include a count of total number of unique tasks for each responsible person? Thanks in advance.
Hi,
This code was just a simple “count groups” script. If you want more logic, i would recommend writing a script that uses a webservice call to query the list and build the result using HTML. Doing this in a list view would be a bit to flimsy.
Alexander
Anyone else running into the issue of the Tasks not counting the first displayed sub group? Just like the above example I have 2 for Responsible but only showing 6 for Tasks. It is not counting the tasks under the first group (Alexander).
I made a change to the code and replaces gb2 with vb2. Then I divided the CountGroup2 value by 2 and it works now. I have no idea why but I have tested it out by adding/deleting items and it seems to hold up.
Hello,
Thank you for this valuable post. Is it possible to read the subgroup counts?
I have not looked at this one for about 5 years, but I’m not sure the subgroups are actually rendered before you expand the parent group. This means you cannot count the contents of the subgroups when the page loads.
Alexander
I am fairly new to SharePoint, trying to use the code above. However, it is not working for me. I pasted the code in CEWP under the list with a grouped view and modified the location of the jquery file. I also downloaded the latest 1.11.3 file and updated the script reference in the code. I don’t see any change on the page. Any suggestions?
This was made for SharePoint 2007 almost 6 years ago so I’m afraid I cannot help you with it, but the code is fairly short and easy so you might be able to make it work by trial and error.
Alexander