doutan3192 2018-01-25 12:41
浏览 75

datatables.net列对另一列进行排序

I'm still in deep trouble with that issue. I use datatable.net to display some data from my database (mySql).

I do exactely what the API notice me to do in order to sort columns by name ASC (toggle DESC). https://datatables.net/manual/server-side

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.

So my code is like this:

AJAX call:

var dataTable = $('#tableau').DataTable({
        "processing":true,
        "serverSide":true,
        "order":[],
        "orderFixed": [],
        "ordering":true,
        "ajax":{
            url:"/charbon/clae-listes/actions/appel.php", 
            type:"POST"
        },

        "columnDefs":[
            {
                "targets":[ 2, 3], 
                "orderable":false,
            },
        ],
    });

And my appel.php script

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

That's where trouble begins:

when I click my 1st column I receive that error message on localhost :

DataTables warning: table id=tableau - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

And when I click my 2nd column to sort it that sorts the previous column. It's like datatables picks directly the column number from my database, and offset of 1 because my database doesn't have a column 0

how to attribute the right column to the right sorting?

output JSON response

1st column

<b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' in /Users/.../appel.php:45

2nd column

{"draw":9,"recordsTotal":10,"recordsFiltered":11,"data":[["type","Client","client".........

My headers

draw:9
columns[0][data]:0
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:1
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
columns[2][data]:2
columns[2][name]:
columns[2][searchable]:true
columns[2][orderable]:true
columns[2][search][value]:
columns[2][search][regex]:false
columns[3][data]:3
columns[3][name]:
columns[3][searchable]:true
columns[3][orderable]:false
columns[3][search][value]:
columns[3][search][regex]:false
order[0][column]:1
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false
  • 写回答

1条回答 默认 最新

  • doushenjia8514 2018-01-25 12:50
    关注

    Use header('Content-Type: application/json'); in order to send JSON data to the browser without having to parse it.

    $output_data = /** your array with the data **/;
    header('Content-Type: application/json');
    echo json_encode($output_data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示