Updated with correct UI code - custom rendering using orthogonal data
I'm working with MVC C# within framework 4.5.1 and I'm having an issue creating custom sorting within a table utilizing jquery datatables 1.10.7. Bootstrap is used within the project too.
Everything was working as expected initially, but that changed when I decided to custom render a column to display an image. The image is rendering just fine, but sorting doesn't work at all for this new column. The JSON being returned as the datasource for this column is an object with 3 properties: Process.Id, Process.Description, and Process.ImageUrl. The sorting should work based on the Process.Id. The render just creates an image tag, and there's some logic in there to dynamically add some information to the title. The last render creates a button link to view more details.
Here's the datatable initialization in my view, which is called during doc ready:
UPDATE: columnDefs has been updated to render correctly when I need the custom sort value, or the image tag.
function LoadDataTable() {
table = $('#accountsRequests').DataTable(
{
"destroy": true,
"processing": true,
"ajax":
{
"type": "GET",
"url": "@Url.Action(MyActionName, MyControllerName)"
}
, "paging": true
, "columns": [
{ "data": "UserId" },
{ "data": "Process", title: "Status" },
{ "data": "LastName", "title": "Last Name" },
{ "data": "UserId" }
]
, "columnDefs": [
{
"targets": 0, // hiding first column, userId
"visible": false,
"searchable": false,
"sortable": false
},
{
"targets": 1,
"render": function (data, type, row) {
var element = "<img id='status_" + row["UserId"] + "' src='" + data["ImageUrl"] + "' data-order='" + data["Id"] + "' data-sort='" + data["Id"] + "' title='" + data["Description"] + "";
if (type === "sort" || type === 'type') {
return data["Id"];
}
else {
if (data["Id"] == "2") // add in-process name
{
var userId = row["ProcessUserId"].trim();
element = element + " by " + userId;
}
return element + "'/>";
} },
"searchable": false,
"sortable": true
},
{
"targets": 3,
"render": function (data, type, row) {
return "<a class='btn btn-default' role='button' href=\"@Url.Action(MyOtherAction, MyController)?userId=" + row["UserId"] + "\">View Details</a>"
},
"searchable": false,
"sortable": false
}
]
});
}
And this is what it looks like on the UI:
Here's the Process object definition:
I have attempted to dynamically add the data-sort and data-order HTML5 attributes into my render statement, but that doesn't seem to do anything. This adds these attributes to the image itself, which might not be valid for sorting.
I've also attempted to use the createCell callback function, shown below and that doesn't work at all, regardless what I put in there. In my example, as a quick test, I'm attempting to add something to ALL cells. And I get nothing.
"createdCell": function (cell, cellData, rowData, rowIndex, colIndex) {
$(cell).attr('data-userId', 'adsfadsasfas');}
Just to make sure that this is working as expected I inserted a createRow function just to make sure. I've worked with this before so I knew what it should look like when working. Works. Boooyah!
"createdRow": function (row, data, index) {
$(row).attr('data-userId', 'fadsafasdfdasfasfasa'); }
Here's the output showing that the createdRow function is working, but nothing for the createdCell.
So, what am I doing wrong, and how can I add the sorting property that I want (the Process.Id), regardless of what's actually displayed? Can I add a sort property to the Column definition? I've seen something similar but I'm not sure. Any help is appreciated! Please let me know if there's any clarification needed in the above explanations.



_:andsort:are supposed to go in therender:option, not thedata:option. Try that and see if anything changes.columnDefsandcolumnsare essentially the same thing; anything you can define incolumnDefscan also be defined incolumn. One issue (and the reason I'm commenting instead of answering) is that I've never tried to userenderas a function with orthogonal data, so I don't know if it is exactly the same syntax. If it helps, try moving all yourcolumnDefsstuff into thecolumnsdeclaration. See the documentation for these here and here.