Help with sorting by date

Help with sorting by date

SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

I have a fairly simple DataTable that works great, but I can't get the date sorting to work properly. I have read numerous examples and can't seem to get any of them to work.

I am displaying the date as follows.

<td style="text-align:center; font-weight:bold">
                <asp:Label ID="lblNPWOreqComp" runat="server" Width="50" Text='<%# Eval("requested_completion", "{0:MM/dd/yyyy}")%>'></asp:Label>
 </td>

and initializing my DataTable

<script type="text/javascript">
        $(document).ready(function () {
                $('#npwoList').dataTable({
                "lengthMenu": [25, 50, 75, 100],
                //paging: false,
                //"ordering": false,
                "scrolly": 400,
                "stateSave": true,
                "order": [[ 0, "desc" ]]
            });
            $('#npwoList tbody').on('click', 'tr', function () {
                var id = $('td', this).eq(0).text();
                document.getElementById('<%=txtWorkOrderId.ClientID%>').value = id;
                var clickButton = document.getElementById("<%= btnOpenNPWO.ClientID%>");
                clickButton.click();
            });
        });
    </script>

I have removed my attempts at sorting this column, can anyone show me where to implement proper sorting?
Thanks in advance!

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    A date would fall under 'Orthogonal data'

    https://datatables.net/manual/orthogonal-data

    Heres an example with dates: https://datatables.net/examples/ajax/orthogonal-data.html

  • SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

    This looks like it would work if I was getting my data from an ajax call, but I am populating the table from the server from a SQL database.
    Is that true?

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015 Answer ✓

    but I am populating the table from the server from a SQL database. Is that true?

    Are you asking me if you're populating the table from a SQL db?... lol. That would be something you would know, not I.

    And for parsing the orthogonal data, you can use the columnDefs.render setting.

    Heres a small demo I wrote for ya

    That converts epoch to a formatted date, using the tools.date() function I created. Obviously you can do the opposite as well by creating your own function to convert the date into an epoch timestamp.

    You can see in the columnDefs.render setting, I target column index #1, and for the Display and Filter, I show the formatted timestamp, but for sorting, it uses the epoch, which so that way it will sort them in the proper order.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Im not quite sure what the difference between columns.render and columnDefs.render...

    columns.render says:

    The data that is returned by the columns.render option (regardless of if it is used as a function, integer or string) is what DataTables will use for the requested data type (this is called the resolved data)

    But thats what columnDefs.render does as well.. o.O

    @allan, why is columnDefs.render a 404?

  • SoundwavesSoundwaves Posts: 12Questions: 3Answers: 0

    Thanks for such a complete answer, sorry about my poorly written response and thanks for the help.

This discussion has been closed.