From 020f3b1ec231f90f51bc7e888b50cb98583a14e8 Mon Sep 17 00:00:00 2001 From: Gabriela Komorowska Date: Thu, 9 Jul 2015 13:38:52 +0200 Subject: [PATCH] Added possibility to reload grid using Post method, without displaying parameters in querystring. --- MVCGrid/Interfaces/IMVCGridDefinition.cs | 5 + MVCGrid/Models/GridDefaults.cs | 2 + MVCGrid/Models/GridDefinition.cs | 6 ++ MVCGrid/Models/MVCGridBuilder.cs | 9 ++ MVCGrid/Scripts/MVCGrid.js | 116 ++++++++++++++++------- MVCGrid/Web/MVCGridHtmlGenerator.cs | 3 + MVCGrid/Web/QueryStringParser.cs | 36 +++---- 7 files changed, 123 insertions(+), 54 deletions(-) diff --git a/MVCGrid/Interfaces/IMVCGridDefinition.cs b/MVCGrid/Interfaces/IMVCGridDefinition.cs index e7d82b7..4d9069e 100644 --- a/MVCGrid/Interfaces/IMVCGridDefinition.cs +++ b/MVCGrid/Interfaces/IMVCGridDefinition.cs @@ -125,5 +125,10 @@ public interface IMVCGridDefinition /// Indicated the authorization type. Anonymous access is the default. /// AuthorizationType AuthorizationType { get; set; } + + /// + /// Changes method used to update grid data to post. It disables browser history feature. + /// + bool UsePost { get; set; } } } diff --git a/MVCGrid/Models/GridDefaults.cs b/MVCGrid/Models/GridDefaults.cs index e4d351c..39566f8 100644 --- a/MVCGrid/Models/GridDefaults.cs +++ b/MVCGrid/Models/GridDefaults.cs @@ -33,6 +33,7 @@ public GridDefaults() AllowChangingPageSize = false; MaxItemsPerPage = null; AuthorizationType = Models.AuthorizationType.AllowAnonymous; + UsePost = false; } public bool PreloadData { get; set; } @@ -75,5 +76,6 @@ public T GetAdditionalSetting(string name, T defaultValue) } public AuthorizationType AuthorizationType { get; set; } + public bool UsePost { get; set; } } } diff --git a/MVCGrid/Models/GridDefinition.cs b/MVCGrid/Models/GridDefinition.cs index c858e6f..ba6c888 100644 --- a/MVCGrid/Models/GridDefinition.cs +++ b/MVCGrid/Models/GridDefinition.cs @@ -51,6 +51,7 @@ public GridDefinition(GridDefaults gridDefaults) this.AllowChangingPageSize = gridDefaults.AllowChangingPageSize; this.MaxItemsPerPage = gridDefaults.MaxItemsPerPage; this.AuthorizationType = gridDefaults.AuthorizationType; + this.UsePost = gridDefaults.UsePost; } public IEnumerable GetColumns() @@ -301,6 +302,11 @@ public T GetAdditionalSetting(string name, T defaultValue) /// Indicated the authorization type. Anonymous access is the default. /// public AuthorizationType AuthorizationType { get; set; } + + /// + /// Changes method used to update grid data to post. It disables browser history feature. + /// + public bool UsePost { get; set; } } } diff --git a/MVCGrid/Models/MVCGridBuilder.cs b/MVCGrid/Models/MVCGridBuilder.cs index 8ab294e..cfcdbfe 100644 --- a/MVCGrid/Models/MVCGridBuilder.cs +++ b/MVCGrid/Models/MVCGridBuilder.cs @@ -378,5 +378,14 @@ public MVCGridBuilder WithAuthorizationType(AuthorizationType authType) GridDefinition.AuthorizationType = authType; return this; } + + /// + /// Changes method used to update grid data to post. It disables browser history feature. + /// + public MVCGridBuilder WithUsePost(bool usesPost) + { + GridDefinition.UsePost = usesPost; + return this; + } } } diff --git a/MVCGrid/Scripts/MVCGrid.js b/MVCGrid/Scripts/MVCGrid.js index 4090298..6fce96e 100644 --- a/MVCGrid/Scripts/MVCGrid.js +++ b/MVCGrid/Scripts/MVCGrid.js @@ -14,9 +14,10 @@ var MVCGrid = new function () { var jsonData = $('#' + 'MVCGrid_' + mvcGridName + '_JsonData').html(); - currentGrids.push( - $.parseJSON(jsonData) - ); + var grid = $.parseJSON(jsonData); + grid.data = {}; + + currentGrids.push(grid); }); for (var i = 0; i < currentGrids.length; i++) { @@ -269,13 +270,18 @@ var MVCGrid = new function () { } }); - var newUrl = window.location.href; + if (gridDef.usePost) { + gridDef.data[gridDef.qsPrefix + 'cols'] = colString; + MVCGrid.reloadGrid(mvcGridName); + } else { + var newUrl = window.location.href; - $.each(obj, function (k, v) { - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'cols', colString); - }); + $.each(obj, function(k, v) { + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'cols', colString); + }); - setURLAndReload(mvcGridName, newUrl); + setURLAndReload(mvcGridName, newUrl); + } }; // public @@ -289,13 +295,20 @@ var MVCGrid = new function () { var gridDef = findGridDef(mvcGridName); - var newUrl = window.location.href; + if (gridDef.usePost) { + $.each(obj, function (k, v) { + gridDef.data[gridDef.qsPrefix + k] = v; + }); + MVCGrid.reloadGrid(mvcGridName); + } else { + var newUrl = window.location.href; - $.each(obj, function (k, v) { - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + k, v); - }); + $.each(obj, function (k, v) { + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + k, v); + }); - setURLAndReload(mvcGridName, newUrl); + setURLAndReload(mvcGridName, newUrl); + } }; // public @@ -315,12 +328,17 @@ var MVCGrid = new function () { var gridDef = findGridDef(mvcGridName); - var newUrl = window.location.href; - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'sort', sortColumn); - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'dir', sortDirection); + if (gridDef.usePost) { + gridDef.data[gridDef.qsPrefix + 'sort'] = sortColumn; + gridDef.data[gridDef.qsPrefix + 'dir'] = sortDirection; + MVCGrid.reloadGrid(mvcGridName); + } else { + var newUrl = window.location.href; + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'sort', sortColumn); + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'dir', sortDirection); - - setURLAndReload(mvcGridName, newUrl); + setURLAndReload(mvcGridName, newUrl); + } }; // public @@ -333,10 +351,16 @@ var MVCGrid = new function () { this.setPage = function (mvcGridName, pageNumber) { var gridDef = findGridDef(mvcGridName); - - var newUrl = window.location.href; - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'page', pageNumber); - setURLAndReload(mvcGridName, newUrl); + + if (gridDef.usePost) { + gridDef.data[gridDef.qsPrefix + 'page'] = pageNumber; + MVCGrid.reloadGrid(mvcGridName); + } + else { + var newUrl = window.location.href; + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'page', pageNumber); + setURLAndReload(mvcGridName, newUrl); + } }; // public @@ -349,10 +373,16 @@ var MVCGrid = new function () { this.setPageSize = function (mvcGridName, pageSize) { var gridDef = findGridDef(mvcGridName); - - var newUrl = window.location.href; - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'pagesize', pageSize); - setURLAndReload(mvcGridName, newUrl); + + if (gridDef.usePost) { + gridDef.data[gridDef.qsPrefix + 'pagesize'] = pageSize; + MVCGrid.reloadGrid(mvcGridName); + } + else { + var newUrl = window.location.href; + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + 'pagesize', pageSize); + setURLAndReload(mvcGridName, newUrl); + } }; // public @@ -365,14 +395,22 @@ var MVCGrid = new function () { this.setAdditionalQueryOptions = function (mvcGridName, obj) { var gridDef = findGridDef(mvcGridName); + + if (gridDef.usePost) { + $.each(obj, function (k, v) { + gridDef.data[gridDef.qsPrefix + k] = v; + }); + MVCGrid.reloadGrid(mvcGridName); + } + else { + var newUrl = window.location.href; - var newUrl = window.location.href; - - $.each(obj, function (k, v) { - newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + k, v); - }); + $.each(obj, function (k, v) { + newUrl = updateURLParameter(newUrl, gridDef.qsPrefix + k, v); + }); - setURLAndReload(mvcGridName, newUrl); + setURLAndReload(mvcGridName, newUrl); + } }; // private @@ -407,13 +445,19 @@ var MVCGrid = new function () { $.each(gridDef.pageParameters, function (k, v) { var thisPP = "_pp_" + gridDef.qsPrefix + k; - fullAjaxUrl = updateURLParameter(fullAjaxUrl, thisPP, v); + if(gridDef.usePost) + gridDef.data[thisPP] = v; + else + fullAjaxUrl = updateURLParameter(fullAjaxUrl, thisPP, v); }); + if (gridDef.usePost) + gridDef.data['Name'] = mvcGridName; + $.ajax({ - type: "GET", - url: fullAjaxUrl, - data: { 'Name': mvcGridName }, + type: gridDef.usePost ? "POST" : "GET", + url: gridDef.usePost ? ajaxBaseUrl : fullAjaxUrl, + data: gridDef.usePost ? gridDef.data : { 'Name': mvcGridName }, cache: false, beforeSend: function () { if (gridDef.clientLoading != '') { diff --git a/MVCGrid/Web/MVCGridHtmlGenerator.cs b/MVCGrid/Web/MVCGridHtmlGenerator.cs index 20ea0f5..7bbc456 100644 --- a/MVCGrid/Web/MVCGridHtmlGenerator.cs +++ b/MVCGrid/Web/MVCGridHtmlGenerator.cs @@ -225,6 +225,9 @@ private static string GenerateClientDefinitionJson(string gridName, IMVCGridDefi sbJson.Append(","); sbJson.AppendFormat("\"renderingMode\": \"{0}\"", def.RenderingMode.ToString().ToLower()); + sbJson.Append(","); + sbJson.AppendFormat("\"usePost\": {0}", def.UsePost.ToString().ToLower()); + sbJson.Append(","); sbJson.Append("\"pageParameters\": {"); sbJson.Append(GenerateJsonPageParameters(pageParameters)); diff --git a/MVCGrid/Web/QueryStringParser.cs b/MVCGrid/Web/QueryStringParser.cs index 7a8c3dc..5f8f4c6 100644 --- a/MVCGrid/Web/QueryStringParser.cs +++ b/MVCGrid/Web/QueryStringParser.cs @@ -32,9 +32,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt var options = new QueryOptions(); - if (httpRequest.QueryString[qsKeyEngine] != null) + if (httpRequest[qsKeyEngine] != null) { - string re = httpRequest.QueryString[qsKeyEngine]; + string re = httpRequest[qsKeyEngine]; options.RenderingEngineName = re; } @@ -49,10 +49,10 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt if (grid.AllowChangingPageSize) { - if (httpRequest.QueryString[qsKeyPageSize] != null) + if (httpRequest[qsKeyPageSize] != null) { int pageSize; - if (Int32.TryParse(httpRequest.QueryString[qsKeyPageSize], out pageSize)) + if (Int32.TryParse(httpRequest[qsKeyPageSize], out pageSize)) { options.ItemsPerPage = pageSize; } @@ -70,10 +70,10 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt } options.PageIndex = 0; - if (httpRequest.QueryString[qsKeyPage] != null) + if (httpRequest[qsKeyPage] != null) { int pageNum; - if (Int32.TryParse(httpRequest.QueryString[qsKeyPage], out pageNum)) + if (Int32.TryParse(httpRequest[qsKeyPage], out pageNum)) { options.PageIndex = pageNum - 1; if (options.PageIndex < 0) options.PageIndex = 0; @@ -93,9 +93,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt { string qsKey = grid.QueryStringPrefix + col.ColumnName; - if (httpRequest.QueryString[qsKey] != null) + if (httpRequest[qsKey] != null) { - string filterValue = httpRequest.QueryString[qsKey]; + string filterValue = httpRequest[qsKey]; if (!String.IsNullOrWhiteSpace(filterValue)) { @@ -116,9 +116,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt options.SortColumnName = null; string sortColName = null; - if (httpRequest.QueryString[qsKeySort] != null) + if (httpRequest[qsKeySort] != null) { - sortColName = httpRequest.QueryString[qsKeySort]; + sortColName = httpRequest[qsKeySort]; } if (String.IsNullOrWhiteSpace(sortColName)) @@ -146,9 +146,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt options.SortDirection = grid.DefaultSortDirection; - if (httpRequest.QueryString[qsKeyDirection] != null) + if (httpRequest[qsKeyDirection] != null) { - string sortDir = httpRequest.QueryString[qsKeyDirection]; + string sortDir = httpRequest[qsKeyDirection]; if (String.Compare(sortDir, "dsc", true) == 0) { options.SortDirection = SortDirection.Dsc; @@ -167,9 +167,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt string qsKeyAQO = grid.QueryStringPrefix + aqon; string val = ""; - if (httpRequest.QueryString[qsKeyAQO] != null) + if (httpRequest[qsKeyAQO] != null) { - val = httpRequest.QueryString[qsKeyAQO]; + val = httpRequest[qsKeyAQO]; } options.AdditionalQueryOptions.Add(aqon, val); @@ -183,9 +183,9 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt string qsKeyAQO = QueryStringPrefix_PageParameter + grid.QueryStringPrefix + aqon; string val = ""; - if (httpRequest.QueryString[qsKeyAQO] != null) + if (httpRequest[qsKeyAQO] != null) { - val = httpRequest.QueryString[qsKeyAQO]; + val = httpRequest[qsKeyAQO]; } options.PageParameters.Add(aqon, val); @@ -195,7 +195,7 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt var gridColumns = grid.GetColumns(); List requestedColumns = new List(); - if (httpRequest.QueryString[qsColumns] == null) + if (httpRequest[qsColumns] == null) { foreach (var gridColumn in gridColumns) { @@ -208,7 +208,7 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt } else { - string cols = httpRequest.QueryString[qsColumns]; + string cols = httpRequest[qsColumns]; string[] colParts = cols.Split(',', ';');