I am working on developing a dynamic DataTable in which the data source is dynamically generated with [n] columns, where the data source may contain 4 columns or 6 columns as an example. As per this post on the DataTables forum (How to Render Columns from a dynamic data source) it is possible to define and build a variable of a column defintions, and pass this to the initialisation.
Based on this I have created the following code that works well for the given purpose and renders the columns dynaically based on the data source.
HTML:
<div class="data-table">
<table id="dataTable" class="table table-striped table-bordered" style="width:100%;"></table>
</div>
Script:
<script>
var dataToSend = {};
var dataTable;
dataToSend = {
"regionId": @Model.RegionId
}
// Get Column Defintions
var dtColumns = @Html.Raw(Json.`enter code here`Serialize(listOfDefintions));
// DataTable
$(function () {
dataTable = {
dt: null,
init: function () {
dt = $("#dataTable").DataTable({
ajax: {
type: "GET",
url: "@Url.Action("GetDataForGrid", "Contact")",
data: function () {
return dataToSend;
},
datatype: "json",
dataSrc: ""
},
columns: dtColumns,
lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
});
},
refresh: function () {
dt.ajax.reload();
}
}
dataTable.init();
});
</script>
The above code renders the following DataTable, based on the data source which can be seen below.
My question regards the empty final column of the table, in which I need to render a button. I would have normally written something along the lines of the code below, if I had been wanting to render a table with known column values, in which I would use the DataTable property column.Render. As you can see below, using the data property of the column, I can use create a function to render a button, which can be used to quick navigate to the Contact/Edit page passing the "id" as a Url route value, meaning that it is possible to edit the specific details of the choosen contact.
<script>
var dataToSend = {};
var contactDataTable;
dataToSend = {
"regionId": @Model.RegionId
};
$(function () {
contactDataTable = {
dt: null,
init: function () {
dt = $('#contact-data-table').DataTable({
ajax: {
type: "GET",
url: "@Url.Action("GetDataForGrid", "Contact")",
data: function () {
return dataToSend;
},
datatype: "json",
dataSrc: ""
},
columns: [
{
"title": "Forename",
"data": "forename",
"searchable": true,
},
{
"title": "Surname",
"data": "surname",
"searchable": true,
},
{
"title": "Email",
"data": "email",
"searchable": true
},
{
"Status":
"status":
"searchable":
},
{
"title": "Role",
"data": "roleName",
"searchable": true
},
{
"title": "",
"data": "id",
"searchable": false,
"sortable": false,
"render": function (data, type, full, meta) {
var buffer = '<a href="@Url.Action("Edit","Contact")/' + data + '" class="btn btn-sm btn-primary js-action"><i class="far fa-edit"></i></a> '
buffer += '<a href="@Url.Action("Delete", "Contact")/' + data + '" class="btn btn-sm btn-danger js-action"><i class="far fa-trash-alt"></i></a>';
return buffer;
}
}
],
columnDefs: [
{ "width": "15%", "targets": 0 },
{ "width": "15%", "targets": 1 },
{ "width": "20%", "targets": 2 },
{ "width": "15%", "targets": 3 },
{ "width": "15%", "targets": 4 },
{ "width": "20%", "targets": 5 },
],
lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
});
},
refresh: function () {
dt.ajax.reload();
}
}
</script>
How can I achieve the same principle of rendering a button in my table if I am defining and building my column defintion variable before passing this to my DataTable intialization?
Any help would be great.



dataSrc: "", use a function:dataSrc: function ( json ) { ... }. You can see an example here. This allows you to iterate over your source JSON data, and add a new object to each row - where the object is a string containing the HTML for the button you want to build. This is the equivalent to the logic you wanted to use in your column renderer.