I need to update an old project - it has its own backend and does deliver row data as an array which gets displayed as a standard html table.
The last attribute in the row array has an "editing column" by default, which means, it contains markup for an edit-icon like this:
$aData = array(
"first_name" => "John",
"last_name" => "Connor",
"edit_columns" => "<a href='#' class='edit'>Edit</a>"
);
The problem - there are also tables in my backend which will deliver several table cells (which works so far as it used to be a html table) with array data like this:
$aData = array(
"first_name" => "John",
"last_name" => "Connor",
"edit_columns" => "<a href='#' class='edit'>Edit</a> </td><td> <a href='#' class='delete'>Delete</a>"
);
The problem: the datatables plugin will treat edit_columns
as a single cell and filter the </td><td>
markup which results in 2 hyperlinks within one cell.
I know i would need to refactor my data but my customer wants to keep the backend data untouched. So - is there a way to "shift" the cell data as required?
Depending on the version of datatables you are using you could listen to the xhr.dt
-event with javascript and then modify/split the data before it gets rendered in your table.
myTable.on('xhr.dt', function (e, settings, json, xhr) {
let data = json.data;
for ( let i=0, dataLength=data.length; i<dataLength; i++ ) {
let edit_columns = data[i].edit_columns.split("</td><td>");
data[i].edit_column = edit_columns[0];
data[i].delete_column = edit_columns[1];
}
});
Now you can use the created data-columns (edit_column
and delete_column
) in your table like they are regular columns without modifying the code on the backend/serverside.