From ff04719914f281b462495568c15d287d71e37d37 Mon Sep 17 00:00:00 2001 From: Joe Harrison Date: Tue, 24 Feb 2015 23:03:32 -0500 Subject: [PATCH] added column defaults and tweaked demos --- MVCGrid/MVCGrid.csproj | 1 + MVCGrid/Models/ColumnDefaults.cs | 32 +++++++++++++ MVCGrid/Models/GridColumn.cs | 24 ++++++++-- MVCGrid/Models/GridColumnBuilder.cs | 24 ++++++++-- MVCGrid/Models/GridDefinition.cs | 47 ++++++++++--------- MVCGrid/Models/MVCGridBuilder.cs | 16 ++++++- MVCGridExample/App_Start/MVCGridConfig.cs | 41 +++++++++------- MVCGridExample/Content/MVCGridConfig.txt | 41 +++++++++------- MVCGridExample/Views/Demo/PageSizeDemo.cshtml | 19 ++++++-- 9 files changed, 173 insertions(+), 72 deletions(-) create mode 100644 MVCGrid/Models/ColumnDefaults.cs diff --git a/MVCGrid/MVCGrid.csproj b/MVCGrid/MVCGrid.csproj index 13891d1..f3c93af 100644 --- a/MVCGrid/MVCGrid.csproj +++ b/MVCGrid/MVCGrid.csproj @@ -53,6 +53,7 @@ + diff --git a/MVCGrid/Models/ColumnDefaults.cs b/MVCGrid/Models/ColumnDefaults.cs new file mode 100644 index 0000000..c8b57f7 --- /dev/null +++ b/MVCGrid/Models/ColumnDefaults.cs @@ -0,0 +1,32 @@ +using MVCGrid.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MVCGrid.Models +{ + public class ColumnDefaults : IMVCGridColumn + { + public ColumnDefaults() + { + ColumnName = null; + HeaderText = null; + EnableSorting = false; + HtmlEncode = true; + EnableFiltering = false; + Visible = true; + SortColumnData = null; + AllowChangeVisibility = false; + } + + public string ColumnName { get; set; } + public string HeaderText { get; set; } + public bool EnableSorting { get; set; } + public bool HtmlEncode { get; set; } + public bool EnableFiltering { get; set; } + public bool Visible { get; set; } + public object SortColumnData { get; set; } + public bool AllowChangeVisibility { get; set; } + } +} diff --git a/MVCGrid/Models/GridColumn.cs b/MVCGrid/Models/GridColumn.cs index f69413d..2243a70 100644 --- a/MVCGrid/Models/GridColumn.cs +++ b/MVCGrid/Models/GridColumn.cs @@ -11,11 +11,17 @@ public class GridColumn : IMVCGridColumn private string _headerText = null; public GridColumn() - : this(null, null, null) + : this(null, null, null, null) { } public GridColumn(string columnName, string headerText, Func valueExpression) + :this(columnName, headerText, valueExpression, null) + { + + } + + public GridColumn(string columnName, string headerText, Func valueExpression, ColumnDefaults columnDefaults) { if (!String.IsNullOrWhiteSpace(columnName)) { @@ -32,10 +38,18 @@ public GridColumn(string columnName, string headerText, Func diff --git a/MVCGrid/Models/GridColumnBuilder.cs b/MVCGrid/Models/GridColumnBuilder.cs index 4c341fe..369d4b9 100644 --- a/MVCGrid/Models/GridColumnBuilder.cs +++ b/MVCGrid/Models/GridColumnBuilder.cs @@ -7,9 +7,18 @@ namespace MVCGrid.Models { public class GridColumnListBuilder { + private ColumnDefaults _columnDefaults = null; + public GridColumnListBuilder() + : this(null) + { + } + + public GridColumnListBuilder(ColumnDefaults columnDefaults) { ColumnBuilders = new List>(); + + _columnDefaults = columnDefaults; } public List> ColumnBuilders { get; set; } @@ -26,7 +35,7 @@ public GridColumnBuilder Add(string columnName) public GridColumnBuilder Add(string columnName, string headerText, Func valueExpression) { - GridColumnBuilder col = new GridColumnBuilder(columnName, headerText, valueExpression); + GridColumnBuilder col = new GridColumnBuilder(columnName, headerText, valueExpression, _columnDefaults); ColumnBuilders.Add(col); @@ -44,18 +53,25 @@ public GridColumnBuilder Add(GridColumn column) public class GridColumnBuilder { - public GridColumnBuilder():this(null,null,null) + public GridColumnBuilder() + : this(null, null, null, null) { } public GridColumnBuilder(string columnName) - : this(columnName, null, null) + : this(columnName, null, null, null) { } public GridColumnBuilder(string columnName, string headerText, Func valueExpression) + :this(columnName, headerText, valueExpression, null) + { + + } + + public GridColumnBuilder(string columnName, string headerText, Func valueExpression, ColumnDefaults columnDefaults) { - GridColumn = new GridColumn(columnName, headerText, valueExpression); + GridColumn = new GridColumn(columnName, headerText, valueExpression, columnDefaults); } public GridColumn GridColumn { get; set; } diff --git a/MVCGrid/Models/GridDefinition.cs b/MVCGrid/Models/GridDefinition.cs index 4b665cc..8ae55b4 100644 --- a/MVCGrid/Models/GridDefinition.cs +++ b/MVCGrid/Models/GridDefinition.cs @@ -15,36 +15,39 @@ public abstract class GridDefinitionBase public class GridDefinition : GridDefinitionBase, IMVCGridDefinition { - public GridDefinition() : this(new GridDefaults()) + public GridDefinition() : this(null) { } - public GridDefinition(GridDefaults gridDefaults):base() + public GridDefinition(GridDefaults gridDefaults) + : base() { Columns = new List>(); - if (gridDefaults != null) + if (gridDefaults == null) { - this.PreloadData = gridDefaults.PreloadData; - this.Paging = gridDefaults.Paging; - this.ItemsPerPage = gridDefaults.ItemsPerPage; - this.Sorting = gridDefaults.Sorting; - this.DefaultSortColumn = gridDefaults.DefaultSortColumn; - this.NoResultsMessage = gridDefaults.NoResultsMessage; - this.ClientSideLoadingMessageFunctionName = gridDefaults.ClientSideLoadingMessageFunctionName; - this.ClientSideLoadingCompleteFunctionName = gridDefaults.ClientSideLoadingCompleteFunctionName; - this.Filtering = gridDefaults.Filtering; - this.RenderingEngine = gridDefaults.RenderingEngine; - this.TemplatingEngine = gridDefaults.TemplatingEngine; - this.AdditionalSettings = gridDefaults.AdditionalSettings; - this.RenderingMode = gridDefaults.RenderingMode; - this.ViewPath = gridDefaults.ViewPath; - this.QueryStringPrefix = gridDefaults.QueryStringPrefix; - this.ErrorMessageHtml = gridDefaults.ErrorMessageHtml; - this.AdditionalQueryOptionNames = gridDefaults.AdditionalQueryOptionNames; - this.AllowChangingPageSize = gridDefaults.AllowChangingPageSize; - this.MaxItemsPerPage = gridDefaults.MaxItemsPerPage; + gridDefaults = new GridDefaults(); } + this.PreloadData = gridDefaults.PreloadData; + this.Paging = gridDefaults.Paging; + this.ItemsPerPage = gridDefaults.ItemsPerPage; + this.Sorting = gridDefaults.Sorting; + this.DefaultSortColumn = gridDefaults.DefaultSortColumn; + this.NoResultsMessage = gridDefaults.NoResultsMessage; + this.ClientSideLoadingMessageFunctionName = gridDefaults.ClientSideLoadingMessageFunctionName; + this.ClientSideLoadingCompleteFunctionName = gridDefaults.ClientSideLoadingCompleteFunctionName; + this.Filtering = gridDefaults.Filtering; + this.RenderingEngine = gridDefaults.RenderingEngine; + this.TemplatingEngine = gridDefaults.TemplatingEngine; + this.AdditionalSettings = gridDefaults.AdditionalSettings; + this.RenderingMode = gridDefaults.RenderingMode; + this.ViewPath = gridDefaults.ViewPath; + this.QueryStringPrefix = gridDefaults.QueryStringPrefix; + this.ErrorMessageHtml = gridDefaults.ErrorMessageHtml; + this.AdditionalQueryOptionNames = gridDefaults.AdditionalQueryOptionNames; + this.AllowChangingPageSize = gridDefaults.AllowChangingPageSize; + this.MaxItemsPerPage = gridDefaults.MaxItemsPerPage; + } public IEnumerable GetColumns() diff --git a/MVCGrid/Models/MVCGridBuilder.cs b/MVCGrid/Models/MVCGridBuilder.cs index 59aade8..9b5e11c 100644 --- a/MVCGrid/Models/MVCGridBuilder.cs +++ b/MVCGrid/Models/MVCGridBuilder.cs @@ -8,14 +8,28 @@ namespace MVCGrid.Models { public class MVCGridBuilder { + ColumnDefaults _columnDefaults = null; + public MVCGridBuilder() { GridDefinition = new GridDefinition(); } public MVCGridBuilder(GridDefaults gridDefaults) + : this(gridDefaults, null) + { + } + + public MVCGridBuilder(ColumnDefaults columnDefaults) + : this(null, columnDefaults) + { + } + + public MVCGridBuilder(GridDefaults gridDefaults, ColumnDefaults columnDefaults) { GridDefinition = new GridDefinition(gridDefaults); + + _columnDefaults = columnDefaults; } public GridDefinition GridDefinition { get; set; } @@ -38,7 +52,7 @@ public MVCGridBuilder AddColumn(string name, string headerText, Func AddColumns(Action> columns) { - GridColumnListBuilder cols=new GridColumnListBuilder(); + GridColumnListBuilder cols = new GridColumnListBuilder(_columnDefaults); columns.Invoke(cols); foreach (var col in cols.ColumnBuilders) diff --git a/MVCGridExample/App_Start/MVCGridConfig.cs b/MVCGridExample/App_Start/MVCGridConfig.cs index ef382e5..b879e54 100644 --- a/MVCGridExample/App_Start/MVCGridConfig.cs +++ b/MVCGridExample/App_Start/MVCGridConfig.cs @@ -115,7 +115,12 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("SortableGrid", new MVCGridBuilder() + ColumnDefaults colDefauls = new ColumnDefaults() + { + EnableSorting = true + }; + + MVCGridDefinitionTable.Add("SortableGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -156,7 +161,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("PagingGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("PagingGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -207,7 +212,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("DIGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("DIGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -239,7 +244,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("FormattingGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("FormattingGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -279,7 +284,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("StyledGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("StyledGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -327,7 +332,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("Preloading", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Preloading", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -360,7 +365,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("CustomLoading", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomLoading", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -398,7 +403,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("Filtering", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Filtering", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -453,7 +458,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("ExportGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ExportGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add().WithColumnName("Id") @@ -500,7 +505,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -542,7 +547,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Col1").WithValueExpression((p, c) => p.Col1); @@ -571,7 +576,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("CustomStyle", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomStyle", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -614,7 +619,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("CustomRazorView", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomRazorView", new MVCGridBuilder(colDefauls) .WithRenderingMode(RenderingMode.Controller) .WithViewPath("~/Views/MVCGrid/_Custom.cshtml") .AddColumns(cols => @@ -658,7 +663,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("ValueTemplate", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ValueTemplate", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -712,7 +717,7 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("CustomErrorMessage", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomErrorMessage", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -784,7 +789,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("GlobalSearchGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("GlobalSearchGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -822,7 +827,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("PageSizeGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("PageSizeGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -856,7 +861,7 @@ public static void RegisterGrids() }) ); - MVCGridDefinitionTable.Add("ColumnVisibilityGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ColumnVisibilityGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) diff --git a/MVCGridExample/Content/MVCGridConfig.txt b/MVCGridExample/Content/MVCGridConfig.txt index ef382e5..b879e54 100644 --- a/MVCGridExample/Content/MVCGridConfig.txt +++ b/MVCGridExample/Content/MVCGridConfig.txt @@ -115,7 +115,12 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("SortableGrid", new MVCGridBuilder() + ColumnDefaults colDefauls = new ColumnDefaults() + { + EnableSorting = true + }; + + MVCGridDefinitionTable.Add("SortableGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -156,7 +161,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("PagingGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("PagingGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -207,7 +212,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("DIGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("DIGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -239,7 +244,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("FormattingGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("FormattingGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -279,7 +284,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("StyledGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("StyledGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -327,7 +332,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("Preloading", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Preloading", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -360,7 +365,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("CustomLoading", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomLoading", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -398,7 +403,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("Filtering", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Filtering", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -453,7 +458,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("ExportGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ExportGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add().WithColumnName("Id") @@ -500,7 +505,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -542,7 +547,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder() + MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Col1").WithValueExpression((p, c) => p.Col1); @@ -571,7 +576,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("CustomStyle", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomStyle", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -614,7 +619,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("CustomRazorView", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomRazorView", new MVCGridBuilder(colDefauls) .WithRenderingMode(RenderingMode.Controller) .WithViewPath("~/Views/MVCGrid/_Custom.cshtml") .AddColumns(cols => @@ -658,7 +663,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("ValueTemplate", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ValueTemplate", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -712,7 +717,7 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("CustomErrorMessage", new MVCGridBuilder() + MVCGridDefinitionTable.Add("CustomErrorMessage", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -784,7 +789,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("GlobalSearchGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("GlobalSearchGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -822,7 +827,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("PageSizeGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("PageSizeGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) @@ -856,7 +861,7 @@ namespace MVCGridExample }) ); - MVCGridDefinitionTable.Add("ColumnVisibilityGrid", new MVCGridBuilder() + MVCGridDefinitionTable.Add("ColumnVisibilityGrid", new MVCGridBuilder(colDefauls) .AddColumns(cols => { cols.Add("Id").WithSorting(false) diff --git a/MVCGridExample/Views/Demo/PageSizeDemo.cshtml b/MVCGridExample/Views/Demo/PageSizeDemo.cshtml index 9313999..c9df9ad 100644 --- a/MVCGridExample/Views/Demo/PageSizeDemo.cshtml +++ b/MVCGridExample/Views/Demo/PageSizeDemo.cshtml @@ -7,8 +7,15 @@ @section Scripts{ } @@ -29,8 +36,12 @@ Page Size: