2017-10-07 81 views
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

回答

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 
} 

編輯 您正在使用的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'; 
+0

table.ajax.reload()函數返回我這個錯誤:「數據表警告:表ID = giacenza - 無效JSON響應有關此錯誤的詳細信息,請參閱HTTP:/ /datatables.net/tn/1「 –

+0

修復它。表變量必須具有從數據庫中提取數據的Ajax調用。這就是Datatable如何知道要重新加載的列表。 – Akintunde007

+0

檢查更新的答案 – Akintunde007