This question is locked. New answers and comments are not allowed.
Hi, i understand that we can sum up a column, but, can we do a sum for the column for the current page?
for example, for current page there is 20 rows, i just want to sum on a particular column for that 20 rows only..
for example, for current page there is 20 rows, i just want to sum on a particular column for that 20 rows only..
4 Answers, 1 is accepted
0
Dadv
Top achievements
Rank 1
answered on 28 Mar 2012, 03:24 PM
Hi,
Server side binding or Client side Binding?
Server side binding or Client side Binding?
0
Eugene
Top achievements
Rank 1
answered on 29 Mar 2012, 03:45 AM
I would like to know both client and server .. thanks!!
0
Accepted
Dadv
Top achievements
Rank 1
answered on 29 Mar 2012, 08:58 AM
Both techniques are really different.
Server side you need to do a custom server binding then in controller, define a ViewBag value (or ViewData) for the aggregate you want to display.
Then create a FooterTemplate
This is the easiest way... now the more complicate :
Client side you need a lot of think, because you have to calculate in the ondatabound and onrowdatabound event and expect the oncomplete(ondatabinding) event for paging reset.
function onDataBound(e) {
$('#subTotal').html(subTotal);
}
function onRowDataBound(e) {
var dataItem = e.dataItem;
subTotal = subTotal + parseInt(dataItem.InvoiceNo);
}
function onComplete(e) {
if (e.name == "dataBinding")
subTotal = 0;
}
Server side you need to do a custom server binding then in controller, define a ViewBag value (or ViewData) for the aggregate you want to display.
public partial class GridController : Controller
{
[GridAction(GridName = "Grid")]
public ActionResult CustomServerBinding(GridCommand command)
{
IEnumerable data = GetServerData(!IsEmptyCommand(command) ? command : new GridCommand()); ViewBag.SubTotal = 123456; //Calculate the subtotal here
return View(data);
}
}
Then create a FooterTemplate
columns.Bound(o => o.InvoiceNo)
.FooterTemplate(result =>
{
@<text>
<div>SubTotal : @ViewBag.SubTotal</div>
@</text>
})
This is the easiest way... now the more complicate :
Client side you need a lot of think, because you have to calculate in the ondatabound and onrowdatabound event and expect the oncomplete(ondatabinding) event for paging reset.
columns.Bound(o => o.InvoiceNo)
.ClientFooterTemplate("<div id='subTotal'>0</div>");
.ClientEvents(events => events
.OnComplete("onComplete").OnRowDataBound("onRowDataBound").OnDataBound("onDataBound"))
var subTotal = 0;function onDataBound(e) {
$('#subTotal').html(subTotal);
}
function onRowDataBound(e) {
var dataItem = e.dataItem;
subTotal = subTotal + parseInt(dataItem.InvoiceNo);
}
function onComplete(e) {
if (e.name == "dataBinding")
subTotal = 0;
}
0
Eugene
Top achievements
Rank 1
answered on 29 Mar 2012, 09:22 AM
WOW, thanks for the wonderful answer!!