Skip to content

Commit

Permalink
fixed javascript encoding issue. added missing semicolons in javascri…
Browse files Browse the repository at this point in the history
…pt. added test page for issue #6
  • Loading branch information
Joe Harrison committed Mar 27, 2015
1 parent 29dc314 commit 6157afc
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 44 deletions.
26 changes: 13 additions & 13 deletions MVCGrid/Scripts/MVCGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var MVCGrid = new function () {
}

bindToolbarEvents();
}
};

var bindToolbarEvents = function (){

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -299,7 +299,7 @@ var MVCGrid = new function () {
});

setURLAndReload(mvcGridName, newUrl);
}
};

// private
var setURLAndReload = function (mvcGridName, newUrl) {
Expand Down Expand Up @@ -362,7 +362,7 @@ var MVCGrid = new function () {
}
}
});
}
};

// public
this.getExportUrl = function (mvcGridName) {
Expand All @@ -373,7 +373,7 @@ var MVCGrid = new function () {
exportUrl = updateURLParameter(exportUrl, 'Name', mvcGridName);

return exportUrl;
}
};
};


Expand Down
10 changes: 5 additions & 5 deletions MVCGrid/Web/MVCGridHtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(",");
Expand Down Expand Up @@ -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(",");
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion MVCGridExample/App_Start/MVCGridConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job>()
{
Expand Down
2 changes: 1 addition & 1 deletion MVCGridExample/Content/MVCGridConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job>()
{
Expand Down
75 changes: 75 additions & 0 deletions MVCGridExample/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -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<Job>()
.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("<a href='{Value}'>{Model.Contact.FullName}</a>").WithPlainTextValueExpression((p, c) => p.Contact != null ? p.Contact.FullName : "");

cols.Add("Delete")
.WithHtmlEncoding(false)
.WithSorting(false)
.WithFiltering(false)
.WithHeaderText("<input type='checkbox' id='chkselectall'>")
.WithValueExpression((p, c) => c.UrlHelper.Action("Save", "Country", new { area = "General", id = p.JobId }))
.WithValueTemplate("<input type='checkbox' class='select' value='{Model.JobId}'>")
.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<string>(), options.SortDirection == SortDirection.Dsc);

return new QueryResult<Job>()
{
Items = data,
TotalRecords = totalRecords
};
})
);
}
}
}
2 changes: 2 additions & 0 deletions MVCGridExample/MVCGridExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\ItemController.cs" />
<Compile Include="Controllers\SupportController.cs" />
<Compile Include="Controllers\TestController.cs" />
<Compile Include="Data\Person.cs">
<DependentUpon>SampleDB.tt</DependentUpon>
</Compile>
Expand Down Expand Up @@ -299,6 +300,7 @@
<Content Include="Views\Support\Index.cshtml" />
<Content Include="Views\Demo\NestedObject.cshtml" />
<Content Include="Views\Shared\_MVCGridToolbar.cshtml" />
<Content Include="Views\Test\Issue6.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\Item\" />
Expand Down
53 changes: 29 additions & 24 deletions MVCGridExample/Models/JobRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class JobRepo
{
const string CacheKey = "JobRepo";

public IEnumerable<Job> GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
public IEnumerable<Job> GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
{
if (HttpContext.Current.Cache[CacheKey] == null)
{
Expand Down Expand Up @@ -37,35 +37,40 @@ public IEnumerable<Job> GetData(out int totalRecords, int? limitOffset, int? lim
}

List<Job> data = (List<Job>)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)
Expand Down
Loading

0 comments on commit 6157afc

Please sign in to comment.