感謝每一位回覆。我想通過this
<div class="filter">
Search Data: <input type="text" id="filtertext" placeholder="Filter...">
</div>
<h3><font color='red'>Table1 Data</font></h3>
<table id="table1" style="background-color: #edf1fa"
class="display compact cell-border">
</table>
<br>
<h3><font color='green'>Table2 Data</font></h3>
<table id="table2" style="background-color: #edf1fa"
class="display compact cell-border">
</table>
<br>
<h3><font color='salmon'>Table3 Data</font></h3>
<table id="table3" style="background-color: #edf1fa"
class="display compact cell-border">
</table>
<br>
<h3><font color='violet'>Table4 Data</font></h3>
<table id="table4" style="background-color: #edf1fa"
class="display compact cell-border">
</table>
<script type="text/javascript">
$(document).ready(function() {
buildHeader();
var table1 = $('#table1').DataTable({
"sDom": 'rt',
"pagingType": "full_numbers",
"ordering": false,
"fixedHeader": true,
"bScrollCollapse": true
});
var table2 = $('#table2').DataTable({
"sDom": 'rt',
"pagingType": "full_numbers",
"ordering": false,
"fixedHeader": true,
"bScrollCollapse": true
});
var table3 = $('#table3').DataTable({
"sDom": 'rt',
"pagingType": "full_numbers",
"ordering": false,
"fixedHeader": true,
"bScrollCollapse": true
});
var table4 = $('#table4').DataTable({
"sDom": 'rt',
"pagingType": "full_numbers",
"ordering": false,
"fixedHeader": true,
"bScrollCollapse": true
});
$("#filtertext").on('keyup', function (e) {
table.search(this.value).draw();
});
initialzeTables();
setInterval(function() {
initialzeTables();
}, 30000);
function buildHeader() {
var rowStr = "<thead>"
+ "<tr>"
+ "<th>name</th>"
+ "<th>Age</th>"
+ "<th>DOJ</th>"
+ "<th>Dept</th>"
+ "</tr>"
+ "</thead>";
$("table.display ").append(rowStr);
}
function initialzeTables() {
$.ajax({
url: "jsonsource.json",
dataType: 'json',
type: 'GET',
data: {
"date" : getCurrentDate()
},
success: function (result) {
handleResponse(result);
}
});
}
function handleResponse(result) {
table1Id = $("#table1");
table2Id = $("#table2");
table3Id = $("#table3");
table4Id = $("#table4");
table1.destroy();
table2.destroy();
table3.destroy();
table4.destroy();
table1 = displayData(result.data1, table1Id, table1);
table2 = displayData(result.data2, table2Id, table2);
table3 = displayData(result.data3, table3Id, table3);
table4 = displayData(result.data4, table4Id, table4);
}
function displayData(data, tableSelector, datatable) {
datatable = tableSelector.DataTable({
"sDom": 'rt',
"pagingType": "full_numbers",
"ordering": false,
"fixedHeader": true,
"bScrollCollapse": true,
"data": data,
"columns": [
{"data": "name"},
{"data": "age"},
{"data": "doj"},
{"data": "dept"},
],
"columnDefs": [
{
'targets': [0],
'orderable': false,
'render': function (data, type, row, meta) {
data = data != null ? data : ' ';
return '<a href="/URL?name=' + data +'">' + data + '</a>';
}
},
]
});
return datatable;
}
function getCurrentDate() {
return $.datepicker.formatDate('yy-mm-dd', new Date());
}
});
</script>
你如何處理result.data2,result.data3等?你如何解釋這一點? – madalinivascu
爲什麼不使用'DataTable'的''ajax''屬性,然後使用DataTable的API中的'.ajax.reload()'方法每30秒重新載入一次表的數據?看到這個鏈接:[https://datatables.net/examples/ajax/simple.html](https://datatables.net/examples/ajax/simple.html) – gabriel
'$ .fn.getCurrentDate( )'?或者,換句話說,你下載了一個jQuery擴展,它給你當前的日期......?這有用嗎?另外,如果這樣做的目的是爲了防止瀏覽器緩存Ajax響應,那麼可以採用不同的方式。 – Tomalak