我正在使用 jquery 的 datatables 表插件。我有另一个使用 Ajax 从服务器检索对象的组件。我想用这个对象更新数据库。我正在努力思考如何将其拼凑在一起。 Ajax 返回一个对象,该对象的格式是数据表接受的数据格式。但是如何从另一个组件 Ajax 调用更新数据表呢?我正在使用 python Flask 和 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();
});
最佳答案
这是一个非常简单的“欺骗”ajax 调用的方法。
转到http://live.datatables.net/nesadivo/1/edit
单击 Run With 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);
}
});
});
关于javascript - 来自另一个组件的数据表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161109/