【问题标题】:Sorting DataTable by recent date按最近日期对数据表进行排序
【发布时间】:2021-10-31 04:07:35
【问题描述】:

我有一张桌子,它按旧日期排序。 我只想按最近日期(第一列)对其进行默认排序。 代码是:

<script>
    $(document).ready( function () {
          $('#table_id').DataTable({
    dom: 'B<"clear">lfrtip',
    buttons: {
        name: 'primary',
        buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
        }
      });
});
</script>

它看起来像:

我在网上尝试了很多解决方案,但都没有奏效!

<script>
    $(document).ready( function () {
          $('#table_id').DataTable({
    dom: 'B<"clear">lfrtip',
    buttons: {
        name: 'primary',
        buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
        }
      }).asSorting([[0, "desc"]]);
});
</script>

还有这个

<script>
    $(document).ready( function () {
          $('#table_id').DataTable({
    dom: 'B<"clear">lfrtip',
    buttons: {
        name: 'primary',
        buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
        }
      }).fnSort([[0, "acs"]]);
});
</script>

还有这个

<script>
    $(document).ready( function () {
          $('#table_id').DataTable({
    "order": [[ 0, "desc" ]]
    dom: 'B<"clear">lfrtip',
    buttons: {
        name: 'primary',
        buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
        }
      });
});
</script>

还有这个

> <script>
>     $(document).ready( function () {
>           $('#table_id').DataTable({
>             "aoColumnDefs": [
>       { "asSorting": [ "asc" ], "aTargets": [ 0 ] }
>     ]
>     dom: 'B<"clear">lfrtip',
>     buttons: {
>         name: 'primary',
>         buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
>         }
>       }); }); </script>

有什么建议吗?

【问题讨论】:

    标签: javascript html django html-table datatable


    【解决方案1】:

    我根据屏幕截图中提供的数据创建了一个示例,但在我的示例中,除日期之外的所有值都相同,因此我可以演示它是如何工作的。

    您已经非常接近了,您只需要包含以下 DataTables 选项

    DataTables order option

    这里:

    var data = [
    {
        "DateofTest": "2006-10-26",
      "OIL": "17.79",
      "WATER": "30.7",
      "GAS": "15380",
      "Gas-Lift": "5330"
    },
    {
        "DateofTest": "2007-05-26",
      "OIL": "17.79",
      "WATER": "30.7",
      "GAS": "15380",
      "Gas-Lift": "5330"
    },
    {
        "DateofTest": "2008-03-26",
      "OIL": "17.79",
      "WATER": "30.7",
      "GAS": "15380",
      "Gas-Lift": "5330"
    }
    ];
    
     $(document).ready( function () {
        $('#example').DataTable({
        data: data,
        "columns" : [
        {"data":"DateofTest"},
        {"data":"OIL"},
        {"data":"WATER"},
        {"data":"GAS"},
        {"data":"Gas-Lift"}
        ],
        dom: 'B<"clear">lfrtip',
        
        // 0 is the first column it is ordering by (Date of Test)
        // 'desc' (descending) is ordering from most recent date to the oldest dates on bottom
        //'asc' (ascending) is ordering from the oldest date to the most recent on bottom
        order: [0, 'desc'],
        buttons: {
            name: 'primary',
            buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
            }
          });
    });
    td {
      text-align: center;
    }
    <!DOCTYPE html>
      <html>
        <head>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/jszip-2.5.0/dt-1.11.0/b-2.0.0/b-colvis-2.0.0/b-html5-2.0.0/b-print-2.0.0/date-1.1.1/r-2.2.9/rg-1.1.3/datatables.min.css"/>
     
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/jszip-2.5.0/dt-1.11.0/b-2.0.0/b-colvis-2.0.0/b-html5-2.0.0/b-print-2.0.0/date-1.1.1/r-2.2.9/rg-1.1.3/datatables.min.js"></script>
    
    
        </head>
        <body>
          <div class="container">
            <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th>Date of Test</th>
                                <th>OIL (m3/d)</th>
                                <th>WATER (m3/d)</th>
                                <th>GAS (m3/d)</th>
                                <th>Gas Lift (m3/d)</th>
                            </tr>
                        </thead>
                    </table>
          </div>
        </body>
      </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      相关资源
      最近更新 更多