0

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

Community
  • 1
  • 1
Guillermo
  • 213
  • 3
  • 15

2 Answers2

2

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,
Mark
  • 3,123
  • 4
  • 20
  • 31
0

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Is this answer assuming that `loadOnce: true`? – Mark Apr 18 '13 at 23:06
  • @Mark: Yes, of cause! Either `datatype: "local"` or some remote `datatype` ("json", "xml" or "jsonp") with `loadonce: true`. If one use `datatype: "json"` without `loadonce: true` then one can't calculate the data on the client side which one not has here. If you don't use and don't want to use `loadonce: true` then one *have to* do this on the server side and place the result in the `userdata` (like in your answer). – Oleg Apr 18 '13 at 23:22
  • Thats what I thought :) I just want to be crystal clear on the subject, when I did my answer I assumed he was pulling pages of data from the server. – Mark Apr 18 '13 at 23:41
  • @Mark: I agree that you could do this better on the server side, but the last words of the question was "but want to do it client side"... So I wrote my answer so. – Oleg Apr 18 '13 at 23:44
  • I agree, I guess we both had to guess a bit because he wasn't clear on how he was getting the data for the grid, oh well it's all good. Congratulations on breaking the 100K rep! Job well done! – Mark Apr 18 '13 at 23:49
  • @Mark: Thanks! It would be nice to see your bug reports on [trirand forum](http://www.trirand.com/blog/?page_id=393/bugs/) and pull requests on [github](https://github.com/tonytomov/jqGrid) which improves jqGrid. I think you knows enough good jqGrid to do this. – Oleg Apr 19 '13 at 00:00
  • Wow thanks for the vote of confidence. I can't say I have considered myself qualified enough for that, but maybe an a couple of months. I'm still getting a functional mastery of github myself through my own development. – Mark Apr 19 '13 at 00:07
  • Mark, Oleg, Many thanks for the responses of both, sorry for not mentioning that the data is sent from the server. – Guillermo Apr 19 '13 at 14:00
  • The code is in the [question](http://stackoverflow.com/questions/15982243/setting-default-value-inline-add-jqgrid) and i need sumarize the column Cabezas – Guillermo Apr 19 '13 at 14:05
  • @Guillermo: Why you wrote: "but want to do it client side" at the end of the question. Usage of aggregation function on the backend or in the database is more effectively (native code or database engine in more quickly as JavaScript interpreter/compiler). Is is not easy to calculate "total" on the server and just place it in `userdata` part of the server response? – Oleg Apr 19 '13 at 14:11
  • @Oleg, Sorry I'm something new with some functionality of json and jqrig, and then did not know `userdata`, again thank you very much for your answers and for your interest in my question. – Guillermo Apr 19 '13 at 14:31
  • @Oleg, by by the way, yes is more easy. – Guillermo Apr 19 '13 at 14:33
  • @Oleg, To be more specific part of my problem is that the view has two models, one that displays the header data and another showing the detail, the problem I had was that when you add, delete or edit data in the grid, the answer server did not include an update of the header, so I imagine a client side solution – Guillermo Apr 19 '13 at 14:54
  • @Guillermo: Sorry, I'm not sure what you mean. You used `url` option of jqGrid which address controller action `'@Url.Action("ListVendedores")'` which provide the data for the grid. The data contains for example `records` field which results produced with *separate SQL query* (like `SELECT COUNT(*) from MyTable`). You can include `userdata: {totalSum: XXX}` where XXX you can calculate with `SELECT SUM(amount) FROM Orders`. You can use LINQ to SQL to generate the statement indirectly (see [here](http://msdn.microsoft.com/en-us/library/bb386928.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1)) – Oleg Apr 19 '13 at 16:44
  • @Oleg, no problem, sorry English is not my forte, I will use `userdata: {totalSum: XXX}` – Guillermo Apr 19 '13 at 17:30
  • 1
    @Guillermo: Look at [here](http://stackoverflow.com/a/3849513/315935) for code example and don't forget that 1) one should use `userdata` property (NOT `userData`) in JSON and `$(this).jqGrid("getGridParam", "userData")` (NOT `userdata`) to access internally saved data. 2) the value of `userdata` must be **object** like `userdata: {amount: 123}` (NOT scalar like `userdata: 123). – Oleg Apr 19 '13 at 17:43