-
Notifications
You must be signed in to change notification settings - Fork 19
Requirements and limitations
Alexander Krutov edited this page Dec 1, 2016
·
8 revisions
There are few points that you should take in account when use DataTables.Queryable:
You should specify data
or/and name
for each column in columnDefs
section of datatables configuration:
// client-side code (JavaScript)
var table = $('#table').DataTable({
serverSide: true,
order: [[0, "asc"]],
pageLength: 10,
ajax: { url: '/DataTables/GetPersons' },
columnDefs: [
{ targets: 0, data: 'Name' },
{ targets: 1, data: 'Position' },
{ targets: 2, data: 'Office' },
{ targets: 3, data: 'Extn' },
...
]
});
Model type used on server side should have public properties with same names as data
or/and name
on client side:
// server-side code (C#)
public class Person
{
public string Name { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public int Extn { get; set; }
...
}
If you use custom filtering or modified search predicates features of DataTables.Queryable with Entity Framework, please remember that only limited number of expressions can be translated to SQL queries.
- Do not use custom C# functions in predicates:
private bool MyMatcherFunction(Person p, string textToMatch)
{
return p.Name.StartsWith(textToMatch);
}
...
// will not work!
request.Columns[p => p.Name]
.GlobalSearchPredicate = p => MyMatcherFunction(p, request.GlobalSearchValue);
// this is OK
request.Columns[p => p.Name]
.GlobalSearchPredicate = p => p.Name.StartsWith(request.GlobalSearchValue);
- Do not use complex type conversions
// will not work!
request.Columns[p => p.StartDate]
.GlobalSearchPredicate = (p) => p.StartDate.ToString("dd-MM-yyyy").StartsWith(request.GlobalSearchValue);
- Do not use regular expressions
// will not work!
var regex = new Regex(request.GlobalSearchValue);
request.Columns[p => p.Name]
.GlobalSearchPredicate = (p) => regex.IsMatch(p.Name);