From 623e97373ab5673073f803f2ee3e8d989876072a Mon Sep 17 00:00:00 2001 From: Joe Harrison Date: Tue, 10 Feb 2015 14:59:10 -0500 Subject: [PATCH] demo of multiple grids per page --- MVCGridExample/App_Start/MVCGridConfig.cs | 92 ++++++++++++++++++- MVCGridExample/Content/MVCGridConfig.txt | 92 ++++++++++++++++++- MVCGridExample/Controllers/DemoController.cs | 5 + MVCGridExample/MVCGridExample.csproj | 1 + MVCGridExample/Views/Demo/Multiple.cshtml | 33 +++++++ .../Views/Shared/_DemoLayout.cshtml | 1 + 6 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 MVCGridExample/Views/Demo/Multiple.cshtml diff --git a/MVCGridExample/App_Start/MVCGridConfig.cs b/MVCGridExample/App_Start/MVCGridConfig.cs index a58ca98..579dfd9 100644 --- a/MVCGridExample/App_Start/MVCGridConfig.cs +++ b/MVCGridExample/App_Start/MVCGridConfig.cs @@ -321,8 +321,11 @@ public static void RegisterGrids() int totalRecords; var repo = DependencyResolver.Current.GetService(); + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -447,12 +450,15 @@ public static void RegisterGrids() } } + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetFilterString("FirstName"), options.GetFilterString("LastName"), active, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -496,9 +502,59 @@ public static void RegisterGrids() int totalRecords; var repo = DependencyResolver.Current.GetService(); + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); + + return new QueryResult() + { + Items = items, + TotalRecords = totalRecords + }; + }) + ); + + MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder() + .AddColumns(cols => + { + cols.Add().WithColumnName("Id") + .WithSorting(false) + .WithHtmlEncoding(false) + .WithValueExpression((p, c) => + { + return String.Format("{1}", + c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id); + }) + .WithPlainTextValueExpression((p, c) => p.Id.ToString()); + cols.Add().WithColumnName("FirstName") + .WithHeaderText("First Name") + .WithValueExpression((p, c) => p.FirstName); + cols.Add().WithColumnName("LastName") + .WithHeaderText("Last Name") + .WithValueExpression((p, c) => p.LastName); + cols.Add().WithColumnName("Status") + .WithHeaderText("Status") + .WithValueExpression((p, c) => p.Active ? "Active" : "Inactive"); + }) + .WithSorting(true) + .WithDefaultSortColumn("LastName") + .WithPaging(true) + .WithItemsPerPage(10) + .WithQueryStringPrefix("grid1") + .WithRetrieveDataMethod((options) => + { + int totalRecords; + var repo = DependencyResolver.Current.GetService(); + + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + + var items = repo.GetData(out totalRecords, + options.GetLimitOffset(), options.GetLimitRowcount(), + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -508,6 +564,36 @@ public static void RegisterGrids() }) ); + MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder() + .AddColumns(cols => + { + cols.Add().WithColumnName("Col1") + .WithValueExpression((p, c) => p.Col1); + cols.Add().WithColumnName("Col2") + .WithValueExpression((p, c) => p.Col2); + cols.Add().WithColumnName("Col3") + .WithValueExpression((p, c) => p.Col3); + }) + .WithSorting(true) + .WithDefaultSortColumn("Col1") + .WithPaging(true) + .WithItemsPerPage(10) + .WithQueryStringPrefix("grid2") + .WithRetrieveDataMethod((options) => + { + TestItemRepository repo = new TestItemRepository(); + int totalRecords; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), options.SortColumn, options.SortDirection == SortDirection.Dsc); + + return new QueryResult() + { + Items = items, + TotalRecords = totalRecords + }; + }) + ); + + MVCGridDefinitionTable.Add("GridDefinitionGrid", new MVCGridBuilder() diff --git a/MVCGridExample/Content/MVCGridConfig.txt b/MVCGridExample/Content/MVCGridConfig.txt index a58ca98..579dfd9 100644 --- a/MVCGridExample/Content/MVCGridConfig.txt +++ b/MVCGridExample/Content/MVCGridConfig.txt @@ -321,8 +321,11 @@ namespace MVCGridExample int totalRecords; var repo = DependencyResolver.Current.GetService(); + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -447,12 +450,15 @@ namespace MVCGridExample } } + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetFilterString("FirstName"), options.GetFilterString("LastName"), active, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -496,9 +502,59 @@ namespace MVCGridExample int totalRecords; var repo = DependencyResolver.Current.GetService(); + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), - options.SortColumn, options.SortDirection == SortDirection.Dsc); + sortColumn, options.SortDirection == SortDirection.Dsc); + + return new QueryResult() + { + Items = items, + TotalRecords = totalRecords + }; + }) + ); + + MVCGridDefinitionTable.Add("Multiple1", new MVCGridBuilder() + .AddColumns(cols => + { + cols.Add().WithColumnName("Id") + .WithSorting(false) + .WithHtmlEncoding(false) + .WithValueExpression((p, c) => + { + return String.Format("{1}", + c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id); + }) + .WithPlainTextValueExpression((p, c) => p.Id.ToString()); + cols.Add().WithColumnName("FirstName") + .WithHeaderText("First Name") + .WithValueExpression((p, c) => p.FirstName); + cols.Add().WithColumnName("LastName") + .WithHeaderText("Last Name") + .WithValueExpression((p, c) => p.LastName); + cols.Add().WithColumnName("Status") + .WithHeaderText("Status") + .WithValueExpression((p, c) => p.Active ? "Active" : "Inactive"); + }) + .WithSorting(true) + .WithDefaultSortColumn("LastName") + .WithPaging(true) + .WithItemsPerPage(10) + .WithQueryStringPrefix("grid1") + .WithRetrieveDataMethod((options) => + { + int totalRecords; + var repo = DependencyResolver.Current.GetService(); + + string sortColumn = options.SortColumn; + if (String.Compare(sortColumn, "status", true) == 0) sortColumn = "active"; + + var items = repo.GetData(out totalRecords, + options.GetLimitOffset(), options.GetLimitRowcount(), + sortColumn, options.SortDirection == SortDirection.Dsc); return new QueryResult() { @@ -508,6 +564,36 @@ namespace MVCGridExample }) ); + MVCGridDefinitionTable.Add("Multiple2", new MVCGridBuilder() + .AddColumns(cols => + { + cols.Add().WithColumnName("Col1") + .WithValueExpression((p, c) => p.Col1); + cols.Add().WithColumnName("Col2") + .WithValueExpression((p, c) => p.Col2); + cols.Add().WithColumnName("Col3") + .WithValueExpression((p, c) => p.Col3); + }) + .WithSorting(true) + .WithDefaultSortColumn("Col1") + .WithPaging(true) + .WithItemsPerPage(10) + .WithQueryStringPrefix("grid2") + .WithRetrieveDataMethod((options) => + { + TestItemRepository repo = new TestItemRepository(); + int totalRecords; + var items = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), options.SortColumn, options.SortDirection == SortDirection.Dsc); + + return new QueryResult() + { + Items = items, + TotalRecords = totalRecords + }; + }) + ); + + MVCGridDefinitionTable.Add("GridDefinitionGrid", new MVCGridBuilder() diff --git a/MVCGridExample/Controllers/DemoController.cs b/MVCGridExample/Controllers/DemoController.cs index 3e1e0d8..e83e2d9 100644 --- a/MVCGridExample/Controllers/DemoController.cs +++ b/MVCGridExample/Controllers/DemoController.cs @@ -69,5 +69,10 @@ public ActionResult Export() { return View(); } + + public ActionResult Multiple() + { + return View(); + } } } \ No newline at end of file diff --git a/MVCGridExample/MVCGridExample.csproj b/MVCGridExample/MVCGridExample.csproj index eaeae47..cc69f3c 100644 --- a/MVCGridExample/MVCGridExample.csproj +++ b/MVCGridExample/MVCGridExample.csproj @@ -265,6 +265,7 @@ + diff --git a/MVCGridExample/Views/Demo/Multiple.cshtml b/MVCGridExample/Views/Demo/Multiple.cshtml new file mode 100644 index 0000000..98421bc --- /dev/null +++ b/MVCGridExample/Views/Demo/Multiple.cshtml @@ -0,0 +1,33 @@ +@using MVCGrid.Web +@{ + ViewBag.Title = "Multiple Grids"; +} + +

@ViewBag.Title

+ +

To have more than 1 grid on a page and still do sorting/paging/filtering, set a GridDefinition.QueryStringPrefix to something unique for each grid.

+ +

Grid 1

+@Html.MVCGrid("Multiple1") + +

Grid 2

+@Html.MVCGrid("Multiple2") + +

Code

+ +
+
Inside MVCGridConfig.cs
+
+
@CodeSnippetHelper.GetCodeSnippet("Multiple1")
+ +
@CodeSnippetHelper.GetCodeSnippet("Multiple2")
+
+
+ +@* +
+
View
+
+
@@Html.MVCGrid("PagingGrid")
+
+
*@ \ No newline at end of file diff --git a/MVCGridExample/Views/Shared/_DemoLayout.cshtml b/MVCGridExample/Views/Shared/_DemoLayout.cshtml index 51694ee..2faffe1 100644 --- a/MVCGridExample/Views/Shared/_DemoLayout.cshtml +++ b/MVCGridExample/Views/Shared/_DemoLayout.cshtml @@ -19,6 +19,7 @@
  • @Html.ActionLink("Filtering", "filtering")
  • @Html.ActionLink("Client-Side API", "clientsideapi")
  • @Html.ActionLink("Export to CSV", "export")
  • +
  • @Html.ActionLink("Multiple Grids on Same Page", "multiple")