Using DataTable.datetime('DD/MM/YY');

Using DataTable.datetime('DD/MM/YY');

csyucsyu Posts: 21Questions: 4Answers: 1
edited May 18 in DataTables 2

Error messages shown:
TypeError: i.datetime is not a function

Description of problem:
I'm am looking to have Datatables render a datetime column so that I can sort it correctly.

When I add DataTable.datetime('DD/MM/YY'); to my Vue page, I get the error above.

My full imports:

import moment from 'moment';
import DataTable from 'datatables.net-vue3';
import DataTablesLib from 'datatables.net';
import 'datatables.net-select';
import 'datatables.net-responsive';

DataTable.use(DataTablesLib);
DataTable.datetime('DD/MM/YY');

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,488Questions: 1Answers: 10,467 Site admin

    It is a method of the datatables.net package. Try DataTablesLib.datetime.

    Allan

  • csyucsyu Posts: 21Questions: 4Answers: 1
    edited May 19

    Thanks Allan.

    Not sure why it's still not working. I now have this code:

    import moment from 'moment';
    import DataTable from 'datatables.net-vue3';
    import DataTablesLib from 'datatables.net';
    import 'datatables.net-select';
    import 'datatables.net-responsive';
    DataTable.use(DataTablesLib);
    DataTablesLib.datetime('DD/MM/YY');
    

    But I get the warning via a browser alert:

    DataTables warning: Formatted date without Moment.js or Luxon - https://datatables.net/tn/17
    

    Moment.js is loaded properly on the page because I can do this successfully:

    render: function (data:any) {
      return moment(data).format('MMM DD YYYY');
    }
    

    Any thoughts on if the DataTablesLib has another dependency to make this work?

  • allanallan Posts: 63,488Questions: 1Answers: 10,467 Site admin
    Answer ✓

    That's a stumbling block I hadn't considered - apologies. In that context moment is local to that file, but DataTables is looking for it globally. I'll need to add an abstraction to DataTables to allow for that, and let it be set.

    At the moment what you'll need to do is assign moment to window - e.g.:

    window.moment = moment;
    
    DataTable.use(DataTablesLib);
    DataTablesLib.datetime('DD/MM/YY');
    

    should do it.

    Allan

  • csyucsyu Posts: 21Questions: 4Answers: 1

    Thank you Allan, your solution worked.

Sign In or Register to comment.