JQuery dataTable reset sort

I am trying to complete a task, just like this question .

  • I have a working jQuery dataTable
  • I need a reset reset function, which, before the user selects any column, is the same as reading from HTML.

This led me to a good plugin.

http://datatables.net/plug-ins/api/fnSortNeutral

jQuery.fn.dataTableExt.oApi.fnSortNeutral = function ( oSettings )
{
    /* Remove any current sorting */
    oSettings.aaSorting = [];

    /* Sort display arrays so we get them in numerical order */
    oSettings.aiDisplay.sort( function (x,y) {
        return x-y;
    } );
    oSettings.aiDisplayMaster.sort( function (x,y) {
        return x-y;
    } );

    /* Redraw */
    oSettings.oApi._fnReDraw( oSettings );
};

However, I do not know how to do this. Does anyone know what “oApi” is, or do I need one more setting before using this plugin?

As my script shows Uncaught TypeError: Cannot read property 'oApi' of undefined , right after copying the script, and the error calls the undefined function. What should I do?

+4
source share
1

.

var table = $('#example').dataTable();

// Sort in the order that was originally in the HTML
table.fnSortNeutral();

"order" : JSBin

jQuery.fn.dataTableExt.oApi.fnSortNeutral = function ( oSettings )
{
    /* Remove any current sorting */
    oSettings.aaSorting = [];

    /* Sort display arrays so we get them in numerical order */
    oSettings.aiDisplay.sort( function (x,y) {
        return x-y;
    } );
    oSettings.aiDisplayMaster.sort( function (x,y) {
        return x-y;
    } );

    /* Redraw */
    oSettings.oApi._fnReDraw( oSettings );
};

$(document).ready(function() {
    var oTable = $('#example').dataTable({
        "order" : [[ 1, "desc" ]]
    });

    setTimeout(function() {
        oTable.fnSortNeutral()
    }, 1000)

});
+2

All Articles