Count groups in grouped list view

This is a short one on counting groups in a grouped list view like this (group count in the list view toolbar):
IMG

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

13 thoughts on “Count groups in grouped list view”

  1. 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

  2. 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

    1. 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

  3. 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).

    1. 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.

    1. 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

  4. 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?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.