Get count of rows for each group in grid

1 Answer 1141 Views
Grid
Acadia
Top achievements
Rank 1
Iron
Acadia asked on 01 Dec 2022, 04:52 PM | edited on 01 Dec 2022, 08:34 PM

Hello, I am struggling to figure out how to get a count of how many rows are in each of my kendo grid groups in the DataBound method. I can't find anything on this. My grid is pretty simple, and it is grouped by a single column (tran_code). 

The grouping works fine but I cannot figure out how to hide the group header row altogether if there is only 1 row in that group, otherwise show it.  All I have so far is my grouping, which is working.  I am using @html helper for the grid:

@(Html.Kendo().Grid(CarryFwdDetail)
.Name("grid")
.Group(groups => groups.Add(p => p.TranCode))

In a javascript function how can I access the group rows and count them (for each group), then hide that group header?
Any help is appreciated!
Thanks, Justin

 

Update:

I an effort to provide as much code as possible, I have been able to get to a point where I can get the count groups there are.  But I still don't know how to get the count of rows within each group and hide that groups header if it is = 1:


  .Events(e=>e.DataBound(@<text>function(e){
                    var view = this.dataSource.view();
                    if(this.dataSource.group())
                    {
                        var count = 0;
                        eachGroupItems(view, function(items, index)
                        {
                            count++
                            alert(count);
                        });
                    }
        }</text>))


function eachGroupItems(data, func) {
        for (var idx = 0, length = data.length; idx < length; idx++) {
          if (data[idx].hasSubgroups) {
            if (eachGroupItems(data[idx].items, func)) {             
              return true;
            }
          } else if (func(data[idx].items, data[idx])) {
            return true;
          }
        }
}

Update 2

I have now figured out how to get the count of rows within each grouping:


function eachGroupItems(data, func) {
        for (var i = 0, length = data.length; i < length; i++) 
        {
           alert("Data length: " + data[i].items.length);  //= number of rows in each group

            if(data[i].items.length == 1)
            {

            }
        }
}


Can anybody help me with how to hide the group header row?

 

Update 3

I figured it out.  Here is the new javascript function to replace eachGroupItems()

function eachGroupItems(data, func) {
        for (var i = 0, length = data.length; i < length; i++) 
        {
           //alert("Data length: " + data[i].items.length);  //= number of rows in each group

            if(data[i].items.length == 1)
            {
                $('#grid tbody .k-grouping-row').hide(); 
            }
        }
}

 

Hopefully this helps somebody in the future...

 

Thanks

1 Answer, 1 is accepted

Sort by
1
Accepted
Aleksandar
Telerik team
answered on 06 Dec 2022, 08:21 AM

Hi Acadia,

This would be the approach for handling the requirements. We have a knowledgebase article demonstrating how to get all the Groups and SubGroups in Grid

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Acadia
Top achievements
Rank 1
Iron
Answers by
Aleksandar
Telerik team
Share this question
or