Am avut de facut o aplicatie cu ASP.NET MVC – si bineinteles, cu un grid ce lista niste vinzari si cu total.
Asta e ceea ce mi-a pus probleme : totalul … Asa incit, dupa ce am studiat grid-ul de la ASP.NET MVC Contrib , a trebuit sa il extind. Si nu a fost foarte greu. Mai intii , practica - sintaxa e asemanatoare, doar ca adaug o functie pentru footer :
<% =Html.HtmlGridFooter<Lines>(ViewData.Model.Lines,
(x) =>
{
decimal total = 0;
foreach (var s in x) { total += s.Total; };
return "<tr><td colspan=1 align=right>Total</td><td colspan=1>"+ total.ToString("#.00")+"</td></tr>";
}
) si de aici incepe grid-ul obisnuit.
Teoria : a trebuit sa extind HtmlTableGridRenderer si sa il atasez de grid …Probabil ca o sa fie nevoie sa am , odata, doi grid renderer?
Cod :
public class HtmlGridFooter<T> : Grid<T>
where T : class
{
public HtmlGridFooter(IEnumerable<T> dataSource, TextWriter writer, ViewContext context) :
base(dataSource, writer, context)
{
}
public Func<IEnumerable<T>, string> footer
{
set
{
this.RenderUsing(new FooterHtmlGrid<T>() { actions = base.DataSource, footer = value });
}
}
}
public class FooterHtmlGrid<T> : HtmlTableGridRenderer<T>
where T : class
{
public IEnumerable<T> actions;
public Func<IEnumerable<T>,string > footer;
protected override void RenderGridEnd(bool isEmpty)
{
if (footer != null)
{
base.RenderText(footer(actions));
}
base.RenderGridEnd(isEmpty);
}
}
si intr-o clasa statica ar trebui pus:
public static IGrid<T> HtmlGridFooter<T>(this HtmlHelper helper, IEnumerable<T> dataSource, Func<IEnumerable<T>, string> footer)
where T : class
{
HtmlGridFooter<T> g = new HtmlGridFooter<T>(dataSource, helper.ViewContext.HttpContext.Response.Output, helper.ViewContext);
g.footer = footer;
return g;
}