我使用库在表中呈现我的数据库: 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->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 ';
}
有人知道可能出现什么问题吗?!?
答案 0 :(得分:0)
正如您在问题中所提到的,$_POST['order'][2]['column']
是整数,它使排序子句无效(例如,ORDER BY 2 asc
)。
我建议使用ssp.class.php
进行使用PHP的服务器端处理。它可以在官方DataTables发行版中找到。