日期未使用数据表进行排序

时间:2016-08-12 20:47:30

标签: mysql datatables

我在网站上使用Datatables来显示MySQL记录。我的问题是 在第一页加载时,记录不会按预期从查询中显示。 在这里你有查询,' vigencia'是类型为date的字段,记录不按降序日期排序' vigencia':

// Execute job
  if ($job == 'get_companies'){

    // Get companies
    $query = "SELECT * FROM tb_documentos LEFT JOIN tbl_uploads ON tb_documentos.id_doc = tbl_uploads.doc ORDER BY vigencia DESC";
    $query = mysqli_query($db_connection, $query);
    if (!$query){
      $result  = 'error';
      $message = 'query error';
    } else {
      $result  = 'success';
      $message = 'query success';
      while ($company = mysqli_fetch_array($query)){

        $imagen = "<a href='uploads/uploads/".$company['file']."''><img src='uploads/uploads/".$company['file']."' width='80' height='60'>";
        $mysql_data[] = array(

          "numero_identificador"  => $company['numero_identificador'],
          "serie"    => $company['serie'],
          "numero_documento"       => $company['numero_documento'],
          "tipo_documento"     => $company['tipo_documento'],
          "vigencia"     => $company['vigencia'],
          "archivo"  =>   $imagen
        );
      }
    } 

这里有JS代码:

 // On page load: datatable
  var table_companies = $('#table_companies').dataTable({
    "ajax": "data_vencimientos.php?job=get_companies",
    "columns": [

      { "data": "numero_identificador" },
      { "data": "serie" },
      { "data": "numero_documento" },
      { "data": "tipo_documento" },
      { "data": "vigencia", },
      { "data": "archivo" }
    ],
    "aoColumnDefs": [
      { "bSortable": false, "aTargets": [-1] }
    ],
    "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
    "oLanguage": {
      "oPaginate": {
        "sFirst":       " ",
        "sPrevious":    " ",
        "sNext":        " ",
        "sLast":        " ",
      },
      "sLengthMenu":    "Documentos por pagina: _MENU_",
      "sInfo":          "Total de _TOTAL_ documentos (mostrando del _START_ al _END_)",
      "sInfoFiltered":  "(filtrados _MAX_ documentos)"
    }
  });

首页加载后,我可以正确点击列表标题对记录进行排序。

我应该更改哪些内容以显示由&#39; vigencia&#39;排序的记录。 (YYYY-m-d类型的日期字段,例如2016-08-12)?

1 个答案:

答案 0 :(得分:0)

深入研究Datatables文档,我发现您可以更改排序顺序,只需在AJAX函数中包含一个参数,就像我的情况一样:

“order”:[[3,“desc”]],

它现在有效。

感谢你。