Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to reload grid using Post method #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MVCGrid/Interfaces/IMVCGridDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,10 @@ public interface IMVCGridDefinition
/// Indicated the authorization type. Anonymous access is the default.
/// </summary>
AuthorizationType AuthorizationType { get; set; }

/// <summary>
/// Changes method used to update grid data to post. It disables browser history feature.
/// </summary>
bool UsePost { get; set; }
}
}
2 changes: 2 additions & 0 deletions MVCGrid/Models/GridDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public GridDefaults()
AllowChangingPageSize = false;
MaxItemsPerPage = null;
AuthorizationType = Models.AuthorizationType.AllowAnonymous;
UsePost = false;
}

public bool PreloadData { get; set; }
Expand Down Expand Up @@ -75,5 +76,6 @@ public T GetAdditionalSetting<T>(string name, T defaultValue)
}

public AuthorizationType AuthorizationType { get; set; }
public bool UsePost { get; set; }
}
}
6 changes: 6 additions & 0 deletions MVCGrid/Models/GridDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IMVCGridColumn> GetColumns()
Expand Down Expand Up @@ -301,6 +302,11 @@ public T GetAdditionalSetting<T>(string name, T defaultValue)
/// Indicated the authorization type. Anonymous access is the default.
/// </summary>
public AuthorizationType AuthorizationType { get; set; }

/// <summary>
/// Changes method used to update grid data to post. It disables browser history feature.
/// </summary>
public bool UsePost { get; set; }
}

}
9 changes: 9 additions & 0 deletions MVCGrid/Models/MVCGridBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,14 @@ public MVCGridBuilder<T1> WithAuthorizationType(AuthorizationType authType)
GridDefinition.AuthorizationType = authType;
return this;
}

/// <summary>
/// Changes method used to update grid data to post. It disables browser history feature.
/// </summary>
public MVCGridBuilder<T1> WithUsePost(bool usesPost)
{
GridDefinition.UsePost = usesPost;
return this;
}
}
}
116 changes: 80 additions & 36 deletions MVCGrid/Scripts/MVCGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 != '') {
Expand Down
3 changes: 3 additions & 0 deletions MVCGrid/Web/MVCGridHtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
36 changes: 18 additions & 18 deletions MVCGrid/Web/QueryStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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))
{
Expand All @@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -195,7 +195,7 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt

var gridColumns = grid.GetColumns();
List<ColumnVisibility> requestedColumns = new List<ColumnVisibility>();
if (httpRequest.QueryString[qsColumns] == null)
if (httpRequest[qsColumns] == null)
{
foreach (var gridColumn in gridColumns)
{
Expand All @@ -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(',', ';');

Expand Down