DataTables.Queryable
DataTables.Queryable copied to clipboard
I think `name` should try to be parsed BEFORE `data`. Here is why:
https://github.com/AlexanderKrutov/DataTables.Queryable/blob/master/DataTables.Queryable/DataTablesRequest.cs#L173-L199
Because of the way DataTables.Queryable (DTQ) works with EF models directly, we usually AutoMapper back these results. This can lead to the return ViewModel being very different than the Entity.
Right now, jQuery DataTables uses columnDefs.data to try and traverse the actual data it gets back from the server. But name is different.
I want to be able to do:
{
columnDefs:
[
{
name: "Entity.ActualNavigationProperty"
data: "ReturnViewModel.Property"
}
]
}
Hopefully this is clear. name
is used so when DTQ tries to traverse the actual properties to search and configure custom column searchs and all that and then data
is used by jQuery DT to traverse the returned viewmodel.
I want to add that I know I can use all sorts of manual overrides of columnDefs.data and datatables.ejax (jQuery ajax). However, I do not like how my callers need to know about the EF entity!
For example, this is currently a really dirty "fix"
DataTables Options
ajax: {
url: editorRestURL,
type: "POST",
contentType: "application/json",
data: function (d) {
// Do not mutate d
var payload = $.extend({}, d);
for (var i = 0; i < payload.columns.length; i++) {
var column = payload.columns[i];
//...
// Handle when column is an object with an `entity` property
if (column.data && column.data.hasOwnProperty('entity')) {
column.data = column.data.entity;
}
}
return JSON.stringify(payload);
}
},
columnDefs: [
{
"targets": "InventoryServerInventoryServerWarrantyLines",
"data": {
"_": "InventoryServers",
"entity": "InventoryServerInventoryServerWarrantyLines"
},
"createdCell": function (td, cellData, rowData, row, col) {
$(td).html(rowData.InventoryServers
.map(function (server) {
return `${server.Name} (${server.SerialNumber})`;
})
.join(`,<br />`));
}
}
]
I have to add my own entity
property to columnDefs.data
so I can make sure the ajax call submits a property the server understands (because DTQ makes me configure searches against entity properties only) and for everything else I use the built in "_"
so jQuery DT can use the property on the JSON (the ViewModel)
Its been difficult to have the clients be aware of the EF entites for requests and then our domain objects (viewmodels) for responses. They should not hve to be aware of the entities.
I think it's better to add some configuration property to DataTablesRequest
which will indicate processing order for the data
and name
parameters.
Something like this:
public enum PropertyNameProcessingOrder
{
DataAndName, // this is default as now
NameAndData,
DataOnly,
NameOnly
}
@AlexanderKrutov That is a great idea.