ajax成功后如何刷新DataTable?

时间:2017-10-07 10:35:52

标签: javascript php ajax datatable

在AJAX成功之后,我尝试了不同的方法来刷新我的数据表,但没有运气。我尝试了draw()和.ajax.reload()函数,但仍然没有运气。你知道怎么刷新吗?

这是我的代码

HTML

minikubeIP:nodeport

的JavaScript

<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>

PHP

$(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);
            }
        });
    });
});

编辑: 我更新了我的JavaScript脚本,但无法解决我的问题。现在我仍然收到类似的错误:“DataTables警告:table id = giacenza - 无法重新初始化DataTable。有关此错误的详细信息,请参阅http://datatables.net/tn/3

1 个答案:

答案 0 :(得分:0)

成功重新加载表格

table.ajax.reload();//where the table variable is the variable that holds the DataTable itself 

示例

var table = $('#giacenza').DataTable({

//ajax call that fetches data from the database. 

});

另一个想法是拥有一个从数据库中获取记录的javascript函数。从数据库中删除成功后,再次调用该函数。

示例

function getRecords(){
//this gets the full list from the database on document ready 
//make Ajax call to retrieve from database 
}

if(result == 'OK'){
//its been successfully deleted 
getRecords();//call the js function that gets from database again
}

EDIT 你正在使用mysqli。此API支持预准备语句。请使用..

$query = "UPDATE records SET PARESTALLA = PARESTALLA-1 WHERE CODBARRA = ?";

$result = $mysqli->prepare($query); 
$result->bind_param('s', $codbarra); 
$result->execute();
echo $result->affected_rows > 0 ? 'OK' : 'KO';