I'm trying to center n number of columns in a table that has DataTables receiving the parameter dynamically but it doesn't work.
I have tried with the following code, but I need the value to be dynamic, and the columns to be centered from the last to the first according to the number I send:
function makerDatatable(number){
$('#myTable').dataTable({
"columnDefs": [
{"className": "text-center", "targets": number}
]
}
}
Only the column with the specific number you receive is centered.
The DataTables documentation is not that clear and is usually out of date, I recently had the same request and the process was as follows:
By default the function will center the last two columns if no parameter is received, otherwise it centers the indicated amount.
Hopefully it's what you're looking for.
Do not complicate yourself. You can solve them only with css.
A well thought out css rule will not break your table.
this does the magic (className: "text-center")