Okay
  Public Ticket #3730123
When clicking it sort, is it possible for it to default to descending rather than ascending?
Closed

Comments

  • Khanh Vo started the conversation

    Hi there,

    When clicking a column sorting, is it possible for it to default to descending rather than ascending?

    Thanks,

    Khanh

  •  1,846
    Miloš replied

    Hello,
    It is not possible with a built-in option, but our developers have a custom workaround code edit which you can use, in order to change how the default 'initial sort order' is triggered when clicking in the Headers.

     

    Please find this JS file in our Plugin's files - via your FTP ( or Hosting File Manager) in this path :

    ..\wp-content\plugins\wpdatatables\assets\js\wpdatatables\wpdatatables.js.

    Find this piece of code, it should be around line 2255 ( best is to use CTRL+F to search for it rather than looking at exact lines, since our devs change code during some Updates):

    $('table.wpDataTable:not(.wpdtSimpleTable)').each(function () {
                var tableDescription = JSON.parse($('#' + $(this).data('described-by')).val());
                wdtRenderDataTable($(this), tableDescription);
            });
    1387835047.png

    Add this under it :

    $('th.sort').on('click', function(){
        var tableDescription = JSON.parse($('#' + $('table.wpDataTable:not(.wpdtSimpleTable)').data('described-by')).val());
        var oSettings = wpDataTables[tableDescription.tableId].fnSettings();
        oSettings.aaSorting[0][1] = oSettings.aaSorting[0][1] === 'asc' ? 'desc' : 'asc';
        oSettings.aoColumns[$(this)[0].cellIndex].asSorting[1];
        wpDataTables[tableDescription.tableId].fnDraw();
    });

    ( That is the full edited code which works on Manual Tables, too)

    So in the end, that entire piece of code should look like this :

    $('table.wpDataTable:not(.wpdtSimpleTable)').each(function () {
                var tableDescription = JSON.parse($('#' + $(this).data('described-by')).val());
                wdtRenderDataTable($(this), tableDescription);
            });
            $('th.sort').on('click', function(){
                var tableDescription = JSON.parse($('#' + $('table.wpDataTable:not(.wpdtSimpleTable)').data('described-by')).val());
                var oSettings = wpDataTables[tableDescription.tableId].fnSettings();
                oSettings.aaSorting[0][1] = oSettings.aaSorting[0][1] === 'asc' ? 'desc' : 'asc';
                oSettings.aoColumns[$(this)[0].cellIndex].asSorting[1];
                wpDataTables[tableDescription.tableId].fnDraw();
            });
        });
    3785461727.png

    Save changes, and now this is going to make all Headers in all your Tables have a default sorting order of Descending - when any Header is clicked on.

    Then the User needs to click again to switch it as  "Ascending" order.


    Let us know if that works, now all your tables ( server side and non server side) should be sorting as 'descending' by default when the Header is clicked on the first time.

     

    If you need us to edit this code for you, please provide me a temporary WP-admin (administrator) user for your site where this happens, and FTP credentials.

    We do not interfere with any data or anything else except for the plugin (in case that’s a production version of the site), and of course, we do not provide login data to third parties. 

    You can write credentials here just check PRIVATE Reply so nobody can see them except us.

     

    Thank you.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Khanh Vo replied

    Thank you Miloš.

  •   Miloš replied privately