datetime render on column not columnDefs
datetime render on column not columnDefs
tonyaldo
Posts: 13Questions: 2Answers: 0
Hi,
All examples in datetime.js suggest that rendering should be on columnDefs. Is this really the case ?
I am trying to combine datetime plugin with the example in Child rows (show extra / detailed information)
but see** no impact on** datetime format server-side is sending e.g 2017-09-03T06:42:29.871Z
there is no errors
Any tips?
Thanks
<script>
/* Formatting function for row details */
function format(d) {
var childTable = '<table class="table table-sm">';
// `d` is the original data object for the row
Object.getOwnPropertyNames(d.details).forEach(
function (val, idx, array) {
childTable = childTable + '<tr>' + '<td>' + val + '</td><td>' + d.details[val] + '</td></tr>';
}
);
childTable = childTable + '</table>';
console.log(childTable);
return childTable;
}
$(document).ready(function () {
var visitsAPIURL = '/api/visits/' + '<%= user.id %>' + '/' + '<%= QS.d %>' + '/?filter=none';
var table = $('#visitHistory').DataTable({
"ajax": {
"url": visitsAPIURL,
"dataSrc": ""
},
deferRender: true,
dom: 'Bfrtip',
columns: [
{
className: 'details-control',
orderable: false,
data: null,
defaultContent: ''
},
{
data: "details.dateTimeIn",
render: $.fn.dataTable.moment('Do MMM YYYYY')
},
{ data: "details.firstName" },
{ data: "details.lastName" },
{ data: "details.host" }
],
"order": [[1, 'desc']]
});
table.buttons().action(function (e, dt, button, config) {
console.log('Button ' + this.text() + ' activated');
this.disable();
});
// Add event listener for opening and closing details
$('#visitHistory tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
});
</script>
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Or
columns
. Any property that can be used on one can be used on the other (with the exception ofcolumnDefs.targets
).Can you give a link to the page in question or use the debugger please? The code above I think looks okay - as far as I can see initially anyway.
Allan
wow debugger is pretty cool! Here is the link, now I know I need to upgrade
https://debug.datatables.net/oxazur
I don't actually see a renderer being used on any of the columns in your table:
Allan
Isnt that strange ? Shouldnt the code I have suffice ? I ran the debug code against the table output for the same code in my original post
It is odd. Are you able to link to the page so I can see it directly?
Thanks,
Allan
Hmm, is this line really correct date formating?
render: $.fn.dataTable.moment('Do MMM YYYYY')
Actually no - that doesn't look correct. It should be in the
$.fn.dataTable.render
object - see here.Allan
This is the blog I followed https://datatables.net/blog/2014-12-18
I tried the example in
https://github.com/DataTables/Plugins/blob/master/dataRender/datetime.js
Now get
jQuery.Deferred exception: $.fn.dataTable.render.moment is not a function TypeError: $.fn.dataTable.render.moment is not a function
This is the order I load the JS in
https://code.jquery.com/jquery-3.2.1.min.js
https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js
https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.min.js
https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js
https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js
https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/dataTables.bootstrap4.min.js
https://cdn.datatables.net/buttons/1.4.1/js/dataTables.buttons.min.js
https://cdn.datatables.net/buttons/1.4.1/js/buttons.html5.min.js
https://cdn.datatables.net/buttons/1.4.1/js/buttons.print.min.js
https://cdn.datatables.net/plug-ins/1.10.16/sorting/datetime-moment.js
The blog post you linked to discusses an ordering plug-in.
The GitHub page you linked to is a data renderer.
They are different things. They work in different ways.
Which is it you want to use? If you could link to the page you are working with or show a sample of the data you need to use that would be helpful.
Allan
I tried to re-create it with a simpler example adapted from one of your examples
Here is the fiddle
https://jsfiddle.net/z163pj9o/1/
If I uncomment the render code as per example
I get:
jquery-3.2.1.min.js:2 jQuery.Deferred exception: $.fn.dataTable.render.moment is not a function TypeError: $.fn.dataTable.render.moment is not a function
Just use the render function on the data instead, see my updated fiddle.
https://jsfiddle.net/vL3yw0z2/
Just change the date format to whatever you need. If you just need the displayed data then check for if (type === 'display')....
Thanks a lot! This worked!
I suppose I dont even need the plugin now
Maybe not as I need to 'format' and 'order' the datetime (most recent first) !