-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathHomeController.cs
119 lines (102 loc) · 4.84 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Microsoft.AspNetCore.Mvc;
using Mvc.JQuery.DataTables.Example.Domain;
using Mvc.JQuery.DataTables.Example.Helpers;
using Mvc.JQuery.DataTables.Models;
using Mvc.JQuery.DataTables.Serialization;
using System.Linq;
using System.Reflection;
namespace Mvc.JQuery.DataTables.Example.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
var getDataUrl = Url.Action(nameof(HomeController.GetUsers));
var vm = DataTablesHelper.DataTableVm<UserTableRowViewModel>("idForTableElement", getDataUrl);
//but you can also set them in a typed fashion:
vm.Filter = true; //Enable filtering
vm.ShowFilterInput = true; //Show or hide the search-across-all-columns input
//Use the advanced per-column filter plugin
vm.UseColumnFilterPlugin = true;
vm //Fluently configure, or use attributes on the ViewModel
.FilterOn("Position", new { sSelector = "#custom-filter-placeholder-position" }, new { sSearch = "Tester" }).Select("Engineer", "Tester", "Manager")
.FilterOn("Id").NumberRange()
.FilterOn("Salary", new { sSelector = "#custom-filter-placeholder-salary" }).NumberRange();
//You can set options on the datatable in an untyped way:
// vm.JsOptions.Add("iDisplayLength", 25);
// vm.JsOptions.Add("aLengthMenu", new object[] { new[] {5, 10, 25, 250, -1} , new object[] { 5, 10, 25, 250, "All"} });
vm.JsOptions.Add("fnCreatedRow", new Raw(@"function( nRow, aData, iDataIndex ) {
$(nRow).attr('data-id', aData[0]);
}"));
//Enable localstorage based saving filter/sort/paging state
vm.StateSave = true;
//you can change the page length options...
vm.LengthMenu = LengthMenuVm.Default(); // 5,10,25,50,All
vm.LengthMenu.RemoveAll(t => t.Item2 == 5);//Take out 5
vm.PageLength = 25; //... and set a default
//Enable column visibility
vm.ColVis = true;
vm.ShowVisibleColumnPicker = true; //Displays a control for showing/hiding columns
//Localizable
if (Request.Query["lang"] == "de")
{
//vm.Language = "{ 'sUrl': '" + Url.Content("~/Content/jquery.dataTables.lang.de-DE.txt") + "' }";
vm.Language = new Language
{
sProcessing = "Bitte warten...",
sLengthMenu = "_MENU_ Einträge anzeigen",
sZeroRecords = "Keine Einträge vorhanden.",
sInfo = "_START_ bis _END_ von _TOTAL_ Einträgen",
sInfoEmpty = "0 bis 0 von 0 Einträgen",
sInfoFiltered = "(gefiltert von _MAX_ Einträgen)",
sInfoPostFix = "",
sSearch = "Suchen",
sUrl = "",
oPaginate = new Paginate()
{
sFirst = "Erster",
sPrevious = "Zurück",
sNext = "Weiter",
sLast = "Letzter"
}
}.ToJsonString();
}
return View(vm);
}
public IActionResult JSUnitTests()
{
return View();
}
public DataTablesResult<UserTableRowViewModel> GetUsers([FromForm] DataTablesParam dataTableParam)
{
return DataTablesResult.Create(FakeDatabase.Users.Select(user => new UserTableRowViewModel()
{
Id = user.Id,
Name = user.Name,
Email = user.Email,
Position = user.Position == null ? "" : user.Position.ToString(),
Number = user.Number,
Hired = user.Hired,
IsAdmin = user.IsAdmin,
Salary = user.Salary,
Thumb = "https://randomuser.me/api/portraits/thumb/men/" + user.Id + ".jpg"
}), dataTableParam,
rowViewModel => new
{
Name = "<b>" + rowViewModel.Name + "</b>",
Hired =
rowViewModel.Hired == null
? "<pending>"
: rowViewModel.Hired.Value.ToString("d") + " " +
rowViewModel.Hired.Value.ToString("t") + " (" +
FriendlyDateHelper.GetPrettyDate(rowViewModel.Hired.Value) + ") ",
Thumb = "<img src='" + rowViewModel.Thumb + "' />"
});
}
public ContentResult EmbeddedResource()
{
var assembly = typeof(DataTableConfigVm).GetTypeInfo().Assembly;
return Content(string.Join("|", assembly.GetManifestResourceNames().Select(r => r)));
}
}
}