Guillermo
Guillermo

Reputation: 213

Sumarize Columns on Jqgrid

I read about using getCol metod from jqgrid for display a summarized column in the footer of grid in this question with something like:

var grid = $("#list"),
sum = grid.jqGrid('getCol', 'amount', false, 'sum');

grid.jqGrid('footerData','set', {ID: 'Total:', amount: sum});

But when the grid include a pager the calculation occurs only for the displayed data, i need show a General total of column in a Textbox.

This should be done using a foreach, or is there some method to do it most effec?

Obviously I could include it in my model but want to do it client side

Upvotes: 0

Views: 913

Answers (2)

Oleg
Oleg

Reputation: 221997

You can use grid.jqGrid("getGridParam", "data") first to get the internal data of the grid. The data contains information about all pages. Then you can make simple for loop to enumerate the items in the array returned by grid.jqGrid("getGridParam", "data"). Every item has property amount. So you need use parseInt(item.amount, 10) to convert the data to int (or use parseFloat) and calculate the required sum.

Upvotes: 0

Mark
Mark

Reputation: 3123

Unless you are passing down the entire data set there is no way to do this with multiple pages. It would be very easy to do this and to pass this data down as part of your userdata.

Ex

Controller:

    foreach (var item in dataset)
    {
        summaryTotal += (sum of some relevant values)
    }
    ...
    records = totalRecords,                
    userdata = new { ColumnNameForFooterSubtotal = summaryTotal.ToString() },
    rows = (
    ...

Then in your view I would recommend using a footer, then the value you computed in the controller will appear in the footer row of the column name you used above. (ColumnNameForFooterSubtotal )

   ...//jqGrid setup
   //turn on the footer   
   footerrow: true,
   userDataOnFooter: true,

Upvotes: 2

Related Questions