Skip to content

Commit

Permalink
Merge pull request #24 from AlexanderKrutov/development
Browse files Browse the repository at this point in the history
Merge 1.7 version from development
  • Loading branch information
AlexanderKrutov authored Aug 27, 2018
2 parents 80398b1 + 2ae94e3 commit 9c6521f
Show file tree
Hide file tree
Showing 27 changed files with 738 additions and 164 deletions.
Binary file removed .nuget/NuGet.exe
Binary file not shown.
File renamed without changes.
Binary file added .utils/NuGet.exe
Binary file not shown.
File renamed without changes.
Binary file added .utils/vswhere.exe
Binary file not shown.
49 changes: 41 additions & 8 deletions DataTables.Queryable.Samples/Controllers/DataTablesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

using DataTables.Queryable.Samples.Database;
using DataTables.Queryable.Samples.Models;
using System.Data.Entity.SqlServer;
using System.Linq.Expressions;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity.SqlServerCompact;
using System.Threading.Tasks;

namespace DataTables.Queryable.Samples.Controllers
{
Expand All @@ -19,7 +26,7 @@ public JsonResult Sample1()
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office.Address").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}

Expand All @@ -32,17 +39,15 @@ public JsonResult Sample2()

request.Columns[p => p.Name]
.GlobalSearchPredicate = (p) => p.Name.StartsWith(request.GlobalSearchValue);

request.Columns[p => p.Position]
.GlobalSearchPredicate = (p) => p.Position.StartsWith(request.GlobalSearchValue);

request.Columns[p => p.Office.Name]
.GlobalSearchPredicate = (p) => p.Office.Name.StartsWith(request.GlobalSearchValue);

using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}

Expand All @@ -60,27 +65,55 @@ public JsonResult Sample3()

// define custom predicate to filter results:
// get all persons hired in period between "dateFrom" and "dateTo"
request.CustomFilterPredicate =
request.CustomFilterPredicate =
(s) => s.StartDate >= dateFrom && s.StartDate <= dateTo;

using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}

/// <summary>
/// AJAX POST
/// </summary>
public JsonResult Sample4(DataTablesAjaxPostModel model)
{
var request = new DataTablesRequest<Person>(model);
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office.Address").ToPagedList(request);
return JsonDataTable(persons, request.Draw);
}
}

/// <summary>
/// Async processing
/// </summary>
public async Task<JsonResult> Sample5()
{
var request = new DataTablesRequest<Person>(Request.QueryString);
using (var ctx = new DatabaseContext())
{
var persons = await ctx.Persons.Include("Office.Address").ToPagedListAsync(request);
return JsonDataTable(persons, request.Draw);
}
}

/// <summary>
/// Helper method that converts <see cref="IPagedList{T}"/> collection to the JSON-serialized object in datatables-friendly format.
/// </summary>
/// <param name="model"><see cref="IPagedList{T}"/> collection of items</param>
/// <param name="draw">Draw counter (optional).</param>
/// <returns>JsonNetResult instance to be sent to datatables</returns>
protected JsonNetResult JsonDataTable<T>(IPagedList<T> model)
protected JsonNetResult JsonDataTable<T>(IPagedList<T> model, int draw = 0)
{
JsonNetResult jsonResult = new JsonNetResult();
jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
jsonResult.Data = new
{
draw = draw,
recordsTotal = model.TotalCount,
recordsFiltered = model.TotalCount,
data = model
Expand Down Expand Up @@ -111,5 +144,5 @@ public override void ExecuteResult(ControllerContext context)
response.Write(serializedObject);
}
}
}
}
}
10 changes: 10 additions & 0 deletions DataTables.Queryable.Samples/Controllers/WebUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@ public ActionResult Sample3()
{
return View();
}

public ActionResult Sample4()
{
return View();
}

public ActionResult Sample5()
{
return View();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@
<ItemGroup>
<Content Include="Views\web.config" />
<Content Include="Views\_Layout.cshtml" />
<Content Include="Views\Sample4.cshtml" />
<Content Include="Views\Sample5.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand Down
4 changes: 4 additions & 0 deletions DataTables.Queryable.Samples/Database/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;

using DataTables.Queryable.Samples.Models;
using System.Diagnostics;

namespace DataTables.Queryable.Samples.Database
{
Expand All @@ -13,6 +14,9 @@ public class DatabaseContext : DbContext

public DatabaseContext() : base()
{
#if TRACE
this.Database.Log = (s) => Trace.WriteLine($"DataTables.Queryable SQL log:\n {s}");
#endif
System.Data.Entity.Database.SetInitializer(new DatabaseInitializer());
}

Expand Down
4 changes: 2 additions & 2 deletions DataTables.Queryable.Samples/Views/Sample1.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public JsonResult Sample1()
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office.Address").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}
</code>
Expand All @@ -79,7 +79,7 @@ public JsonResult Sample1()
order: [[0, "asc"]],
pageLength: 10,
ajax: {
url: '/DataTables/Sample1',
url: '/DataTables/Sample1'
},
columnDefs: [
{ targets: 0, data: 'Name' },
Expand Down
2 changes: 1 addition & 1 deletion DataTables.Queryable.Samples/Views/Sample2.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public JsonResult Sample2()
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}
</code>
Expand Down
2 changes: 1 addition & 1 deletion DataTables.Queryable.Samples/Views/Sample3.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public JsonResult Sample3()
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office").ToPagedList(request);
return JsonDataTable(persons);
return JsonDataTable(persons, request.Draw);
}
}
</code>
Expand Down
121 changes: 121 additions & 0 deletions DataTables.Queryable.Samples/Views/Sample4.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
@{
Layout = "_Layout.cshtml";
ViewBag.Title = "Sample 4: AJAX POST";
}

<h3>@ViewBag.Title</h3>
<p>This page demonstrates basic usage of <samp>DataTables.Queryable</samp> library with all features enabled using an AJAX POST:</p>
<ul>
<li>Global search by all columns with default predicate <code>String.Contains(...)</code></li>
<li>Individual column search with default predicate <code>String.Contains(...)</code></li>
<li>Binding to and search/order by nested properties (<samp>Office</samp>, <samp>Address</samp>)</li>
<li>Sorting by all column with multisort support</li>
<li>Pagination</li>
<li>AJAX POST</li>
</ul>
<p>Tips:</p>
<ul>
<li>Check the Visual Studio output view (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>O</kbd>) to see how the datatables-to-server request is translated to LINQ and Entity Framework query.</li>
<li>Try multi-column ordering with pressed <kbd>Shift</kbd> and click on a column.</li>
</ul>
<br />

<table id="table" class="table table-condensed table-bordered table-vcentered dataTable">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Address</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Address</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>

<br />

<p>Server-side code:</p>

<pre>
<code class="csharp">
// DataTablesController.cs:

public JsonResult Sample4(DataTablesAjaxPostModel model)
{
var request = new DataTablesRequest&lt;Person&gt;(model);
using (var ctx = new DatabaseContext())
{
var persons = ctx.Persons.Include("Office.Address").ToPagedList(request);
return JsonDataTable(persons, request.Draw);
}
}
</code>
</pre>

<script type="text/javascript">
$(function () {
$('#table tfoot th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" class="form-control" style="width: 100%" placeholder="Search ' + title + '" />');
});
var table = $('#table').DataTable({
autoWidth: false,
processing: true,
serverSide: true,
order: [[0, "asc"]],
pageLength: 10,
ajax: {
url: '/DataTables/Sample4',
type: 'POST'
},
columnDefs: [
{ targets: 0, data: 'Name' },
{ targets: 1, data: 'Position' },
{ targets: 2, data: 'Office.Name' },
{
targets: 3, data: 'Office.Address.Street',
render: function (street, type, person, meta) { return person.Office.Address.Street + ", " + person.Office.Address.Building; }
},
{ targets: 4, data: 'Extn' },
{
targets: 5, data: 'StartDate',
render: function (data) { return Date.parse(data).toString('MMMM dd, yyyy'); }
},
{
targets: 6, data: 'Salary'
},
]
});
// Apply the search
table.columns().every(function () {
var that = this;
$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
hljs.initHighlightingOnLoad();
});
</script>

Loading

0 comments on commit 9c6521f

Please sign in to comment.