【问题标题】:Why can't datatables display data in descending order by default?为什么datatables默认不能按降序显示数据?
【发布时间】:2015-04-05 18:21:32
【问题描述】:

除了datatable中正常的排序能力外,我想默认以降序(按id从mysql结果排序)显示我的datatable。但是没有任何错误,它一直按升序显示数据表。

以下是我目前所做的: JS:

$('#example').dataTable({
    "aaSorting": [[0, "desc"]]
});

和我的服务器端: PHP:

SELECT s . * , c.name AS cls, sec.name AS sect
                    FROM `student` s
                    INNER JOIN cls c 
                    ON s.cls = c.id
                    INNER JOIN sect sec 
                    ON s.sect = sec.id 
                    ORDER BY s.id DESC

HTML:

<table class="table table-striped 
 table-bordered bootstrap-datatable datatable" id="example">
//table data here
</table>

非常感谢任何建议和帮助。

【问题讨论】:

  • 会不会是JS跟SQL倒序了?
  • 也许您订购了两次:在您的 SQL 中:DESC,然后在 JS 中:再次 DESC。 DESC*DESC=ASC

标签: php jquery mysql datatable


【解决方案1】:

我也面临同样的问题。我们可以通过在数据表中按降序逐一添加数据来解决。通过 html 和

创建一个空表
var table = $("#example").dataTable();

使用ajax并在变量中按降序获取数据。

 $.ajax({
          // params
          success(data){
            table.fnClearTable(); // to clear the table
            $.each(data, function(i,item)){
               table.fnAddData([data.item1, data.item2,data.item3,.....]) // adding data
            }
            table.fnDraw(); // re draw the table
      }
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2020-02-07
    • 2013-05-31
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多