【发布时间】:2019-02-25 21:23:56
【问题描述】:
我有一个错误:
无效的 JSON 响应
我的问题是我无法重新加载/刷新我的DataTable,除了使用以下方法之外还有什么办法:
$('#selected').load('mypage.php #selected') // 刷新表格但分页/按钮/搜索栏消失
$('#selected').DataTable( {
dom: 'Bfrtip',
buttons: [
'print', 'excel', 'pdf'
]
} );
$(document).ready(function() {
<?php
include('../config/dbconn.php');
mysqli_query($dbconn, 'DELETE FROM temp_trans');
?>
$(document).on('click','a[data-role=add1]', function() {
window.onbeforeunload = function() {
return "Data will be lost if you leave the page, are you sure?";
};
var transnum = '';
if(transnum == '') {
transnum = genId();
}
var id= $(this).data('id');
var qty = 1;
var price = $('#' + id).children('td[data-target=price]').text();
price = parseInt(price.substr(1, price.length));
$.ajax({
url: 'temp_trans.php',
method: 'get',
data: {
id: id,
transnum: transnum,
qty: qty,
price: price
},
success: function() {
table = $("#selected").DataTable();
table.ajax.reload(null, false);
}
});
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="selected">
<thead>
<tr>
<th>Transaction Number</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include('../config/dbconn.php');
$sql = "SELECT * FROM `temp_trans` inner join `products` on temp_trans.prod_id = products.prod_id";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) { ?>
<tr id="<?php echo $row['prod_id'] ?>">
<td data-target="sid"><?php echo $row['trans_num']; ?></td>
<td data-target="sname"><?php echo $row['prod_name']; ?></td>
<td data-target="sqty"><?php echo $row['qty']; ?></td>
<td data-target="sprice"><?php echo utf8_encode('₱'). $row['price']; ?></td>
<td data-target="stotal"><?php echo utf8_encode('₱'). number_format($row['price'] * $row['qty'],2); ?></td>
<td><a data-role="update" data-id="<?php echo $row['prod_id'];?>" style="color: orange; font-weight: bold;">Update</a></td>
</tr>
<?php }
}
?>
</tbody>
</table>
【问题讨论】:
标签: javascript php jquery datatables