我有一个从ajax源获取数据的数据表,它目前在第4列下降。但是,我有一个数据数组,我想订购不作为列的表。
oTable = $('#Table').DataTable({
"scrollX": true,
stateSave: false,
"processing": true,
"serverSide": false,
"ajax": "scripts/SSP_enquiry.php",
select: true,
colReorder: true,
"columnDefs": [
{
"visible": false,
"searchable": false,
"targets": [9, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
},
{className: "word", "targets": [1]}
],
order: [4, 'desc'],
dom: '<"top">lrt<"bottom"pB><"clear">',
responsive: false,
buttons: [
'excel'
]
});
我的函数使用Moment.JS对表中的日期(存在于第4列)和今天的日期进行比较。然后它将天数差异推到名为dArray
的数组中。
我的数组为每个值都有一个索引,该索引应该与表中的行相关。
所以我想要做的是使用索引和值根据dArray
function dateGet() {
var idx = oTable
.columns( 4 )
.data()
.eq( 0 );// Reduce the 2D array into a 1D array of data
//console.log(idx);
var dArray = [];
var today = moment();
$.each(idx, function(index,value) {
var tempDate = moment(value);
var deadlineDiff = tempDate.diff(today, 'days');
dArray.push(index,deadlineDiff);
console.log(dArray);
})
}
dArray
[0, 6]
[1, 0]
[2, -0]
[3, -0]
[4, -1]
[5, -6]
[6, -7]
[7, -7]
[8, -11]
[9, -12]
[10, -12]
[11, -12]
[12, -13]
更新
我添加了一个新列,默认值为0,所以我现在需要根据截止时间差值数组来分配值。他们需要匹配相应行的ID。
result[j][0]
将为我提供数组中的ID
result[j][1]
将为我提供数组中的差值
function dateGet() {
var dArray = [];
var today = moment();
var ID = oTable
.columns(0)
.data()
.eq(0);
var dDate = oTable
.columns(4)
.data()
.eq(0);// Reduce the 2D array into a 1D array of data
$.each(dDate, function (index, value) {
var tempDate = moment(value);
var deadlineDiff = Math.abs(tempDate.diff(today, 'days'));
dArray.push(deadlineDiff);
});
var result = $.map(ID, function (el, idx) {
return [[el, dArray[idx]]];
});
oTable.rows().every(function (rowIdx, tableLoop, rowLoop) {
var data = this.data();
for (var j = 0; j < result.length; j++) {
if (data[0] == result[j][0]) {
//in here the ID in the table and the ID in the array
// match, I need to iterate through rows and assign the value
//in the array to column 28's cell
}
}
});
}
答案 0 :(得分:1)
这样的事情应该做你需要的事情(假设你希望差异能够发挥作用而不管消极或积极:
$(function() {
var example = $("#example").DataTable({
columns: [{
"title": "Released Date",
"type": "int-diff",
"render": function(d){
var diff = Math.abs(moment().diff(moment(d, "DD/MM/YYYY"), 'days'));
return $("<span></span>",{
"text": d + " (" + diff + ")",
"data-diff": diff
}).prop("outerHTML");
}
}]
});
});
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"int-diff-pre": function(a) {
return ~~$(a).data("diff");
},
"int-diff-asc": function(a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"int-diff-desc": function(a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
给出一个这样的表:
<table id="example">
<thead>
<tr>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>01/04/2016</td>
</tr>
<tr>
<td>02/04/2016</td>
</tr>
<tr>
<td>03/04/2016</td>
</tr>
<tr>
<td>04/04/2016</td>
</tr>
<tr>
<td>05/04/2016</td>
</tr>
<tr>
<td>06/04/2016</td>
</tr>
<tr>
<td>07/04/2016</td>
</tr>
<tr>
<td>08/04/2016</td>
</tr>
<tr>
<td>09/04/2016</td>
</tr>
<tr>
<td>10/04/2016</td>
</tr>
<tr>
<td>11/04/2016</td>
</tr>
<tr>
<td>12/04/2016</td>
</tr>
<tr>
<td>13/04/2016</td>
</tr>
<tr>
<td>14/04/2016</td>
</tr>
</tbody>
</table>
工作JSFiddle。基本上我们正在做的是将今天的日期和单元格中的日期之间的差异作为跨度中的正整数作为数据属性,其中日期是跨度的内容。然后我们使用该数据属性进行排序而不是跨度的内容。
希望有所帮助。