Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  97] [ 2]  / answers: 1 / hits: 17735  / 9 Years ago, fri, december 4, 2015, 12:00:00

Basically I want to pull data from ajax into my columns, but I want the cells in the columns to have the data-order attribute on them with the value from the ajax call and use moment.js to format the data in the cell.



I'm assuming this is the best way to make it pretty AND orderable. I found the plugin for datetime-momentJS, but it will only order the date, not format it as well.



var dataTable = $('#products').DataTable( {
'processing': true,
'ajax': '/products',
'columns': [
{
'data': 'updated_at',
'className':'date'
}
]
});


right now I have this as the final result:



<td class=date>2015-11-08T11:00:00.000Z</td>


but the result I want is:



<td class=date data-order=2015-11-08T11:00:00.000Z>11/08/2015</td>


Can I use the render option to do this somehow?



The moment code to format it like I want would be moment('2015-11-08T11:00:00.000Z').format('DD/MM/YY').


More From » jquery

 Answers
11

You can use createdRow callback in order to apply any custom logics after row creation:



$('#products').dataTable({
/* */
'createdRow': function(row, data, dataIndex) {
var $dateCell = $(row).find('td:eq(0)'); // get first column
var dateOrder = $dateCell.text(); // get the ISO date
$dateCell
.data('order', dateOrder) // set it to data-order
.text(moment(dateOrder).format('DD/MM/YY')); // and set the formatted text
}
});


Note that td:eq(0) selector assumes that the column with date is the first column. You need to change 0 to another value, if it's not.


[#64171] Wednesday, December 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
3 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;