我得到的Json如下:
[
{
"id":1,
"repDate":{
"offset":{
"totalSeconds":7200,
"id":"+02:00",
"rules":{
"fixedOffset":true,
"transitions":[
],
"transitionRules":[
]
}
},
"nano":880042000,
"year":2018,
"monthValue":4,
"dayOfMonth":25,
"hour":12,
"minute":58,
"second":53,
"month":"APRIL",
"dayOfWeek":"WEDNESDAY",
"dayOfYear":115
},
"hashrate":5114926.0
},
...more entries
]
我必须以特定格式显示日期:yyyy.mm.dd - hh.mm.ss.nnn
,所以我想创建一个自定义变量,但我不确定在哪里。这是我的JS函数,用于检索json
并设置DataTables
。我尝试在columnDefs
中创建一个字符串,但这不起作用。
var table;
$(document).ready(function() {
table = $('#main-table').DataTable({
ajax: {
url: '/refresh',
dataSrc:''
},
paging: true,
lengthChange: false,
pageLength: 20,
stateSave: true,
info: true,
searching: false,
"columnDefs": [
{
"className": "text-center",
"targets": 0,
"data": "id",
},
{
"className": "text-center",
"targets": 1,
"data": "repDate.year" + "." + "repDate.monthValue" + "." + "repDate.dayOfMonth",
},
{
"className": "text-center",
"targets": 2,
"data": "hashrate",
}
],
"aoColumns": [
{ "orderSequence": [ "asc", "desc" ] },
{ "orderSequence": [ "asc", "desc" ] },
{ "orderSequence": [ "desc", "asc" ] }
],
"order": [[ 0, "asc" ]]
});
});
setInterval(function(){
table.ajax.reload(null, false);
}, 8000);
答案 0 :(得分:0)
感谢您的回答。在render
选项后,似乎function
效果很好。这是一个更正的代码段:
"columnDefs": [
{
"className": "text-center",
"targets": 0,
"data": "id",
},
{
"className": "text-center",
"targets": 1,
"data" : function(data){
var seconds = data.repDate.second < 10 ? seconds = "0" + data.repDate.second : seconds = data.repDate.second;
var minutes = data.repDate.minute < 10 ? minutes = "0" + data.repDate.minute : minutes = data.repDate.minute;
var months = data.repDate.monthValue < 10 ? months = "0" + data.repDate.monthValue : months = data.repDate.monthValue;
var days = data.repDate.dayOfMonth < 10 ? days = "0" + data.repDate.dayOfMonth : days = data.repDate.dayOfMonth;
return data.repDate.year + "-" + months + "-" + days + " " + data.repDate.hour + ":" + minutes + ":" + seconds;
},
},
{
"className": "text-center",
"targets": 2,
"data": function(data){
return data.hashrate/1000.0;
},
}
]