1

I have a custom component that is using the v-client-table method from the Vue Tables 2. The table has two columns, one of which is a datetime column (format: MM/DD/YYYY h:mm A). I have it as sortable, but it doesn't sort accurately based on the time. The output of that appears like this:

table output example

As you can see, it goes from 12:09 PM to 12:30 AM. Unfortunately, this date/time format is required.

Question: is there a way to incorporate momentjs into the configuration to make this properly sort (without using a filter as it shows in the client-side-sorting documentation)?

Below is the data and options:

export const content = {
    state: {
        columns: ['callDateTime', 'explanation'],
        tableData: [
            { callDateTime: '10/13/2017 10:09 AM', explanation: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." },
            { callDateTime: '10/13/2017 12:30 AM', explanation: "Lorem ipsum dolor" },
            { callDateTime: '10/13/2017 12:09 PM', explanation: "Lorem ipsum dolor sit amet },
            { callDateTime: '10/13/2017 1:15 PM', explanation: "Ut a fringilla mauris" },
            { callDateTime: '10/13/2017 1:30 PM', explanation: "Lorem ipsum dolor" }
        ],
        options: {
            headings: {
                'callDateTime': 'Call Date/Time',
                'explanation': 'Interaction Notes',
            },
            filterable: 'false',
            sortable: 'callDateTime',                           
            orderBy: { column: 'callDateTime' },
            pagination: {
                edge: false,
                dropdown: false,
                chunk: 1
            },
            sortIcon: {
                down: 'arrow arrow-down',
                up: 'arrow arrow-up'
            },
            perPage: '10',
            texts: {
                count: ''                                       
            }
        }
    }
}
cfnerd
  • 3,658
  • 12
  • 32
  • 44

1 Answers1

3

Use can use customSorting for this

customSorting: {
    callDateTime: function (ascending) {
        return function (a, b) {
            let dateA = new Date(a.callDateTime);
            let dateB = new Date(b.callDateTime);

            if (ascending)
                return dateA >= dateB ? 1 : -1;

            return dateA <= dateB ? 1 : -1;
        }
    }
}
cfnerd
  • 3,658
  • 12
  • 32
  • 44
ittus
  • 21,730
  • 5
  • 57
  • 57
  • You were close. The a, b variables are actually objects, so you have to pass in the correct field to the new Date() function. I made an edit to your answer. Once the edit goes through, I will award you the answer. Thanks again! – cfnerd Jun 19 '18 at 13:55
  • No problem. Happy coding! – ittus Jun 19 '18 at 13:56