如何从两个其他字段的时差值向数据表添加自定义列

时间:2019-04-16 09:57:05

标签: javascript jquery datatables

我有一个包含许多字段和两个日期字段created_atupdated_at的MySQL表。

我希望DataTables中的一列具有这两个字段之间的时间差。

我正在为数据表和serverSide:true使用选项ssp.class.php

下面是我的代码。

 <?php
    error_reporting("E_ALL");
    if (isset($_GET['ajax'])) {
        $table = 'table_here';
        // Table's primary key
        $primaryKey = 'id';
        // Array of database columns which should be read and sent back to DataTables.
        // The `db` parameter represents the column name in the database, while the `dt`
        // parameter represents the DataTables column identifier. In this case simple
        // indexes
        $columns = array(
                array('db' => 'type', 'dt' => 0),
                array('db' => 'uniqueid', 'dt' => 1),
                array('db' => 'created_at', 'dt' => 8),
                array('db' => 'updated_at', 'dt' => 9), );
        require('ssp.class.php');
        echo json_encode(
            SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns));
        exit();
    }
?>
$(document).ready(function () {
    const table = $('#example').DataTable({
            "columnDefs": [{
                    "targets": [7],
                    "visible": false,
                }
            ],
            "processing": true,
            "serverSide": true,
            "ajax": "url"
        });
});

1 个答案:

答案 0 :(得分:1)

您可能需要为此使用columns.render

const dataTables = $('#mytable').DataTable({
      ...
      columns: [
        ...
        {title: 'delta', data: null, render: (data, type, row, meta) => {
            //translate your timestamp strings into actual Date 
            const created = new Date(row.created);
            const updated = new Date(row.updated);
            //calculate the difference in seconds and return as a cell contents
            return Math.round((updated-created)/1000);
          }
        }
      ]
    });

下面您可能会找到演示

//sample source data
const dataSrc = [
  {id: 1, created: '16 Apr 2019, 16:10:15', updated: '16 Apr 2019, 16:15:21'},
  {id: 2, created: '16 Apr 2019, 16:15:08', updated: '16 Apr 2019, 16:25:18'},
  {id: 3, created: '16 Apr 2019, 16:20:01', updated: '16 Apr 2019, 16:21:15'}
];
//DataTables initialization
const dataTables = $('#mytable').DataTable({
  dom: 't',
  data: dataSrc,
  columns: [
    {title: 'id', data: 'id'},
    {title: 'created', data: 'created'},
    {title: 'updated', data: 'updated'},
    {title: 'delta', data: null, render: (data, type, row, meta) => {
      const created = new Date(row.created);
      const updated = new Date(row.updated);
      return Math.round((updated-created)/1000);
      }
    }
  ]
});
<!doctype html>
<html>
<head>
  <script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script type="application/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
</head>
<body>
<table id="mytable"></table>
</body>
</html>