0
我在使用jQuery中的.each
方法將數據添加到我的表中時出現問題,然後實例化DataTable
後綴。查看頁面時,數據將顯示在表格中,而不顯示數據表的過濾選項。使用.each方法時數據表沒有使用數據實例化
如果還有更好的方法,我不需要使用.each()
方法。這正是當時對我最有意義的東西。
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" ></script>
<script src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" >
<script src="js/users.js"></script>
</head>
<body>
<table id="table">
<thead>
<tr>
<th>id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
<script>
$(document).ready(function() {
$('tbody').append($('<tr>', {id: "test"}));
var $arr_length = users.length;
for(i = 0; i <= $arr_length; i++) {
var $row = $('tr').get(i+1);
$.each(users[i], function(index, value) {
$($row).append("<td>" + value + "</td>");
});
$($row).after($('<tr>', {id: "test"}));
};
$('#table').DataTable();
});
</script>
</html>
如果你想添加新行,請按照下列步驟操作: - https://datatables.net/reference/api/row.add() –
我需要能夠使用數組中的數據填充表,然後我需要實例化將數據表放到該預先存在的表上。 –
爲什麼不把數據傳遞給數據庫並讓它處理呢? – Bindrid