提问人:Snehal 提问时间:1/3/2023 更新时间:1/3/2023 访问量:184
在 Datatable (ServerSide) sql 查询中使用“Grouped by”语句进行排序
Sorting in Datatable (ServerSide) sql query with "Grouped by" Statement
问:
我正在使用datatable jQuery插件从MySQL数据库中呈现表。我能够根据需要呈现表格。但是排序和个人搜索不起作用。
这是我的数据表脚本:
var table = $("#example").DataTable({
"serverSide" : true,
"processing" : true,
"lengthMenu": [10,25, 50, 100 ],
"ajax" : {
url:"serverdata.php",
type:"POST",
},
// to search individual column value
initComplete: function() {
var api = this.api();
// For each column
api
.columns([0, 1])
.eq(0)
.each(function(i) {
// Set the header cell to contain the input element
var cell = $('.search th').eq(
$(api.column(i).header()).index()
);
var title = $('#example thead tr:eq(1) th').text();
$(cell).html('<input type="text" placeholder="Search " />');
// On every keypress in this input
$('input', this).on('keyup change', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
if ( table.search() !== this.value ) {
table
.search( this.value )
.draw();
}
}
});
});
},
});
PHP代码 : serveride.php
$query = '';
$output = array();
$columns = array('frnumber', 'customername');
$query .= "SELECT * FROM (select frnumber from tablename";
//common search
if(isset($_POST["search"]["value"]))
{
$query .= '
where frnumber LIKE "%'.$_POST["search"]["value"].'%"
OR customername LIKE "%'.$_POST["search"]["value"].'%"
';
}
if(isset($_POST["order"]))
{
$query .= ' ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].')t1 group by frnumber ';
}
else
{
$query .= ' ORDER BY id DESC)t1 group by frnumber ';
}
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
....
...
....
}
我在 Stack Overflow 上研究了这类问题,有很多问题有答案,但似乎都不适合我。
有人可以指导我吗?
答: 暂无答案
评论
order: []
ordering: false