2017-04-01 41 views
0

我正在爲jquery使用datatables表插件。我有另一個使用Ajax從服務器檢索對象的組件。我想用這個對象更新數據庫。我正在努力如何將這一塊拼湊在一起。 Ajax返回一個對象,該對象採用數據表爲數據接受的格式。但是,我如何從另一個組件Ajax調用更新數據表?我正在使用python燒瓶和jinja2模板。這是目前存在的JavaScript:從另一個組件進行數據表更新

$(function() { 
    var container = document.getElementById('visualization'); 
    var items = new vis.DataSet({{documents|safe}}); 
    var options = {}; 
    var timeline = new vis.Timeline(container, items, options); 

    timeline.on('select', function (properties) { 
     $.getJSON('/getDependencyHistory', { 
      uuid: properties.items[0] 
     }, function(data) { 
      console.log("Place this into the datatable"); 
     }); 
     return false; 
    }); 
}); 

$(document).ready(function() { 
    var table = $('#example').DataTable(); 
}); 
+0

是來自getJSON的數據全表更新還是隻更新某些行?目前,我假設選擇框上的每個更改都會使用新數據完全重新填充數據表。 – Bindrid

+0

btw,$(document).ready(function()和$(function(){)正在做同樣的事情,所以你實際上可以用其他代碼移動數據表。 – Bindrid

回答

1

這裏是一個非常簡單的「愚弄」ajax調用。

http://live.datatables.net/nesadivo/1/edit

點擊運行方式JS按鈕來初始化一切。點擊go按鈕去獲取數據

$(document).ready(function() { 
    // created a global variable for the datatable to us to find the data 
    var dtData = null; 

    // On the button click, use regular ajax to get the data 
    $("#btnGo").on("click", function(){ 
     $.ajax({url:"http://live.datatables.net/examples/server_side/scripts/server_processing.php", 

      success:function(cData){ 
        // on success, set the global variable then reload table 
        dtData = JSON.parse(cData); 
        $('#example').DataTable().ajax.reload(); 
       }, 
       error:function(err){debugger;}} ); 
     }); 

     // initialize the table on page load 
     $('#example').DataTable({ 
      "ajax": function(a,callback,c){ 
       callback(dtData); 
      } 
     }); 
    });