From 24bb27e5f9aee953c53de17eb8c2c97d97cd4f94 Mon Sep 17 00:00:00 2001 From: Joe Harrison Date: Wed, 11 Feb 2015 14:56:03 -0500 Subject: [PATCH] Modified RetrieveData method to take GridContext for consistency and for access to GridName. Consolidated documentation grid configurations. --- MVCGrid/Models/GridDefinition.cs | 4 +- MVCGrid/Models/MVCGridBuilder.cs | 8 +- MVCGridExample/App_Start/MVCGridConfig.cs | 164 ++++++++--------- MVCGridExample/Content/MVCGridConfig.txt | 166 +++++++++--------- MVCGridExample/MVCGridExample.csproj | 1 - .../Views/Documentation/Index.cshtml | 56 +++++- 6 files changed, 227 insertions(+), 172 deletions(-) diff --git a/MVCGrid/Models/GridDefinition.cs b/MVCGrid/Models/GridDefinition.cs index 8b36995..eb14b13 100644 --- a/MVCGrid/Models/GridDefinition.cs +++ b/MVCGrid/Models/GridDefinition.cs @@ -71,14 +71,14 @@ public void AddColumn(GridColumn column) private List> Columns { get; set; } - public Func> RetrieveData { get; set; } + public Func> RetrieveData { get; set; } public Func RowCssClassExpression { get; set; } internal override List GetData(GridContext context, out int? totalRecords) { List resultRows = new List(); - var queryResult = RetrieveData(context.QueryOptions); + var queryResult = RetrieveData(context); totalRecords = queryResult.TotalRecords; if (context.GridDefinition.Paging && !totalRecords.HasValue) diff --git a/MVCGrid/Models/MVCGridBuilder.cs b/MVCGrid/Models/MVCGridBuilder.cs index 12dd3ee..4455e60 100644 --- a/MVCGrid/Models/MVCGridBuilder.cs +++ b/MVCGrid/Models/MVCGridBuilder.cs @@ -44,7 +44,13 @@ public MVCGridBuilder AddColumns(Action> columns) return this; } - public MVCGridBuilder WithRetrieveDataMethod(Func> retrieveData) + public MVCGridBuilder AddColumn(GridColumn column) + { + GridDefinition.AddColumn(column); + return this; + } + + public MVCGridBuilder WithRetrieveDataMethod(Func> retrieveData) { GridDefinition.RetrieveData = retrieveData; return this; diff --git a/MVCGridExample/App_Start/MVCGridConfig.cs b/MVCGridExample/App_Start/MVCGridConfig.cs index 8af92d9..394ddb1 100644 --- a/MVCGridExample/App_Start/MVCGridConfig.cs +++ b/MVCGridExample/App_Start/MVCGridConfig.cs @@ -49,8 +49,10 @@ public static void RegisterGrids() .WithPaging(true) .WithItemsPerPage(10) .WithPreloadData(true) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -109,8 +111,9 @@ public static void RegisterGrids() }) .WithSorting(true) .WithDefaultSortColumn("LastName") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; var result = new QueryResult(); using (var db = new SampleDatabaseEntities()) @@ -160,8 +163,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + var result = new QueryResult(); using (var db = new SampleDatabaseEntities()) @@ -218,8 +223,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -262,8 +269,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -315,8 +324,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -352,8 +363,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -387,8 +400,10 @@ public static void RegisterGrids() .WithItemsPerPage(10) .WithClientSideLoadingMessageFunctionName("showLoading") .WithClientSideLoadingCompleteFunctionName("hideLoading") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -430,8 +445,10 @@ public static void RegisterGrids() .WithPaging(true) .WithItemsPerPage(10) .WithFiltering(true) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -496,8 +513,10 @@ public static void RegisterGrids() .WithItemsPerPage(10) .WithClientSideLoadingMessageFunctionName("showLoading") .WithClientSideLoadingCompleteFunctionName("hideLoading") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -543,8 +562,10 @@ public static void RegisterGrids() .WithPaging(true) .WithItemsPerPage(10) .WithQueryStringPrefix("grid1") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -578,8 +599,10 @@ public static void RegisterGrids() .WithPaging(true) .WithItemsPerPage(10) .WithQueryStringPrefix("grid2") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + TestItemRepository repo = new TestItemRepository(); int totalRecords; var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), options.SortColumn, options.SortDirection == SortDirection.Dsc); @@ -620,8 +643,10 @@ public static void RegisterGrids() .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(20) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -641,79 +666,54 @@ public static void RegisterGrids() ); - MVCGridDefinitionTable.Add("GridDefinitionGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("GridDefinition"); - - return result; - }) + var docsReturnTypeColumn = new GridColumn() + { + ColumnName = "ReturnType", + HeaderText = "Return Type", + HtmlEncode = false, + ValueExpression = (p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return)) + }; + var docsNameColumn = new GridColumn() + { + ColumnName = "Name", + HtmlEncode = false, + ValueExpression = (p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name)) + }; + var docsDescriptionColumn = new GridColumn() + { + ColumnName = "Description", + ValueExpression = (p, c) => p.Description + }; + + Func> docsLoadData = (context) => + { + var result = new QueryResult(); + + DocumentationRepository repo = new DocumentationRepository(); + result.Items = repo.GetData(context.GridName); + + return result; + }; + + MVCGridDefinitionTable.Add("GridDefinition", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); - MVCGridDefinitionTable.Add("GridColumnGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("GridColumn"); - - return result; - }) + MVCGridDefinitionTable.Add("GridColumn", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); - MVCGridDefinitionTable.Add("QueryOptionsGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("QueryOptions"); - - return result; - }) + MVCGridDefinitionTable.Add("QueryOptions", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); diff --git a/MVCGridExample/Content/MVCGridConfig.txt b/MVCGridExample/Content/MVCGridConfig.txt index eefc5dd..394ddb1 100644 --- a/MVCGridExample/Content/MVCGridConfig.txt +++ b/MVCGridExample/Content/MVCGridConfig.txt @@ -49,8 +49,10 @@ namespace MVCGridExample .WithPaging(true) .WithItemsPerPage(10) .WithPreloadData(true) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -109,8 +111,9 @@ namespace MVCGridExample }) .WithSorting(true) .WithDefaultSortColumn("LastName") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; var result = new QueryResult(); using (var db = new SampleDatabaseEntities()) @@ -160,8 +163,10 @@ namespace MVCGridExample .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + var result = new QueryResult(); using (var db = new SampleDatabaseEntities()) @@ -218,8 +223,10 @@ namespace MVCGridExample .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -262,8 +269,10 @@ namespace MVCGridExample .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -315,8 +324,10 @@ namespace MVCGridExample .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -347,13 +358,15 @@ namespace MVCGridExample .WithHeaderText("Last Name") .WithValueExpression((p, c) => p.LastName); }) - .WithPreloadData(true) + .WithPreloadData(false) .WithSorting(true) .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(10) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -387,8 +400,10 @@ namespace MVCGridExample .WithItemsPerPage(10) .WithClientSideLoadingMessageFunctionName("showLoading") .WithClientSideLoadingCompleteFunctionName("hideLoading") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -430,8 +445,10 @@ namespace MVCGridExample .WithPaging(true) .WithItemsPerPage(10) .WithFiltering(true) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -496,8 +513,10 @@ namespace MVCGridExample .WithItemsPerPage(10) .WithClientSideLoadingMessageFunctionName("showLoading") .WithClientSideLoadingCompleteFunctionName("hideLoading") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -543,8 +562,10 @@ namespace MVCGridExample .WithPaging(true) .WithItemsPerPage(10) .WithQueryStringPrefix("grid1") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -578,8 +599,10 @@ namespace MVCGridExample .WithPaging(true) .WithItemsPerPage(10) .WithQueryStringPrefix("grid2") - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + TestItemRepository repo = new TestItemRepository(); int totalRecords; var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), options.SortColumn, options.SortDirection == SortDirection.Dsc); @@ -620,8 +643,10 @@ namespace MVCGridExample .WithDefaultSortColumn("LastName") .WithPaging(true) .WithItemsPerPage(20) - .WithRetrieveDataMethod((options) => + .WithRetrieveDataMethod((context) => { + var options = context.QueryOptions; + int totalRecords; var repo = DependencyResolver.Current.GetService(); @@ -641,79 +666,54 @@ namespace MVCGridExample ); - MVCGridDefinitionTable.Add("GridDefinitionGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("GridDefinition"); - - return result; - }) + var docsReturnTypeColumn = new GridColumn() + { + ColumnName = "ReturnType", + HeaderText = "Return Type", + HtmlEncode = false, + ValueExpression = (p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return)) + }; + var docsNameColumn = new GridColumn() + { + ColumnName = "Name", + HtmlEncode = false, + ValueExpression = (p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name)) + }; + var docsDescriptionColumn = new GridColumn() + { + ColumnName = "Description", + ValueExpression = (p, c) => p.Description + }; + + Func> docsLoadData = (context) => + { + var result = new QueryResult(); + + DocumentationRepository repo = new DocumentationRepository(); + result.Items = repo.GetData(context.GridName); + + return result; + }; + + MVCGridDefinitionTable.Add("GridDefinition", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); - MVCGridDefinitionTable.Add("GridColumnGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("GridColumn"); - - return result; - }) + MVCGridDefinitionTable.Add("GridColumn", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); - MVCGridDefinitionTable.Add("QueryOptionsGrid", new MVCGridBuilder() - .AddColumns(cols => - { - cols.Add().WithColumnName("ReturnType") - .WithHeaderText("Return Type") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Return))); - cols.Add().WithColumnName("Name") - .WithHtmlEncoding(false) - .WithValueExpression((p, c) => String.Format("{0}", HttpUtility.HtmlEncode(p.Name))); - cols.Add().WithColumnName("Description") - .WithValueExpression((p, c) => p.Description); - }) - .WithPreloadData(true) - .WithRetrieveDataMethod((options) => - { - var result = new QueryResult(); - - DocumentationRepository repo = new DocumentationRepository(); - result.Items = repo.GetData("QueryOptions"); - - return result; - }) + MVCGridDefinitionTable.Add("QueryOptions", new MVCGridBuilder() + .AddColumn(docsReturnTypeColumn) + .AddColumn(docsNameColumn) + .AddColumn(docsDescriptionColumn) + .WithRetrieveDataMethod(docsLoadData) ); diff --git a/MVCGridExample/MVCGridExample.csproj b/MVCGridExample/MVCGridExample.csproj index c05e9e8..5cad587 100644 --- a/MVCGridExample/MVCGridExample.csproj +++ b/MVCGridExample/MVCGridExample.csproj @@ -270,7 +270,6 @@ - diff --git a/MVCGridExample/Views/Documentation/Index.cshtml b/MVCGridExample/Views/Documentation/Index.cshtml index b8aaa54..8373d3c 100644 --- a/MVCGridExample/Views/Documentation/Index.cshtml +++ b/MVCGridExample/Views/Documentation/Index.cshtml @@ -6,10 +6,60 @@

@ViewBag.Title

Grid Definition

-@Html.MVCGrid("GridDefinitionGrid") +@Html.MVCGrid("GridDefinition")

Column Definition

-@Html.MVCGrid("GridColumnGrid") +@Html.MVCGrid("GridColumn")

QueryOptions

-@Html.MVCGrid("QueryOptionsGrid") +@Html.MVCGrid("QueryOptions") + +

+ +

Code used to generate the 3 above grids:

+

Just because I could

+
+var docsReturnTypeColumn = new GridColumn<methoddocitem>()
+{
+    ColumnName = "ReturnType",
+    HeaderText = "Return Type",
+    HtmlEncode = false,
+    ValueExpression = (p, c) => String.Format("<code>{0}</code>", HttpUtility.HtmlEncode(p.Return))
+};
+var docsNameColumn = new GridColumn<methoddocitem>()
+{
+    ColumnName = "Name",
+    HtmlEncode = false,
+    ValueExpression = (p, c) => String.Format("<code>{0}</code>", HttpUtility.HtmlEncode(p.Name))
+};
+var docsDescriptionColumn = new GridColumn<methoddocitem>()
+{
+    ColumnName = "Description",
+    ValueExpression = (p, c) => p.Description
+};
+Func<gridcontext, queryresult<MethodDocItem>> docsLoadData = (context) =>
+{
+    var result = new QueryResult<MethodDocItem>();
+    DocumentationRepository repo = new DocumentationRepository();
+    result.Items = repo.GetData(context.GridName);
+    return result;
+};
+MVCGridDefinitionTable.Add("GridDefinition", new MVCGridBuilder<MethodDocItem>()
+    .AddColumn(docsReturnTypeColumn)
+    .AddColumn(docsNameColumn)
+    .AddColumn(docsDescriptionColumn)
+    .WithRetrieveDataMethod(docsLoadData)
+);
+MVCGridDefinitionTable.Add("GridColumn", new MVCGridBuilder<MethodDocItem>()
+    .AddColumn(docsReturnTypeColumn)
+    .AddColumn(docsNameColumn)
+    .AddColumn(docsDescriptionColumn)
+    .WithRetrieveDataMethod(docsLoadData)
+);
+MVCGridDefinitionTable.Add("QueryOptions", new MVCGridBuilder<MethodDocItem>()
+    .AddColumn(docsReturnTypeColumn)
+    .AddColumn(docsNameColumn)
+    .AddColumn(docsDescriptionColumn)
+    .WithRetrieveDataMethod(docsLoadData)
+);
+
\ No newline at end of file