DataTables在某些列上排序错误?

时间:2014-05-01 18:10:19

标签: jquery sorting datatables

我正在使用JQuery插件Datatables并且它没有按照它应该排序,第一列排序正常,但其他一切都排序错误。当我尝试对列进行排序"长度"我收到一个错误。自己测试一下:http://ppaddict.com/tests.php 它们都是int或double,在phpmyadmin中我排序它们没有问题。 长度是"时间"寿。

$(document).ready(function() {
  var oTable = $('#example').dataTable({
    "bServerSide": true,
    "sAjaxSource": "serversideTests.php",
    "sAjaxDataProp": "aaData",
    "bProcessing": true,
      "aoColumns": [{
      "mData": [0],
      "sClass" : "hidden"
    }, {
      "mData": [1],
      "sClass" : "aligncenter"
    }, {
      "mData": [2],
      "sClass" : "aligncenter"
    }, {
      "mData": [3],
      "sClass" : "aligncenter"
    }, {
      "mData": [4],
      "sClass": "hidden"
    }, {
      "mData": [5],
      "sClass": "hidden"
    }, {
      "mData": [6],
      "sClass": "hidden"
    }, {
      "mData": null,
      "bSortable" : false,
      "sClass" : "aligncenter",
      "mRender": function(data, type, full) {
        return full[4] + ' - ' +
            full[5] + ' [' + full[6] + ']' +
          '<br><a href=' + 'http://osu.ppy.sh/d/' + full[0] + '>' + 'Download' + '</a>';
      }
    }, {
      "mData": [7],

      "sClass" : "aligncenter"
    }, {
      "mData": [8],
      "sClass" : "aligncenter"
    }, {
      "mData": [9],
      "sClass" : "aligncenter"
    }, {
      "mData": [10],
      "sClass" : "aligncenter"
    }, {
      "mData": [11],
      "sClass" : "aligncenter"
    }], "aaSorting": [[2, 'desc']]
  })
})

我正在使用DataTables的服务器端示例进行一些修改:

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $aColumns[$i] == "setid" )
            {
                /* Special output formatting for 'version' column */
                $row[] = ($aRow[ $aColumns[$i] ]) ? /* <a href='$aRow[ $aColumns[$i] ]'><img src='http://s.ppy.sh/images/osu-download-direct.png'></a>*/
           '<a href=\'http://osu.ppy.sh/s/'.$aRow[ $aColumns[$i]].'\' target=\'_blank\'><img height=\'100\' witdh=\'100\' src=\'http://b.ppy.sh/thumb/'.$aRow[ $aColumns[$i]].'l.jpg\' alt=\'N/A\'></a><a href=\'osu://dl/'.$aRow[ $aColumns[$i]].'\'><img height=\'100\' witdh=\'100\'  src=\'http://s.ppy.sh/images/osu-download-direct.png\'></a>'  : $aRow[ $aColumns[$i] ];
            }

            else if ( $aColumns[$i] == 'length' ) 
            {
                $row[] = ($aRow[ $aColumns[$i] ]) ? 


date('i:s',strtotime($aRow[ $aColumns[$i] ]))   : $aRow[ $aColumns[$i] ];

            }


            else if ( $aColumns[$i] != ' ' )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }

    echo json_encode( $output );

1 个答案:

答案 0 :(得分:0)

数据表默认按第一列排序

您可以通过添加aoColumns

来更改默认排序
$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [
            null,
            { "asSorting": [ "asc" ] },
            { "asSorting": [ "desc", "asc", "asc" ] },
            { "asSorting": [ "desc" ] },
            null
        ]
    } );
} );

或者您可以按定义的列

进行排序
$(document).ready(function() {
    $('#example').dataTable( {
        "order": [[ 3, "desc" ]]
    } );
} );