My table date column desc shows correctly but can't sort properly, sort others date can't sort.

My table date column desc shows correctly but can't sort properly, sort others date can't sort.

sparklersparkler Posts: 9Questions: 2Answers: 0

Description of problem:
I have built the form using a WordPress shortcode and outputting by php. I can't do by ajax because it won't show the taxonomy or the if else in a table td.
So I have the published date column. It shows the date correctly on page refresh however if you sort the other columns it then shows up in the order of month name instead of month year.

When I sort the "Download" column and then try to sort the date column it goes by month not month year like this:

<code>
I am using this for the date order:

jQuery(document).ready(function() {

jQuery('#newsletter').DataTable( {

columnDefs: [ { type: 'date', 'targets': [2] } ],
order: [[ 2, 'desc' ]]

});
} );
</code>
Also after sorting another column I can't sort order the date column it does not move up or down.

I was wondering if I put in search filter for each column if that would fix the problem. If so is there any tutorials on how to do this for each column?
Thank you.
I am not finding information on WordPress other than ajax server side but as stated above I can't get the proper if else to show for file type in the column and the taxonomy would not work. I also found it to be quicker loading this way rather than the ajax.
For the published date I am using
<code>
the_date('F Y');
</code>
I am using Advanced Custom Fields to create the forms to add to tables via custom post type.

I saw this live.datatables.net/piqidoqo/648/edit
and like the drop down filter. Is it possible to have a drop down of the month and one for the year to split the WordPress date?

I have added the table here: live.datatables.net/vejiqadu/1/

This question has an accepted answers - jump to answer

Answers

  • sparklersparkler Posts: 9Questions: 2Answers: 0
  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924

    Also after sorting another column I can't sort order the date column it does not move up or down.

    Don't use columns.type. Datatables will automatically detect the type. To fix the date sorting use the solution presented in this blog to define the format of your date column.

    s it possible to have a drop down of the month and one for the year to split the WordPress date?

    One option is to create a search plugin to search the column either by the month or the year. See if this example gives you an idea of what to do.

    Kevin

  • sparklersparkler Posts: 9Questions: 2Answers: 0

    Hi thanks I updated my table and added the moment.js but now the sort has gone altogether including the search bar. See image:

    I changed the code to this:
    <code>

    jQuery(document).ready(function() { $.fn.dataTable.moment( 'MMMM, YYYY' ); jQuery('#newsletter').DataTable( { order: [[ 2, 'desc' ]] }); } );

    </code>

  • sparklersparkler Posts: 9Questions: 2Answers: 0

    Hi fixed the sort and search coming back, needed to change $ to jQuery. However it is sorting it by month name not the year. This is what happened before adding the moment.js so it still doesn't sort by date MMMM YYYYY
    How to fix please?

  • sparklersparkler Posts: 9Questions: 2Answers: 0

    If I don't have
    <code>
    columnDefs: [ { type: 'date', 'targets': [2] } ],
    order: [[ 2, 'desc' ]]
    </code>

    it won't work for date in order it goes alphabetically. So I am back to square one where it won't sort properly for date. The js doesn't seem to work maybe because of WordPress:
    //cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js
    //cdn.datatables.net/plug-ins/1.10.24/sorting/datetime-moment.js

  • kthorngrenkthorngren Posts: 21,181Questions: 26Answers: 4,924
    Answer ✓

    You have this:

    $.fn.dataTable.moment( 'MMMM, YYYY' )
    

    It dossn't look like your dates have commas. I think you should use this instead:

    $.fn.dataTable.moment( 'MMMM YYYY' )
    

    The format needs to match your date format or it won't be applied. If you still need help please provide a test case showing an example of your dates so we can help debug.

    Kevin

This discussion has been closed.