Skip to content

Commit 6c2091f

Browse files
committed
Support aoColumnDefs
This change emits aoColumnDefs based on the view model that we are rendering. For example: 1. [ScaffoldColumn(false) will apply bVisible: false to the column 2. [DisplayName("Title")] will apply sTitle: Title to the column 3. The sName property will always be set to allow for a more flexible client and server side decoupling I also changed the sample to demonstrate this, and clean up a few small issues like making files look in the virtual path instead of an absolute top-level path.
1 parent 938d39a commit 6c2091f

File tree

14 files changed

+1485
-1383
lines changed

14 files changed

+1485
-1383
lines changed
Lines changed: 109 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,110 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.Linq;
5-
using System.Web;
6-
using System.Web.Mvc;
7-
8-
namespace Mvc.JQuery.Datatables.Example.Controllers
9-
{
10-
public class HomeController : Controller
11-
{
12-
private static List<User> _users;
13-
//
14-
// GET: /Home/
15-
16-
public ActionResult Index()
17-
{
18-
return View();
19-
}
20-
21-
public enum PositionTypes
22-
{
23-
Engineer,
24-
Tester,
25-
Manager
26-
}
27-
28-
public DataTablesResult<UserView> GetUsers(DataTablesParam dataTableParam)
29-
{
30-
var users = Users().AsQueryable();
31-
32-
return DataTablesResult.Create(users, dataTableParam, user => new UserView()
33-
{
34-
Id = user.Id,
35-
Name = new MvcHtmlString("<b>" + user.Name +"</b>"),
36-
Email = user.Email,
37-
Position = user.Position == null ? "" : user.Position.ToString(),
38-
Number = user.Number,
39-
Hired = user.Hired
40-
});
41-
}
42-
public DataTablesResult GetUsersUntyped(DataTablesParam dataTableParam)
43-
{
44-
var users = Users();
45-
46-
return DataTablesResult.Create(users, dataTableParam);
47-
}
48-
49-
private static List<User> Users()
50-
{
51-
var r = new Random();
52-
var domains = "gmail.com,yahoo.com,hotmail.com".Split(',').ToArray();
53-
54-
var positions = new List<PositionTypes?> { null, PositionTypes.Engineer, PositionTypes.Tester, PositionTypes.Manager };
55-
return _users ?? (_users = new List<User>
56-
(
57-
Enumerable.Range(1, 100).Select(i =>
58-
new User()
59-
{
60-
Id = i,
61-
Email = "user" + i + "@" + domains[i%domains.Length],
62-
Name = r.Next(6) == 3 ? null : "User" + i,
63-
Position = positions[i%positions.Count],
64-
Number = (Numbers) r.Next(4),
65-
Hired = DateTimeOffset.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble())
66-
})
67-
));
68-
}
69-
}
70-
71-
public enum Numbers
72-
{
73-
Zero,
74-
One,
75-
Two,
76-
Three,
77-
Four
78-
}
79-
public class User
80-
{
81-
public int Id { get; set; }
82-
public string Name { get; set; }
83-
public string Email { get; set; }
84-
85-
public HomeController.PositionTypes? Position { get; set; }
86-
87-
public DateTimeOffset Hired { get; set; }
88-
89-
public Numbers Number { get; set; }
90-
}
91-
92-
public class UserView
93-
{
94-
public int Id { get; set; }
95-
public MvcHtmlString Name { get; set; }
96-
97-
public string Email { get; set; }
98-
99-
public string Position { get; set; }
100-
public DateTimeOffset Hired { get; set; }
101-
102-
public Numbers Number { get; set; }
103-
}
104-
105-
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.ComponentModel.DataAnnotations;
5+
using System.Globalization;
6+
using System.Linq;
7+
using System.Web;
8+
using System.Web.Mvc;
9+
10+
namespace Mvc.JQuery.Datatables.Example.Controllers
11+
{
12+
public class HomeController : Controller
13+
{
14+
private static List<User> _users;
15+
//
16+
// GET: /Home/
17+
18+
public ActionResult Index()
19+
{
20+
return View();
21+
}
22+
23+
public enum PositionTypes
24+
{
25+
Engineer,
26+
Tester,
27+
Manager
28+
}
29+
30+
public DataTablesResult<UserView> GetUsers(DataTablesParam dataTableParam)
31+
{
32+
var users = Users().AsQueryable();
33+
34+
return DataTablesResult.Create(users, dataTableParam, user => new UserView()
35+
{
36+
Id = user.Id,
37+
Name = new MvcHtmlString("<b>" + user.Name +"</b>"),
38+
Email = user.Email,
39+
Position = user.Position == null ? "" : user.Position.ToString(),
40+
Number = user.Number,
41+
Hired = user.Hired
42+
});
43+
}
44+
public DataTablesResult GetUsersUntyped(DataTablesParam dataTableParam)
45+
{
46+
var users = Users();
47+
48+
return DataTablesResult.Create(users, dataTableParam);
49+
}
50+
51+
private static List<User> Users()
52+
{
53+
var r = new Random();
54+
var domains = "gmail.com,yahoo.com,hotmail.com".Split(',').ToArray();
55+
56+
var positions = new List<PositionTypes?> { null, PositionTypes.Engineer, PositionTypes.Tester, PositionTypes.Manager };
57+
return _users ?? (_users = new List<User>
58+
(
59+
Enumerable.Range(1, 100).Select(i =>
60+
new User()
61+
{
62+
Id = i,
63+
Email = "user" + i + "@" + domains[i%domains.Length],
64+
Name = r.Next(6) == 3 ? null : "User" + i,
65+
Position = positions[i%positions.Count],
66+
Number = (Numbers) r.Next(4),
67+
Hired = DateTimeOffset.UtcNow.AddDays(-1 * 365 * 3 * r.NextDouble())
68+
})
69+
));
70+
}
71+
}
72+
73+
public enum Numbers
74+
{
75+
Zero,
76+
One,
77+
Two,
78+
Three,
79+
Four
80+
}
81+
public class User
82+
{
83+
public int Id { get; set; }
84+
public string Name { get; set; }
85+
public string Email { get; set; }
86+
87+
public HomeController.PositionTypes? Position { get; set; }
88+
89+
public DateTimeOffset Hired { get; set; }
90+
91+
public Numbers Number { get; set; }
92+
}
93+
94+
public class UserView
95+
{
96+
[ScaffoldColumn(false)]
97+
public int Id { get; set; }
98+
[DisplayName("UserName")]
99+
public MvcHtmlString Name { get; set; }
100+
101+
public string Email { get; set; }
102+
103+
public string Position { get; set; }
104+
public DateTimeOffset Hired { get; set; }
105+
106+
public Numbers Number { get; set; }
107+
}
108+
109+
106110
}

0 commit comments

Comments
 (0)