Datatables only sorts by int with error

Datatables only sorts by int with error

smcconnell13smcconnell13 Posts: 2Questions: 1Answers: 0

I have a datatables set up to read JSON from the server. One of the columns however should be sorted by it's hidden index versus what is displayed on the screen.

This is what I have to display the table:

        $("#ad-table").dataTable({
            "lengthMenu": [[100, 50, 25], [100, 50, 25]],
            "ajax": {
                "url": "assessmentsduetable?peid="+peId,
                "dataSrc": ""
            },

            dom: 'Bfrtip',
            "buttons": ['print', 'pageLength'],
            "order": [[4, "asc"]],
            "columns": [
           { "data": "Consumer ID" },
           { "data": "Last Name" },
        {
            "data": {
                _: "WindowNameInfo.WindowDue",
                sort: "WindowNameInfo.WindowDueIndex"

            }
        },
           { "data": "Window End Date" },
           { "data": "Days Left In Window" },
           { "data": "Assessment Name" },
           { "data": "Date of Last Assessment" },
           { "data": "# of Assessments" },
           { "data": "Clinician" },
           { "data": "Clinic" }
            ]
        });



    });

The problem is with WindowNameInfo.WindowDue and WindowNameInfo.WindowDueIndex. For some reason, it seems to be sorting on WindowDueIndex as if it were a string even though it is an integer. However, when I do this:

   "data": {
                    _: "WindowNameInfo.WindowDue",
                    sort: "WindowNameInfo.WindowDueIndex",
                    type "int"

                }

it throws an error, then continues to display the table, this time with it sorting properly!

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Answer ✓

    type "int"

    Set that to be:

    sort: "WindowNameInfo.WindowDueIndex",
    

    The type option also tells DataTables where to get the data to use for the type detection.

    If you do want to specifically set a type use columns.data, although int is not one of the build in types. Best to just let the built in type detection do its thing with your data.

    Allan

  • smcconnell13smcconnell13 Posts: 2Questions: 1Answers: 0

    Yes, that did fix the issue! However, there was a typo in that response. What did the trick was:

                         _: "WindowNameInfo.WindowDue",
                        sort: "WindowNameInfo.WindowDueIndex",
                        type: "WindowNameInfo.WindowDueIndex",
    

    Thank you!

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Answer ✓

    Oops - good spotting! Good to hear it helped though :-)

    Allan

This discussion has been closed.