0
我试过不同的方式来刷新我的数据表后AJAX成功,但没有运气。我尝试了draw()和.ajax.reload()函数,但仍然没有运气。你有什么想法如何刷新它?如何在ajax成功后刷新DataTable?
这里是我的代码
HTML
<table id="giacenza" class="table table-striped table-bordered">
<thead>
<tr>
<th>Modello</th>
<th>Descrizione</th>
<th>Colore</th>
<th>Taglia</th>
<th>Barcode</th>
<th>Quantità</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
的JavaScript
$(document).ready(function() {
function getRecords(){
$.ajax({
type: "POST",
url: "functions/getImported.php",
success: function(result) {
var table = $('#giacenza').DataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdfHtml5', 'print'
],
order: [[ 1, "asc" ]],
});
var data = JSON.parse(result);
table.rows.add(data).draw();
},
error: function(result) {
alert("error: "+result);
}
});
}
getRecords();
$("#scarica-articolo").submit(function(event) {
event.preventDefault();
var codbarra = $("#codbarra").val();
$("#scarica-btn").attr("disabled",true);
$.ajax({
type: "POST",
url: "functions/scaricaArticolo.php",
data: {codbarra:codbarra},
success: function(result) {
if(result = "OK"){
new PNotify({
title: 'Articolo scaricato!',
text: 'L\'articolo è già stato scaricato dalla tabella seguente!',
type: 'success',
styling: 'bootstrap3'
});
$("#scarica-btn").attr("disabled",false);
getRecords();
}else if(result == "KO"){
new PNotify({
title: 'Oh No!',
text: 'La query non è andata a buon fine. Contattare l\'assistenza.',
type: 'error',
styling: 'bootstrap3'
});
$("#scarica-btn").attr("disabled",false);
}
},
error: function(result) {
alert("error: "+result);
}
});
});
});
PHP
<?php
include("db_config.php");
$mysqli->set_charset("utf8");
$codbarra = $mysqli->real_escape_string($_POST["codbarra"]);
$query = "UPDATE records SET PARESTALLA = PARESTALLA-1 WHERE CODBARRA = ".$codbarra.";";
$results = array();
$result = $mysqli->prepare($query);
$result->execute();
if($result->affected_rows > 0){
echo "OK";
}else{
echo "KO";
}
?>
编辑: 我更新了我的JavaScript脚本,但我可以没有解决我的问题。现在,我仍然收到类似的错误:“数据表警告:表ID = giacenza - 不能重新初始化数据表中有关此错误的详细信息,请参阅http://datatables.net/tn/3”
table.ajax.reload()函数返回我这个错误:“数据表警告:表ID = giacenza - 无效JSON响应有关此错误的详细信息,请参阅HTTP:/ /datatables.net/tn/1“ –
修复它。表变量必须具有从数据库中提取数据的Ajax调用。这就是Datatable如何知道要重新加载的列表。 – Akintunde007
检查更新的答案 – Akintunde007