【问题标题】:sorting setup with datatable.net (server side)使用 datatable.net 排序设置(服务器端)
【发布时间】:2018-06-29 16:01:21
【问题描述】:

我使用库在表中呈现我的数据库: https://datatables.net/

我的问题是它只能很好地排序我的第一个列。 在代码方面是[0],我的第一列是"id"

每次我尝试设置它时,我都会从 Chrome 的网络检查中收到此消息错误

<br />
<b>Notice</b>:  Undefined offset: 2 in <b>/.../xxx/.../appel.php</b> on line <b>22</b><br />
<br />
<b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'asc LIMIT 0, 10' at line 1 in .../xxx/.../appel.php:37
Stack trace:
#0 .../xxx/.../appel.php(37): PDOStatement-&gt;execute()
#1 {main}
  thrown in <b>.../xxx/.../appel.php</b> on line <b>37</b><br />

我的头发正在脱落,我完全不知道会发生什么...... 在documentation 我看到了

order[i][column]    integer Column to which ordering should be applied. This is an index reference to the columns array of information that is also submitted to the server.
order[i][dir]   string  Ordering direction for this column. It will be asc or desc to indicate ascending ordering or descending ordering, respectively.

所以这应该可行........

这是我的 AJAX 调用

var dataTable = $('#tableau').DataTable({
        "processing":true,
        "serverSide":true,
        "order":[],
        "ajax":{
            url:"actions/appel.php", // adresse du script AJAX
            type:"POST"
        },

        "columnDefs":[
            {
                "targets":[3, 4, 5], // exeptions des ordres de triage
                "orderable":false,
            },
        ],
    });

和我的 PHP 代码 (appel.php)

if(isset($_POST["order"]))
{
    $query .= 'ORDER BY '.$_POST['order'][2]['column'].' '.$_POST['order'][0]['dir'].' ';
}
else
{
    $query .= 'ORDER BY id DESC ';
}

有人知道会出什么问题吗?!?

【问题讨论】:

    标签: php jquery ajax sorting datatables


    【解决方案1】:

    正如您在问题中指出的,$_POST['order'][2]['column'] 是整数,这使得排序子句无效(例如,ORDER BY 2 asc)。

    我建议使用ssp.class.php 使用 PHP 进行服务器端处理。它在官方 DataTables 发行版中可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2023-02-25
      • 2017-06-17
      • 2015-08-27
      相关资源
      最近更新 更多