简体   繁体   English

JavaScript Datatable.JS / PHP MySQLi排序问题

[英]JavaScript Datatable.JS / PHP MySQLi Sorting Issue

So I'm using Datatables.JS to create a table of results returned from a MySQL database. 因此,我正在使用Datatables.JS创建一个从MySQL数据库返回的结果表。 When I return the results I'm trying to display them in DESC order. 当我返回结果时,我试图按DESC顺序显示它们。 I have tried using ORDER BY DESC in my MySQLi query, this does return the results in the correct order, however when datatables is reading the results it's displaying them in some random order. 我尝试在MySQLi查询中使用ORDER BY DESC,这确实会以正确的顺序返回结果,但是,当数据表读取结果时,它会以某种随机顺序显示它们。 So I've tried playing with the datatable settings to sort by my ID column but keep that column hidden. 因此,我尝试使用数据表设置按ID列进行排序,但将该列保持隐藏状态。 Whenever I attempt to add any code to handle the sorting, the sorting issue itself becomes resolved but all my pagination and buttons such as the buttons that allow the user to select which columns they would like to view just disappear. 每当我尝试添加任何代码来处理排序时,排序问题本身都会解决,但是我所有的分页和按钮(例如允许用户选择他们要查看的列的按钮)都消失了。 Below is the JS I'm using to select the features and setup I need for this datatable, can anyone show me how to add sorting by the ID column DESC into this without breaking it? 以下是我用来选择此数据表所需的功能和设置的JS,谁能告诉我如何在不破坏ID的情况下将ID列DESC添加到其中?

    $(document).ready(function() {
    $('#datatable').DataTable();

    //Buttons examples
    var table = $('#datatable-buttons').DataTable({
        pageLength: 20,
        lengthChange: false,
        searching: false,
        buttons: ['copy', 'excel', 'pdf', 'colvis']
    });

    table.buttons().container()
        .appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
} );

Adding ordering: and then selecting the column and defining the order seems to break all the other settings, I lose my pagination and default results length as well as all control buttons on the page. 添加排序:然后选择该列并定义顺序似乎破坏了所有其他设置,我失去了分页和默认结果长度以及页面上的所有控制按钮。

$(document).ready(function() {
    $('#datatable').DataTable();

    //Buttons examples
    var table = $('#datatable-buttons').DataTable({

        pageLength: 20,
        lengthChange: false,
        searching: false,
        buttons: ['copy', 'excel', 'pdf', 'colvis'],
        order: [[ 1, desc ]]
    });

    table.buttons().container()
        .appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
} );

I have no idea why this solutions works and I still don't understand what caused the original problem but I did find a working piece of code posted under a loosely relevant problem on another forum. 我不知道为什么这种解决方案有效,但我仍然不明白是什么原因导致了最初的问题,但是我确实在另一个论坛上的一个松散相关的问题下找到了一段有效的代码。 I combined that code with what I had already and it solved my problem. 我将该代码与现有代码结合起来,从而解决了我的问题。 Hopefully this helps someone else out who may encounter the same issue. 希望这可以帮助其他可能遇到相同问题的人。

$(document).ready(function() {


    $('#datatable-buttons').DataTable( {
        pageLength: 20,
        lengthChange: false,
        searching: false,
        ordering: true,
        order: [[ 3, 'DESC' ]],
        dom: 'B1frtip',
        buttons: [
            'copy',
            'excel',
            'pdf',
            'colvis'
        ]
    });

      table.buttons().container()
     .appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');  
});

Just wanted to mention this JS was for a datatable that's part of the Fonik Bootstrap Admin Template. 只是想提一下,此JS是针对Fonik Bootstrap管理模板一部分的数据表的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM