From af97a186fc787aea6571931a0ded35741fb9ebb1 Mon Sep 17 00:00:00 2001 From: Joe Harrison Date: Wed, 11 Feb 2015 10:26:59 -0500 Subject: [PATCH] refactored html writer into rendering engine and create grid engine to consolidate code --- MVCGrid/Engine/GridEngine.cs | 162 +++++++++++ MVCGrid/Interfaces/IMVCGridHtmlWriter.cs | 13 - MVCGrid/Interfaces/IMVCGridRenderingEngine.cs | 2 +- MVCGrid/MVCGrid.csproj | 3 +- MVCGrid/Models/GridDefinition.cs | 49 ++-- MVCGrid/Rendering/BootstrapHtmlWriter.cs | 223 --------------- MVCGrid/Rendering/CsvRenderingEngine.cs | 20 +- MVCGrid/Rendering/HtmlRenderingEngine.cs | 264 +++++++++++------- MVCGrid/Web/HtmlExtensions.cs | 12 +- MVCGrid/Web/MVCGridHandler.cs | 52 +--- 10 files changed, 388 insertions(+), 412 deletions(-) create mode 100644 MVCGrid/Engine/GridEngine.cs delete mode 100644 MVCGrid/Interfaces/IMVCGridHtmlWriter.cs delete mode 100644 MVCGrid/Rendering/BootstrapHtmlWriter.cs diff --git a/MVCGrid/Engine/GridEngine.cs b/MVCGrid/Engine/GridEngine.cs new file mode 100644 index 0000000..c145c35 --- /dev/null +++ b/MVCGrid/Engine/GridEngine.cs @@ -0,0 +1,162 @@ +using MVCGrid.Interfaces; +using MVCGrid.Models; +using MVCGrid.Rendering; +using MVCGrid.Utility; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Web; + +namespace MVCGrid.Engine +{ + public class GridEngine + { + public void Run(IMVCGridRenderingEngine renderingEngine, GridContext gridContext, Stream outputStream) + { + if (!renderingEngine.AllowsPaging) + { + gridContext.QueryOptions.ItemsPerPage = null; + } + + int? totalRecords; + var rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords); + + // if a page was requested higher than available pages, requery for first page + if (rows.Count == 0 && totalRecords.HasValue && totalRecords.Value > 0) + { + gridContext.QueryOptions.PageIndex = 0; + rows = ((GridDefinitionBase)gridContext.GridDefinition).GetData(gridContext, out totalRecords); + } + + var model = PrepModel(totalRecords, rows, gridContext); + + renderingEngine.Render(model, outputStream); + } + + private RenderingModel PrepModel(int? totalRecords, List rows, Models.GridContext gridContext) + { + RenderingModel model = new RenderingModel(); + + model.HandlerPath = HtmlUtility.GetHandlerPath(); + model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName); + + PrepColumns(gridContext, model); + //PrepRows(data, gridContext, model); + model.Rows = rows; + + if (model.Rows.Count == 0) + { + model.NoResultsMessage = gridContext.GridDefinition.NoResultsMessage; + } + + model.PagingModel = null; + if (gridContext.QueryOptions.ItemsPerPage.HasValue) + { + model.PagingModel = new PagingModel(); + + int currentPageIndex = gridContext.QueryOptions.PageIndex.Value; + + model.PagingModel.TotalRecords = totalRecords.Value; + + model.PagingModel.FirstRecord = (currentPageIndex * gridContext.QueryOptions.ItemsPerPage.Value) + 1; + model.PagingModel.LastRecord = (model.PagingModel.FirstRecord + gridContext.QueryOptions.ItemsPerPage.Value) - 1; + if (model.PagingModel.LastRecord > model.PagingModel.TotalRecords) + { + model.PagingModel.LastRecord = model.PagingModel.TotalRecords; + } + model.PagingModel.CurrentPage = currentPageIndex + 1; + + var numberOfPagesD = (model.PagingModel.TotalRecords + 0.0) / (gridContext.QueryOptions.ItemsPerPage.Value + 0.0); + model.PagingModel.NumberOfPages = (int)Math.Ceiling(numberOfPagesD); + + for (int i = 1; i <= model.PagingModel.NumberOfPages; i++) + { + model.PagingModel.PageLinks.Add(i, HtmlUtility.MakeGotoPageLink(gridContext.GridName, i)); + } + } + return model; + } + + //private void PrepRows(Models.GridData data, Models.GridContext gridContext, RenderingModel model) + //{ + // foreach (var item in data.Rows) + // { + // Row renderingRow = new Row(); + // model.Rows.Add(renderingRow); + + // if (!String.IsNullOrWhiteSpace(item.RowCssClass)) + // { + // renderingRow.CalculatedCssClass = item.RowCssClass; + // } + + // foreach (var col in gridContext.GetVisibleColumns()) + // { + // string val = ""; + + // if (item.Values.ContainsKey(col.ColumnName)) + // { + // val = item.Values[col.ColumnName]; + // } + + // Cell renderingCell = new Cell(); + // renderingRow.Cells.Add(col.ColumnName, renderingCell); + // if (item.CellCssClasses.ContainsKey(col.ColumnName)) + // { + // string cellCss = item.CellCssClasses[col.ColumnName]; + // if (!String.IsNullOrWhiteSpace(cellCss)) + // { + // renderingCell.CalculatedCssClass = cellCss; + // } + // } + + // if (col.HtmlEncode) + // { + // renderingCell.HtmlText = HttpUtility.HtmlEncode(val); + // } + // else + // { + // renderingCell.HtmlText = val; + // } + // } + // } + //} + + private void PrepColumns(Models.GridContext gridContext, RenderingModel model) + { + foreach (var col in gridContext.GetVisibleColumns()) + { + Column renderingColumn = new Column(); + model.Columns.Add(renderingColumn); + renderingColumn.Name = col.ColumnName; + renderingColumn.HeaderText = col.HeaderText; + + if (gridContext.GridDefinition.Sorting && col.EnableSorting) + { + SortDirection linkDirection = SortDirection.Asc; + SortDirection iconDirection = SortDirection.Unspecified; + + if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Asc) + { + iconDirection = SortDirection.Asc; + linkDirection = SortDirection.Dsc; + } + else if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Dsc) + { + iconDirection = SortDirection.Dsc; + linkDirection = SortDirection.Asc; + } + else + { + iconDirection = SortDirection.Unspecified; + linkDirection = SortDirection.Dsc; + } + + renderingColumn.Onclick = HtmlUtility.MakeSortLink(gridContext.GridName, col.ColumnName, linkDirection); + renderingColumn.SortIconDirection = iconDirection; + } + } + } + } +} diff --git a/MVCGrid/Interfaces/IMVCGridHtmlWriter.cs b/MVCGrid/Interfaces/IMVCGridHtmlWriter.cs deleted file mode 100644 index b51385c..0000000 --- a/MVCGrid/Interfaces/IMVCGridHtmlWriter.cs +++ /dev/null @@ -1,13 +0,0 @@ -using MVCGrid.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace MVCGrid.Interfaces -{ - public interface IMVCGridHtmlWriter - { - string WriteHtml(RenderingModel model); //, Models.GridContext gridContext - } -} diff --git a/MVCGrid/Interfaces/IMVCGridRenderingEngine.cs b/MVCGrid/Interfaces/IMVCGridRenderingEngine.cs index 83368d4..4ac25af 100644 --- a/MVCGrid/Interfaces/IMVCGridRenderingEngine.cs +++ b/MVCGrid/Interfaces/IMVCGridRenderingEngine.cs @@ -13,6 +13,6 @@ public interface IMVCGridRenderingEngine { bool AllowsPaging { get; } void PrepareResponse(HttpResponse response); - void Render(GridData data, GridContext gridContext, Stream outputStream); + void Render(RenderingModel model, Stream outputStream); } } diff --git a/MVCGrid/MVCGrid.csproj b/MVCGrid/MVCGrid.csproj index cfc0910..76e74f4 100644 --- a/MVCGrid/MVCGrid.csproj +++ b/MVCGrid/MVCGrid.csproj @@ -48,13 +48,12 @@ - + - diff --git a/MVCGrid/Models/GridDefinition.cs b/MVCGrid/Models/GridDefinition.cs index a38c7ca..cef95d0 100644 --- a/MVCGrid/Models/GridDefinition.cs +++ b/MVCGrid/Models/GridDefinition.cs @@ -10,17 +10,17 @@ namespace MVCGrid.Models { public abstract class GridDefinitionBase { - internal abstract GridData GetData(GridContext context); + internal abstract List GetData(GridContext context, out int? totalRecords); } public class GridDefinition : GridDefinitionBase, IMVCGridDefinition { - private Type _htmlWriterType; + private Type _defaultRenderingEngine; const string DefaultNoResultsMessage = "No results."; public GridDefinition() : this(null) { - + //_defaultRenderingEngine= } public GridDefinition(GridConfiguration copyFromConfig):base() @@ -32,7 +32,7 @@ public GridDefinition(GridConfiguration copyFromConfig):base() Columns = new List>(); NoResultsMessage = DefaultNoResultsMessage; - _htmlWriterType = typeof(MVCGrid.Rendering.BootstrapHtmlWriter); + //_htmlWriterType = typeof(MVCGrid.Rendering.BootstrapHtmlWriter); } public GridConfiguration GridConfiguration { get; set; } @@ -78,58 +78,63 @@ public void AddColumn(GridColumn column) public Func> RetrieveData { get; set; } public Func RowCssClassExpression { get; set; } - internal override GridData GetData(GridContext context) + internal override List GetData(GridContext context, out int? totalRecords) { - GridData result = new GridData(); + List resultRows = new List(); var queryResult = RetrieveData(context.QueryOptions); - result.TotalRecords = queryResult.TotalRecords; + totalRecords = queryResult.TotalRecords; - if (context.GridDefinition.Paging && !result.TotalRecords.HasValue) + if (context.GridDefinition.Paging && !totalRecords.HasValue) { throw new Exception("When paging is enabled, QueryResult must contain the TotalRecords"); } foreach (var item in queryResult.Items) { - GridRow thisRow = new GridRow(); + Row thisRow = new Row(); if (RowCssClassExpression != null) { string rowCss = RowCssClassExpression(item, context); if (!String.IsNullOrWhiteSpace(rowCss)) { - thisRow.RowCssClass = rowCss; + thisRow.CalculatedCssClass = rowCss; } } foreach (var col in this.Columns) { - string val = col.ValueExpression(item, context); + Cell thisCell = new Cell(); + thisRow.Cells.Add(col.ColumnName, thisCell); - string plainVal = val; - if (col.PlainTextValueExpression != null) + thisCell.HtmlText = col.ValueExpression(item, context); + + if (col.HtmlEncode) { - plainVal = col.PlainTextValueExpression(item, context); + thisCell.HtmlText = System.Web.HttpUtility.HtmlEncode(thisCell.HtmlText); } - thisRow.Values.Add(col.ColumnName, val); - thisRow.PlainTextValues.Add(col.ColumnName, plainVal); + thisCell.PlainText = thisCell.HtmlText; + if (col.PlainTextValueExpression != null) + { + thisCell.PlainText = col.PlainTextValueExpression(item, context); + } if (col.CellCssClassExpression != null) { string cellCss = col.CellCssClassExpression(item, context); if (!String.IsNullOrWhiteSpace(cellCss)) { - thisRow.CellCssClasses.Add(col.ColumnName, cellCss); + thisCell.CalculatedCssClass = cellCss; } } } - result.Rows.Add(thisRow); + resultRows.Add(thisRow); } - return result; + return resultRows; } /// @@ -149,15 +154,15 @@ internal override GridData GetData(GridContext context) public string ClientSideLoadingMessageFunctionName { get; set; } public string ClientSideLoadingCompleteFunctionName { get; set; } - public Type HtmlWriterType + public Type DefaultRenderingEngine { get { - return _htmlWriterType; + return _defaultRenderingEngine; } set { - _htmlWriterType = value; + _defaultRenderingEngine = value; } } } diff --git a/MVCGrid/Rendering/BootstrapHtmlWriter.cs b/MVCGrid/Rendering/BootstrapHtmlWriter.cs deleted file mode 100644 index bd4ed62..0000000 --- a/MVCGrid/Rendering/BootstrapHtmlWriter.cs +++ /dev/null @@ -1,223 +0,0 @@ -using MVCGrid.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace MVCGrid.Rendering -{ - public class BootstrapHtmlWriter : IMVCGridHtmlWriter - { - private string CssTable; - private string HtmlImageSortAsc; - private string HtmlImageSortDsc; - private string HtmlImageSort; - - public BootstrapHtmlWriter() - { - CssTable = "table table-striped table-bordered"; - } - - public string WriteHtml(Models.RenderingModel model) - { - HtmlImageSortAsc = String.Format("", model.HandlerPath); - HtmlImageSortDsc = String.Format("", model.HandlerPath); - HtmlImageSort = String.Format("", model.HandlerPath); - - StringBuilder sbHtml = new StringBuilder(); - - sbHtml.AppendFormat(""); - - RenderHeader(model, sbHtml); - - if (model.Rows.Count > 0) - { - RenderBody(model, sbHtml); - } - else - { - sbHtml.Append(""); - sbHtml.Append(""); - sbHtml.AppendFormat(""); - sbHtml.Append(""); - sbHtml.Append(""); - } - sbHtml.AppendLine("
", model.Columns.Count()); - sbHtml.Append(model.NoResultsMessage); - sbHtml.Append("
"); - - RenderPaging(model, sbHtml); - - - return sbHtml.ToString(); - } - - private void AppendCssAttribute(string classString, StringBuilder sbHtml) - { - if (!String.IsNullOrWhiteSpace(classString)) - { - sbHtml.Append(String.Format(" class='{0}'", classString)); - } - } - - private void RenderBody(Models.RenderingModel model, StringBuilder sbHtml) - { - sbHtml.AppendLine(""); - - foreach (var row in model.Rows) - { - sbHtml.Append(""); - - foreach (var col in model.Columns) - { - var cell = row.Cells[col.Name]; - - sbHtml.Append(""); - sbHtml.Append(cell.HtmlText); - sbHtml.Append(""); - } - sbHtml.AppendLine(" "); - } - - sbHtml.AppendLine(""); - } - - private void RenderHeader(Models.RenderingModel model, StringBuilder sbHtml) - { - sbHtml.AppendLine(""); - sbHtml.AppendLine(" "); - foreach (var col in model.Columns) - { - sbHtml.Append(""); - - sbHtml.Append(col.HeaderText); - - if (col.SortIconDirection.HasValue) - { - switch (col.SortIconDirection) - { - case Models.SortDirection.Asc: - sbHtml.Append(" "); - sbHtml.Append(HtmlImageSortAsc); - break; - case Models.SortDirection.Dsc: - sbHtml.Append(" "); - sbHtml.Append(HtmlImageSortDsc); - break; - case Models.SortDirection.Unspecified: - sbHtml.Append(" "); - sbHtml.Append(HtmlImageSort); - break; - } - } - sbHtml.AppendLine(""); - } - sbHtml.AppendLine(" "); - sbHtml.AppendLine(""); - } - - private void RenderPaging(Models.RenderingModel model, StringBuilder sbHtml) - { - if (model.PagingModel == null) - { - return; - } - - Models.PagingModel pagingModel = model.PagingModel; - - sbHtml.Append("
"); - sbHtml.Append("
"); - sbHtml.AppendFormat("Showing {0} to {1} of {2} entries", - pagingModel.FirstRecord, pagingModel.LastRecord, pagingModel.TotalRecords - ); - sbHtml.Append("
"); - - - sbHtml.Append("
"); - int pageToStart = pagingModel.CurrentPage - 2; - while (pageToStart < 1) - { - pageToStart++; - } - int pageToEnd = pageToStart + 4; - while (pageToEnd > pagingModel.NumberOfPages) - { - pageToStart--; - pageToEnd = pageToStart + 4; - } - while (pageToStart < 1) - { - pageToStart++; - } - - sbHtml.Append(""); - sbHtml.Append("
"); - sbHtml.Append("
"); - } - } -} diff --git a/MVCGrid/Rendering/CsvRenderingEngine.cs b/MVCGrid/Rendering/CsvRenderingEngine.cs index 69fbe42..fa4407c 100644 --- a/MVCGrid/Rendering/CsvRenderingEngine.cs +++ b/MVCGrid/Rendering/CsvRenderingEngine.cs @@ -22,39 +22,35 @@ public void PrepareResponse(System.Web.HttpResponse httpResponse) httpResponse.AddHeader("content-disposition", "attachment; filename=\"" + "export" + ".csv\""); } - public void Render(Models.GridData data, Models.GridContext gridContext, Stream outputStream) + public void Render(Models.RenderingModel model, Stream outputStream) { - using (StreamWriter sw = new StreamWriter(outputStream)) { StringBuilder sbHeaderRow = new StringBuilder(); - foreach (var col in gridContext.GetVisibleColumns()) + foreach (var col in model.Columns) { if (sbHeaderRow.Length != 0) { sbHeaderRow.Append(","); } - sbHeaderRow.Append(CsvEncode(col.ColumnName)); + sbHeaderRow.Append(CsvEncode(col.Name)); } sbHeaderRow.AppendLine(); sw.Write(sbHeaderRow.ToString()); - foreach (var item in data.Rows) + foreach (var item in model.Rows) { StringBuilder sbRow = new StringBuilder(); - foreach (var col in gridContext.GetVisibleColumns()) + foreach (var col in model.Columns) { + var cell = item.Cells[col.Name]; + if (sbRow.Length != 0) { sbRow.Append(","); } - string val = ""; - - if (item.PlainTextValues.ContainsKey(col.ColumnName)) - { - val = item.PlainTextValues[col.ColumnName]; - } + string val = cell.PlainText; sbRow.Append(CsvEncode(val)); } diff --git a/MVCGrid/Rendering/HtmlRenderingEngine.cs b/MVCGrid/Rendering/HtmlRenderingEngine.cs index 81c5561..fab2e75 100644 --- a/MVCGrid/Rendering/HtmlRenderingEngine.cs +++ b/MVCGrid/Rendering/HtmlRenderingEngine.cs @@ -13,11 +13,14 @@ namespace MVCGrid.Rendering { public class HtmlRenderingEngine : IMVCGridRenderingEngine { - IMVCGridHtmlWriter _writer; + private string CssTable; + private string HtmlImageSortAsc; + private string HtmlImageSortDsc; + private string HtmlImageSort; - public HtmlRenderingEngine(IMVCGridHtmlWriter writer) + public HtmlRenderingEngine() { - _writer = writer; + CssTable = "table table-striped table-bordered"; } public void PrepareResponse(HttpResponse response) @@ -29,139 +32,208 @@ public bool AllowsPaging get { return true; } } - public void Render(GridData data, GridContext gridContext, Stream outputStream)//HttpResponse httpResponse + public void Render(RenderingModel model, Stream outputStream) { - RenderingModel model = PrepModel(data, gridContext); + HtmlImageSortAsc = String.Format("", model.HandlerPath); + HtmlImageSortDsc = String.Format("", model.HandlerPath); + HtmlImageSort = String.Format("", model.HandlerPath); - var content = _writer.WriteHtml(model); + StringBuilder sbHtml = new StringBuilder(); - using (StreamWriter sw = new StreamWriter(outputStream)) - { - sw.Write(content.ToString()); - } - } + sbHtml.AppendFormat(""); - private RenderingModel PrepModel(Models.GridData data, Models.GridContext gridContext) - { - RenderingModel model = new RenderingModel(); + RenderHeader(model, sbHtml); - model.HandlerPath = HtmlUtility.GetHandlerPath(); - model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName); + if (model.Rows.Count > 0) + { + RenderBody(model, sbHtml); + } + else + { + sbHtml.Append(""); + sbHtml.Append(""); + sbHtml.AppendFormat(""); + sbHtml.Append(""); + sbHtml.Append(""); + } + sbHtml.AppendLine("
", model.Columns.Count()); + sbHtml.Append(model.NoResultsMessage); + sbHtml.Append("
"); - PrepColumns(gridContext, model); - PrepRows(data, gridContext, model); + RenderPaging(model, sbHtml); - if (model.Rows.Count == 0) + using (StreamWriter sw = new StreamWriter(outputStream)) { - model.NoResultsMessage = gridContext.GridDefinition.NoResultsMessage; + sw.Write(sbHtml.ToString()); } + } - model.PagingModel = null; - if (gridContext.QueryOptions.ItemsPerPage.HasValue) + private void AppendCssAttribute(string classString, StringBuilder sbHtml) + { + if (!String.IsNullOrWhiteSpace(classString)) { - model.PagingModel = new PagingModel(); + sbHtml.Append(String.Format(" class='{0}'", classString)); + } + } - int currentPageIndex = gridContext.QueryOptions.PageIndex.Value; + private void RenderBody(Models.RenderingModel model, StringBuilder sbHtml) + { + sbHtml.AppendLine(""); - model.PagingModel.TotalRecords = data.TotalRecords.Value; + foreach (var row in model.Rows) + { + sbHtml.Append(""); - model.PagingModel.FirstRecord = (currentPageIndex * gridContext.QueryOptions.ItemsPerPage.Value) + 1; - model.PagingModel.LastRecord = (model.PagingModel.FirstRecord + gridContext.QueryOptions.ItemsPerPage.Value) - 1; - if (model.PagingModel.LastRecord > data.TotalRecords) + foreach (var col in model.Columns) { - model.PagingModel.LastRecord = data.TotalRecords.Value; - } - model.PagingModel.CurrentPage = currentPageIndex + 1; + var cell = row.Cells[col.Name]; - var numberOfPagesD = (data.TotalRecords.Value + 0.0) / (gridContext.QueryOptions.ItemsPerPage.Value + 0.0); - model.PagingModel.NumberOfPages = (int)Math.Ceiling(numberOfPagesD); - - for (int i = 1; i <= model.PagingModel.NumberOfPages; i++) - { - model.PagingModel.PageLinks.Add(i, HtmlUtility.MakeGotoPageLink(gridContext.GridName, i)); + sbHtml.Append(""); + sbHtml.Append(cell.HtmlText); + sbHtml.Append(""); } + sbHtml.AppendLine(" "); } - return model; + + sbHtml.AppendLine(""); } - private void PrepRows(Models.GridData data, Models.GridContext gridContext, RenderingModel model) + private void RenderHeader(Models.RenderingModel model, StringBuilder sbHtml) { - foreach (var item in data.Rows) + sbHtml.AppendLine(""); + sbHtml.AppendLine(" "); + foreach (var col in model.Columns) { - Row renderingRow = new Row(); - model.Rows.Add(renderingRow); + sbHtml.Append(""); - foreach (var col in gridContext.GetVisibleColumns()) - { - string val = ""; + sbHtml.Append(col.HeaderText); - if (item.Values.ContainsKey(col.ColumnName)) - { - val = item.Values[col.ColumnName]; - } - - Cell renderingCell = new Cell(); - renderingRow.Cells.Add(col.ColumnName, renderingCell); - if (item.CellCssClasses.ContainsKey(col.ColumnName)) - { - string cellCss = item.CellCssClasses[col.ColumnName]; - if (!String.IsNullOrWhiteSpace(cellCss)) - { - renderingCell.CalculatedCssClass = cellCss; - } - } - - if (col.HtmlEncode) - { - renderingCell.HtmlText = HttpUtility.HtmlEncode(val); - } - else + if (col.SortIconDirection.HasValue) + { + switch (col.SortIconDirection) { - renderingCell.HtmlText = val; + case Models.SortDirection.Asc: + sbHtml.Append(" "); + sbHtml.Append(HtmlImageSortAsc); + break; + case Models.SortDirection.Dsc: + sbHtml.Append(" "); + sbHtml.Append(HtmlImageSortDsc); + break; + case Models.SortDirection.Unspecified: + sbHtml.Append(" "); + sbHtml.Append(HtmlImageSort); + break; } } + sbHtml.AppendLine(""); } + sbHtml.AppendLine(" "); + sbHtml.AppendLine(""); } - private void PrepColumns(Models.GridContext gridContext, RenderingModel model) + private void RenderPaging(Models.RenderingModel model, StringBuilder sbHtml) { - foreach (var col in gridContext.GetVisibleColumns()) + if (model.PagingModel == null) { - Column renderingColumn = new Column(); - model.Columns.Add(renderingColumn); - renderingColumn.Name = col.ColumnName; - renderingColumn.HeaderText = col.HeaderText; + return; + } - if (gridContext.GridDefinition.Sorting && col.EnableSorting) - { - SortDirection linkDirection = SortDirection.Asc; - SortDirection iconDirection = SortDirection.Unspecified; + Models.PagingModel pagingModel = model.PagingModel; + + sbHtml.Append("
"); + sbHtml.Append("
"); + sbHtml.AppendFormat("Showing {0} to {1} of {2} entries", + pagingModel.FirstRecord, pagingModel.LastRecord, pagingModel.TotalRecords + ); + sbHtml.Append("
"); - if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Asc) - { - iconDirection = SortDirection.Asc; - linkDirection = SortDirection.Dsc; - } - else if (gridContext.QueryOptions.SortColumn == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Dsc) - { - iconDirection = SortDirection.Dsc; - linkDirection = SortDirection.Asc; - } - else - { - iconDirection = SortDirection.Unspecified; - linkDirection = SortDirection.Dsc; - } - renderingColumn.Onclick = HtmlUtility.MakeSortLink(gridContext.GridName, col.ColumnName, linkDirection); - renderingColumn.SortIconDirection = iconDirection; + sbHtml.Append("
"); + int pageToStart = pagingModel.CurrentPage - 2; + while (pageToStart < 1) + { + pageToStart++; + } + int pageToEnd = pageToStart + 4; + while (pageToEnd > pagingModel.NumberOfPages) + { + pageToStart--; + pageToEnd = pageToStart + 4; + } + while (pageToStart < 1) + { + pageToStart++; + } + + sbHtml.Append(""); + sbHtml.Append("
"); + sbHtml.Append("
"); } } } diff --git a/MVCGrid/Web/HtmlExtensions.cs b/MVCGrid/Web/HtmlExtensions.cs index ba70e58..031dfab 100644 --- a/MVCGrid/Web/HtmlExtensions.cs +++ b/MVCGrid/Web/HtmlExtensions.cs @@ -1,4 +1,5 @@ -using MVCGrid.Interfaces; +using MVCGrid.Engine; +using MVCGrid.Interfaces; using MVCGrid.Rendering; using MVCGrid.Utility; using System; @@ -35,14 +36,13 @@ internal static IHtmlString MVCGrid(this HtmlHelper helper, string name, IMVCGri var gridContext = GridContextUtility.Create(HttpContext.Current, gridName, grid, options); - IMVCGridHtmlWriter writer = (IMVCGridHtmlWriter)Activator.CreateInstance(gridContext.GridDefinition.HtmlWriterType, true); - IMVCGridRenderingEngine renderingEngine = new HtmlRenderingEngine(writer); - - var results = ((MVCGrid.Models.GridDefinitionBase)grid).GetData(gridContext); + IMVCGridRenderingEngine renderingEngine = new HtmlRenderingEngine(); + GridEngine engine = new GridEngine(); + using (MemoryStream ms = new MemoryStream()) { - renderingEngine.Render(results, gridContext, ms); + engine.Run(renderingEngine, gridContext, ms); preload = Encoding.ASCII.GetString(ms.ToArray()); } diff --git a/MVCGrid/Web/MVCGridHandler.cs b/MVCGrid/Web/MVCGridHandler.cs index 2395065..5146f42 100644 --- a/MVCGrid/Web/MVCGridHandler.cs +++ b/MVCGrid/Web/MVCGridHandler.cs @@ -1,4 +1,5 @@ -using MVCGrid.Interfaces; +using MVCGrid.Engine; +using MVCGrid.Interfaces; using MVCGrid.Models; using MVCGrid.Rendering; using MVCGrid.Utility; @@ -157,16 +158,14 @@ private void HandleTable(HttpContext context) { string gridName = context.Request["Name"]; - StringBuilder sbDebug = new StringBuilder(); - foreach (string key in context.Request.QueryString.AllKeys) - { - sbDebug.Append(key); - sbDebug.Append(" = "); - sbDebug.Append(context.Request.QueryString[key]); - sbDebug.Append("
"); - } - - + //StringBuilder sbDebug = new StringBuilder(); + //foreach (string key in context.Request.QueryString.AllKeys) + //{ + // sbDebug.Append(key); + // sbDebug.Append(" = "); + // sbDebug.Append(context.Request.QueryString[key]); + // sbDebug.Append("
"); + //} var grid = MVCGridDefinitionTable.GetDefinitionInterface(gridName); @@ -174,35 +173,14 @@ private void HandleTable(HttpContext context) var gridContext = GridContextUtility.Create(context, gridName, grid, options); - IMVCGridHtmlWriter writer = (IMVCGridHtmlWriter)Activator.CreateInstance(gridContext.GridDefinition.HtmlWriterType, true); - - IMVCGridRenderingEngine renderingEngine = DetermineRenderingEngine(context, writer); - - - //if (renderingEngine is HtmlRenderingEngine) - //{ - // context.Response.Write(sbDebug.ToString()); - //} - - if (!renderingEngine.AllowsPaging) - { - gridContext.QueryOptions.ItemsPerPage = null; - } - - var results = ((GridDefinitionBase)grid).GetData(gridContext); - - // if a page was requested higher than available pages, requery for first page - if (results.Rows.Count == 0 && results.TotalRecords.HasValue && results.TotalRecords.Value > 0) - { - gridContext.QueryOptions.PageIndex = 0; - results = ((GridDefinitionBase)grid).GetData(gridContext); - } + IMVCGridRenderingEngine renderingEngine = DetermineRenderingEngine(context); + GridEngine engine = new GridEngine(); renderingEngine.PrepareResponse(context.Response); - renderingEngine.Render(results, gridContext, context.Response.OutputStream); + engine.Run(renderingEngine, gridContext, context.Response.OutputStream); } - private IMVCGridRenderingEngine DetermineRenderingEngine(HttpContext context, IMVCGridHtmlWriter writer) + private IMVCGridRenderingEngine DetermineRenderingEngine(HttpContext context) { IMVCGridRenderingEngine engine = null; @@ -217,7 +195,7 @@ private IMVCGridRenderingEngine DetermineRenderingEngine(HttpContext context, IM if (engine == null) { - engine = new HtmlRenderingEngine(writer); + engine = new HtmlRenderingEngine(); } return engine;