diff --git a/MVCGrid/Scripts/MVCGrid.js b/MVCGrid/Scripts/MVCGrid.js index d1c97b9..1d951e0 100644 --- a/MVCGrid/Scripts/MVCGrid.js +++ b/MVCGrid/Scripts/MVCGrid.js @@ -28,7 +28,7 @@ var MVCGrid = new function () { } bindToolbarEvents(); - } + }; var bindToolbarEvents = function (){ @@ -180,7 +180,7 @@ var MVCGrid = new function () { this.getColumnVisibility = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.columnVisibility; - } + }; // public this.setColumnVisibility = function (mvcGridName, obj) { @@ -202,13 +202,13 @@ var MVCGrid = new function () { }); setURLAndReload(mvcGridName, newUrl); - } + }; // public this.getFilters = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.filters; - } + }; // public this.setFilters = function (mvcGridName, obj) { @@ -222,19 +222,19 @@ var MVCGrid = new function () { }); setURLAndReload(mvcGridName, newUrl); - } + }; // public this.getSortColumn = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.sortColumn; - } + }; // public this.getSortDirection = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.sortDirection; - } + }; // public this.setSort = function (mvcGridName, sortColumn, sortDirection) { @@ -253,7 +253,7 @@ var MVCGrid = new function () { this.getPage = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.pageNumber; - } + }; // public this.setPage = function (mvcGridName, pageNumber) { @@ -269,7 +269,7 @@ var MVCGrid = new function () { this.getPageSize = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.itemsPerPage; - } + }; // public this.setPageSize = function (mvcGridName, pageSize) { @@ -285,7 +285,7 @@ var MVCGrid = new function () { this.getAdditionalQueryOptions = function (mvcGridName) { var clientJson = getClientData(mvcGridName); return clientJson.additionalQueryOptions; - } + }; // public this.setAdditionalQueryOptions = function (mvcGridName, obj) { @@ -299,7 +299,7 @@ var MVCGrid = new function () { }); setURLAndReload(mvcGridName, newUrl); - } + }; // private var setURLAndReload = function (mvcGridName, newUrl) { @@ -362,7 +362,7 @@ var MVCGrid = new function () { } } }); - } + }; // public this.getExportUrl = function (mvcGridName) { @@ -373,7 +373,7 @@ var MVCGrid = new function () { exportUrl = updateURLParameter(exportUrl, 'Name', mvcGridName); return exportUrl; - } + }; }; diff --git a/MVCGrid/Web/MVCGridHtmlGenerator.cs b/MVCGrid/Web/MVCGridHtmlGenerator.cs index 5a05fa2..089ae01 100644 --- a/MVCGrid/Web/MVCGridHtmlGenerator.cs +++ b/MVCGrid/Web/MVCGridHtmlGenerator.cs @@ -24,9 +24,9 @@ internal static string GenerateClientDataTransferHtml(GridContext gridContext) sb.Append("{"); - sb.AppendFormat("\"name\": \"{0}\"", gridContext.GridName); + sb.AppendFormat("\"name\": \"{0}\"", HttpUtility.JavaScriptStringEncode(gridContext.GridName)); sb.Append(","); - sb.AppendFormat("\"sortColumn\": \"{0}\"", gridContext.QueryOptions.SortColumnName); + sb.AppendFormat("\"sortColumn\": \"{0}\"", HttpUtility.JavaScriptStringEncode(gridContext.QueryOptions.SortColumnName)); sb.Append(","); sb.AppendFormat("\"sortDirection\": \"{0}\"", gridContext.QueryOptions.SortDirection); sb.Append(","); @@ -77,7 +77,7 @@ private static string GenerateClientJsonVisibility(GridContext gridContext) sb.AppendFormat("\"{0}\": {{", cv.ColumnName); - sb.AppendFormat("\"{0}\": \"{1}\"", "headerText", gridColumn.HeaderText); + sb.AppendFormat("\"{0}\": \"{1}\"", "headerText", HttpUtility.JavaScriptStringEncode(gridColumn.HeaderText)); sb.Append(","); sb.AppendFormat("\"{0}\": {1}", "visible", cv.Visible.ToString().ToLower()); sb.Append(","); @@ -103,7 +103,7 @@ private static string GenerateClientJsonAdditional(GridContext gridContext) { sb.Append(","); } - sb.AppendFormat("\"{0}\": \"{1}\"", aqon, val); + sb.AppendFormat("\"{0}\": \"{1}\"", aqon, HttpUtility.JavaScriptStringEncode(val)); } return sb.ToString(); } @@ -125,7 +125,7 @@ private static string GenerateClientJsonFilter(GridContext gridContext) { sb.Append(","); } - sb.AppendFormat("\"{0}\": \"{1}\"", col.ColumnName, val); + sb.AppendFormat("\"{0}\": \"{1}\"", col.ColumnName, HttpUtility.JavaScriptStringEncode(val)); } return sb.ToString(); } diff --git a/MVCGridExample/App_Start/MVCGridConfig.cs b/MVCGridExample/App_Start/MVCGridConfig.cs index da69c97..9cf292e 100644 --- a/MVCGridExample/App_Start/MVCGridConfig.cs +++ b/MVCGridExample/App_Start/MVCGridConfig.cs @@ -944,7 +944,7 @@ public static void RegisterGrids() var options = context.QueryOptions; JobRepo repo = new JobRepo(); int totalRecords; - var data = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), null, false); + var data = repo.GetData(out totalRecords, null, options.GetLimitOffset(), options.GetLimitRowcount(), null, false); return new QueryResult() { diff --git a/MVCGridExample/Content/MVCGridConfig.txt b/MVCGridExample/Content/MVCGridConfig.txt index da69c97..9cf292e 100644 --- a/MVCGridExample/Content/MVCGridConfig.txt +++ b/MVCGridExample/Content/MVCGridConfig.txt @@ -944,7 +944,7 @@ namespace MVCGridExample var options = context.QueryOptions; JobRepo repo = new JobRepo(); int totalRecords; - var data = repo.GetData(out totalRecords, options.GetLimitOffset(), options.GetLimitRowcount(), null, false); + var data = repo.GetData(out totalRecords, null, options.GetLimitOffset(), options.GetLimitRowcount(), null, false); return new QueryResult() { diff --git a/MVCGridExample/Controllers/TestController.cs b/MVCGridExample/Controllers/TestController.cs new file mode 100644 index 0000000..aa0c950 --- /dev/null +++ b/MVCGridExample/Controllers/TestController.cs @@ -0,0 +1,75 @@ +using MVCGrid.Models; +using MVCGrid.Web.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace MVCGrid.Web.Controllers +{ + public class TestController : Controller + { + + public ActionResult Issue6() + { + return View(); + } + } + + public class TestControllerGrids : GridRegistration + { + public override void RegisterGrids() + { + //Issue6Grid + MVCGridDefinitionTable.Add("Issue6Grid", new MVCGridBuilder() + .WithSorting(true) + .WithDefaultSortColumn("Id") + .WithPaging(true) + .WithAllowChangingPageSize(true) + .WithMaxItemsPerPage(100) + .WithItemsPerPage(10) + .WithAdditionalQueryOptionName("globalsearch") + .AddColumns(cols => + { + cols.Add("Id", "Id", (row, context) => row.JobId.ToString()).WithSorting(true); + cols.Add("Name", "Name", (row, context) => row.Name).WithSorting(true); + + cols.Add("Contact") + .WithHeaderText("Contact") + .WithSorting(false) + .WithHtmlEncoding(false) + .WithValueExpression((p, c) => p.Contact != null ? c.UrlHelper.Action("Edit", "Contact", new { id = p.Contact.Id }) : "") + .WithValueTemplate("{Model.Contact.FullName}").WithPlainTextValueExpression((p, c) => p.Contact != null ? p.Contact.FullName : ""); + + cols.Add("Delete") + .WithHtmlEncoding(false) + .WithSorting(false) + .WithFiltering(false) + .WithHeaderText("") + .WithValueExpression((p, c) => c.UrlHelper.Action("Save", "Country", new { area = "General", id = p.JobId })) + .WithValueTemplate("") + .WithPlainTextValueExpression((p, c) => ""); + }) + .WithRetrieveDataMethod((context) => + { + + var options = context.QueryOptions; + + string globalsearch = options.GetAdditionalQueryOptionString("globalsearch"); + + JobRepo repo = new JobRepo(); + int totalRecords; + var data = repo.GetData(out totalRecords, globalsearch, options.GetLimitOffset(), + options.GetLimitRowcount(), options.GetSortColumnData(), options.SortDirection == SortDirection.Dsc); + + return new QueryResult() + { + Items = data, + TotalRecords = totalRecords + }; + }) + ); + } + } +} \ No newline at end of file diff --git a/MVCGridExample/MVCGridExample.csproj b/MVCGridExample/MVCGridExample.csproj index aa94ab3..16261c0 100644 --- a/MVCGridExample/MVCGridExample.csproj +++ b/MVCGridExample/MVCGridExample.csproj @@ -156,6 +156,7 @@ + SampleDB.tt @@ -299,6 +300,7 @@ + diff --git a/MVCGridExample/Models/JobRepo.cs b/MVCGridExample/Models/JobRepo.cs index 01b3047..e2833d5 100644 --- a/MVCGridExample/Models/JobRepo.cs +++ b/MVCGridExample/Models/JobRepo.cs @@ -9,7 +9,7 @@ public class JobRepo { const string CacheKey = "JobRepo"; - public IEnumerable GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc) + public IEnumerable GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc) { if (HttpContext.Current.Cache[CacheKey] == null) { @@ -37,35 +37,40 @@ public IEnumerable GetData(out int totalRecords, int? limitOffset, int? lim } List data = (List)HttpContext.Current.Cache[CacheKey]; - totalRecords = data.Count; + var q = data.AsQueryable(); - q = q.OrderBy(p => p.JobId); + if (!String.IsNullOrWhiteSpace(globalSearch)) + { + q = q.Where(p => p.Name.Contains(globalSearch)); + } + + totalRecords = q.Count(); if (!String.IsNullOrWhiteSpace(orderBy)) { - //switch (orderBy.ToLower()) - //{ - // case "col1": - // if (!desc) - // q = q.OrderBy(p => p.Col1); - // else - // q = q.OrderByDescending(p => p.Col1); - // break; - // case "col2": - // if (!desc) - // q = q.OrderBy(p => p.Col2); - // else - // q = q.OrderByDescending(p => p.Col2); - // break; - // case "col3": - // if (!desc) - // q = q.OrderBy(p => p.Col3); - // else - // q = q.OrderByDescending(p => p.Col3); - // break; - //} + switch (orderBy.ToLower()) + { + case "id": + if (!desc) + q = q.OrderBy(p => p.JobId); + else + q = q.OrderByDescending(p => p.JobId); + break; + case "name": + if (!desc) + q = q.OrderBy(p => p.Name); + else + q = q.OrderByDescending(p => p.Name); + break; + case "contact": + if (!desc) + q = q.OrderBy(p => p.Contact.FullName); + else + q = q.OrderByDescending(p => p.Contact.FullName); + break; + } } if (limitOffset.HasValue) diff --git a/MVCGridExample/Views/Test/Issue6.cshtml b/MVCGridExample/Views/Test/Issue6.cshtml new file mode 100644 index 0000000..910ecd0 --- /dev/null +++ b/MVCGridExample/Views/Test/Issue6.cshtml @@ -0,0 +1,68 @@ +@using MVCGrid.Web + +
+ + +
+
+ +
+
+
+@Html.MVCGrid("Issue6Grid") + + + +@section Scripts{ + +}