• Resolved tightywritey

    (@tightywritey)


    hi,
    I think I’ve read all the previous posts about this and am still stuck.
    I have a column of date values that I imported that are like YYYY/MMM/DD, for example, 2017/FEB/09. I can easily change the “/” to something else or possibly swap the order, say, 09FEB2017 but would like to avoid all numbers as that’s not always clear to a reader from somewhere (EU or US) …is 1.12.2017 January 12th? or Dec. 1st? Having the text month value is clear.
    Is there some way to sort this YYYY/MMM/DD (or DD/MMM/YYYY or MMM/DD/YYYY etc.)?
    Is it possible to specify the format for the imported date, so whatever sort algorithm there is can chunk it correctly and sort? Would be super nice.

    I saw this but don’t really understand how I’d use it:
    https://datatables.net/blog/2014-12-18
    It seems tantalizingly easy to use but also not. You have to load moment.js and somehow get a reference to the table to be able to do the moment( ‘YYYY/MMM/DD’ ) function on it.

    I tried using
    “columnDefs”: [ { “type”: “date”, “targets”: [ 2 ] } ]
    but it not only didn’t work it seemed to stop the sorting from happening altogether.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your question, and sorry for the trouble.

    Unfortunately, using that moment.js library is not yet possible with DataTables within TablePress, due to how such plugins are loaded. I might add support for it in the future, but cannot guarantee it at this time.

    Your best chance is probably to use the approach from https://wordpress.org/support/topic/date-order-6/ which deals with a very similar scenario.

    Regards,
    Tobias

    Thread Starter tightywritey

    (@tightywritey)

    hi Tobias,
    Thanks for your really quick reply! That’s nice to get a fast response when one is frustrated and stuck.
    I got it to work like you suggested, after some hours of struggle, mostly with my database. My table data comes from a database so I modified my SQL query to generate a new column with a date in MM/DD/YYYY format (I tried YYYY/MM/DD first as it was less work for me but apparently that format is not automatically recognized by TablePress/DataTables).
    Before hiding the extra MM/DD/YYYY formatted column, which I did not want the user to see, I clicked on it’s sort header and it didn’t work–until I added the data type in the Custom Commands field. But, I also had to add the sorting-on-another-column command to the Custom Commands field. I managed to figure out how to add both and the final command is:
    “columnDefs”: [ { “type”: “date”, “targets”: [ 2 ] } , { “orderData”: [ 2 ], “targets”: [ 1 ] } ]

    So column 1 is the “bad date” column I want to click-sort on and column 2 is the extra MM/DD/YYYY column hidden by CSS in PluginOptions:
    .tablepress .column-3 {
    display: none;
    }

    (currently I only have one table so no table number on the tablepress class)

    I just thought I’d add some details here for anyone else who might have a similar problem.

    p.s. any chance you can recommend a table plugin where I can edit and add/delete rows?

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    good to hear that you found a solution for this now 🙂

    I’m not sure what you mean with the last question. You can edit and add/delete rows in TablePress. Or do you mean on the frontend?

    Regards,
    Tobias

    Thread Starter tightywritey

    (@tightywritey)

    yes, I want to have some kind of user-editable table that gets saved to the db. I also would like to be able to export the table data in .csv or some parseable format.

    I see this plugin, https://wordpress.org/plugins/wp-db-table-editor, but don’t know if it’s any good and since you’re a table guru I thought you might be able to recommend something.
    There’s also https://wpdatatables.com but it’s $44 and a bit overkill.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    ah, I see. No sorry, I’m not aware of table plugins that allow frontend data entry 🙁 Sorry.

    Regards,
    Tobias

    Thread Starter tightywritey

    (@tightywritey)

    ok, thanks anyway & for the sort fix.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    no problem! Always happy to help!

    Best wishes,
    Tobias

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘date format and sorting’ is closed to new replies.