Sorting of calculated date column not working

Sorting of calculated date column not working

GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

I am adding 8 years to the date of birth and then displaying as a new column. The sorting on this new column is not working. The date is read in from json. The dates are calculated and displayed correctly however sort gives:

I have been unable to get your required example (http://live.datatables.net/idinat/738/edit?html,css,js,console,output) to work as it is html input.

I have read the examples and "{targets: [3, 4, 7, 12, 13, 14, 15], render: $.fn.dataTable.render.moment( 'DD/MM/YYYY' )}," is included.

Kind regards,

Glyn

Replies

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    edited March 2020

    If you add the column after Data Table initialization it is probably not working. If you do the adding of the 8 years server side and send the additional column from the server with the other columns it should work.

    Maybe it helps to do this again after adding the additional column?!
    https://datatables.net/reference/api/order()

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Hi rf1234,

    Yes that would work. I am not a programmer, so is it preferable to do this sort of work server side or does not matter?

    Kind regards,

    Glyn

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    edited March 2020

    I do it server side because I think it is easier.
    In addition, as far as I know you can't add columns after Data Tables initialization using the api.

    Take a look at this please. The author of Data Tables @allan gave some valuable comments on it:
    https://datatables.net/forums/discussion/57727

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416

    Because I think it is much easier to return additional columns (i.e. columns that don't exist in the data base) I do this using Editor using aliases of existing columns like this for example. dob is the database column and dob_plus_eight_years is the calculated column that doesn't exist in the database.

    Field::inst( 'dob' ),
    Field::inst( 'dob AS dob_plus_eight_years' )->set( false )
        ->getFormatter( function ( $val, $data, $opts ) {
            $date = new DateTime($val);
            $date->modify('+8 years');
            return $date->format('Y-m-d H:i:s');
        } ),       
    
  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    OK, I will look at it from this angle. Thank you :-)

This discussion has been closed.