render date and retain order functionality (ajax object)

render date and retain order functionality (ajax object)

crush123crush123 Posts: 417Questions: 126Answers: 18
edited December 2014 in DataTables 1.10

I am using datatables 1.10 with an ajax datasource and am struggling with the syntax for rendering then ordering the json data , specifically date values

My raw date data is in the form 2015-12-10 11:30:00 and my date column is displayed properly and sorts properly.

Using examples from the site as a guide, I have the following code snippet to render the date column, which uses the jquery-dateFormat.min.js to format the date values

    {
       "render": function ( data, type, row )              
    var rowvalue = row["EventDate"];
    return ($.format.date(rowvalue, "E dd/MM/yyyy"));
    },
        "targets": 2
    },

This works, and the dates are displayed as I want them but the sorting no longer works

On earlier incarnations of datatables, I used 2 instances of the date column - hid one of them and formatted the other. I then applied the sort to the hidden column when clicking on the column header of the visible one.

From what I understand, I can use the render function to change the display, but also use it to sort by the original form.

I tried following the example on http://datatables.net/manual/orthogonal-data#Computed-values But can’t get it to work.

Would appreciate some pointers.

a cut down work in progress page here... http://test2.forthwebsolutions.com

Thanks

This question has an accepted answers - jump to answer

Answers

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited January 2015

    Since the original post, I have tried to implement the moment.js plugin. - but the sorting still doesnt work properly.

    I am using a date from a mysql table, in the format YYYY-MM-DD

    i then render this value using moment to format the raw date

    "render": function ( data, type, row ) {
                    var rowvalue = row["EventDateTo"];
                    var rowvalueallday = row["AllDayEvent"];
                    if (rowvalueallday == 0) {
                     return (moment(rowvalue).format("DD/MM/YYYY (HH:mm)"));
                    } else {
                     return (moment(rowvalue).format("DD/MM/YYYY"));
                    }
                    },
                "targets": 3
    

    in my js, before the table script, i add

        $.fn.dataTable.moment( 'DD/MM/YYYY (HH:mm)' );
    $.fn.dataTable.moment( 'DD/MM/YYYY' );
    

    but the dates do not sort correctly - the column is always sorted as if was alphanumeric

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    Fixed it

    I revisited http://datatables.net/manual/orthogonal-data#Computed-values and having read through it again, I used moment to format my sql formatted date for display and left it as for sorting.
    I also used a boolean variable rowvalueallday to format the date with a time where required

    { "data": "EventDate",
            render: function ( data, type, row ) {
              // If display or filter data is requested, format the date
              if ( type === 'display' || type === 'filter' ) {
                  var rowvalue = row["EventDate"];
                  var rowvalueallday = row["AllDayEvent"];
                          if (rowvalueallday == 0) {
                           return (moment(data).format("ddd DD/MM/YYYY (HH:mm)"));
                          } else {
                           return (moment(data).format("ddd DD/MM/YYYY"));
                          }         
              } 
              // Otherwise the data type requested (`type`) is type detection or
              // sorting data, for which we want to use the raw date value, so just return
              // that, unaltered
              return data;
              }
            },
    
  • allanallan Posts: 61,753Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi,

    Sorry I missed your thread before - but great to hear that you have a solution for the problem now!

    Allan

This discussion has been closed.