2017-10-07 4 views
0

AJAX 성공 후 데이터 테이블을 새로 고치는 방법을 여러 번 시도했지만 행운은 없습니다. 나는 draw()와 .ajax.reload() 함수를 시도했지만 여전히 운이 없다. 그것을 새로 고치는 방법을 알고 있습니까? 여기 아약스 성공 후 어떻게 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> 

입니다

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

편집 : 나는 나의 자바 스크립트 스크립트를 업데이트하지만 난 할 수 내 문제를 해결하지 못합니다.

답변

0

성공적으로 테이블

table.ajax.reload();//where the table variable is the variable that holds the DataTable itself 
를 다시로드

- ".의 DataTable을 재 초기화 할 수 없습니다이 오류에 대한 자세한 내용은 것은, http://datatables.net/tn/3 참조하십시오 테이블 ID = giacenza DataTables 경고 :"지금은 아직 유사한 오류 메시지가
var table = $('#giacenza').DataTable({ 

//ajax call that fetches data from the database. 

}); 

또 다른 아이디어는 데이터베이스에서 레코드를 가져 오는 javascript 기능을 갖는 것입니다. 데이터베이스에서 삭제를 성공한 후에 함수를 다시 호출하십시오. 당신은 mysqli를 사용하는

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 
} 

편집 할 수 있습니다. 이 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() 함수 나에게이 오류 반환 : "DataTables 경고 : 테이블 ID = giacenza - 잘못된 JSON 응답이 오류에 대한 자세한 내용은 다음을 참조하십시오 HTTP :. /을 /datatables.net/tn/1 " –

+0

해결하려면. 테이블 변수에는 데이터베이스에서 데이터를 가져 오는 Ajax 호출이 있어야합니다. 이것이 Datatable가 다시로드 할 목록을 알고있는 방식입니다. – Akintunde007

+0

업데이트 된 답변 확인 – Akintunde007