如何使用Bootstrap-Table将纯文本字符串解析为Unix时间

时间:2016-01-07 07:51:07

标签: html json twitter-bootstrap bootstrap-table

我在其中一张桌子中使用this JSON file。如您所见,其中一个JSON对象是logon,它为登录VATSIM网络的人提供了纯文本字符串。

this site上显示的我的表格代码如下:

<table data-toggle="table" data-url="http://api.vateud.net/online/pilots/eg.json" data-cache="false" data-show-refresh="true" data-query-params="queryParams" data-pagination="true" data-search="true" data-page-list="5, 10, 25, 50, 100, All" data-height="400"
            data-sort-name="callsign" data-sort-order="asc">
                <thead>
                    <tr>
                        <th data-field="callsign" data-halign="center" data-align="center" data-sortable="true">Callsign</th>
                        <th data-field="name" data-halign="center" data-align="center" data-sortable="true">Name</th>
                        <th data-field="aircraft" data-halign="center" data-align="center" data-sortable="true">Aircraft</th>
                        <th data-field="origin" data-halign="center" data-align="center" data-sortable="true">Departure Airport</th>
                        <th data-field="destination" data-halign="center" data-align="center" data-sortable="true">Arrival Airport</th>
                        <th data-field="flight_type" data-halign="center" data-align="center" data-sortable="true">Type</th>
                        <th data-field="route" data-halign="center" data-align="center" data-sortable="true">Route</th>
                        <th data-field="altitude" data-halign="center" data-align="center" data-sortable="true">Altitude</th>
                        <th data-field="groundspeed" data-halign="center" data-align="center" data-sortable="true">Groundspeed</th>
                        <th data-field="transponder" data-halign="center" data-align="center" data-sortable="true">Squawk</th>
                        <th data-field="logon" data-halign="center" data-align="center" data-sortable="true">Logon Time</th>
                    </tr>
                </thead>
            </table> 

有没有办法使用Bootstrap-Tables将纯文本字符串解析为格式化的日期和时间戳,然后将其注入表中?

提前致谢。

1 个答案:

答案 0 :(得分:4)

尝试data-Formatter选项。

示例HTML:

<th data-field="name" data-formatter="nameFormatter">Name</th>

示例JS:

function nameFormatter(value) {
    return '<a href="https://github.com/wenzhixin/' + value + '">' + value + '</a>';
}

参考网址:
http://jsfiddle.net/n7s43toq/

对于您的情况,下面是示例/模型格式方法。

function stringToDate (value) {
  var date = new Date(value*1000);
  // Hours part from the timestamp
  var hours = date.getHours();
  // Minutes part from the timestamp
  var minutes = "0" + date.getMinutes();
  // Seconds part from the timestamp
  var seconds = "0" + date.getSeconds();

  // Will display time in 10:30:23 format
  return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
}