Search code examples
jquerydatatablestimeago

Timeago and jQuery dataTables


Using datatables and timeago. Some dates are not showing on next pages when I click on the next page. But with this code it solved it.

$("#simpledatatable_paginate").click(function(){
    jQuery("abbr.timeago").timeago();
});

However, when I clicked on table headings sort or the show pages 10, 20, 25, 50, 100 etc and also search in datatables some data the data is not displayed. Is there any other way to solve this?


Solution

  • I suggest you take advantage of the draw.dt event. It is called every time the dataTables instance is redrawn, i.e when sorting, filtering, changing page etc. Create the handler before you initialize the dataTable (I assume your table id is #simpledatatable) :

    $("#simpledatatable").on('draw.dt', function() {
       jQuery("abbr.timeago").timeago();
    });
    
    $("#simpledatatable").DataTable({
       ...
    });
    

    Now you can skip any other timeago() calls. The problem is that jQuery("abbr.timeago").timeago(); only targets present or visible abbr.timeago elements, not those who are injected later on when you are sorting, changing page etc.