数据表:实时添加额外的行吗? [英] Datatables: Adding an extra row on real time?
问题描述
我正在尝试在表加载后向其添加新行(因为我需要检索一些数据),但是新行仅显示未定义".看起来像这样:
I am trying to add a new row to a table, after it is loaded (since I need to retrieve some data), but the new row just shows 'undefined'. It looks like this:
$('#example tr').each( function () {
id = this.id.substr(4);
var result2;
if (id.length > 0) {
$.post('get_stuff.php', {id: id}, function(result) {
result2 = result;
});
oTable.fnOpen( this, result2, "info_row_");
}
} );
以上内容打开新行并在其中写入"undefined".但是,如果在fnOpen调用之前添加了alert(result2)
,则结果将显示在警报中,然后写入该行.我该如何解决?
The above opens the new rows and writes 'undefined' in them. If, however, before the fnOpen call I add an alert(result2)
the result is shown in the alert and then written to the row. How can I solve this?
推荐答案
您的$.post()
请求是异步的.
因此,信息仍在被请求时被写入.
So the info is written while it is still being requested.
您可以添加:
$.ajaxSetup({async:false});
或将.ajax()
与async: false
选项一起使用.
$.ajaxSetup({async:false});
before the .post()
call or use .ajax()
with async: false
option.
.post()
只是.ajax()
或者您也可以在.post()
函数的成功回调中编写vakue.
Or you could write the vakue in the success callback of the .post()
function.
$.post('get_stuff.php', {id: id}, function(result) {
//result2 = result; // don't know if you still need the result2 var somewhere else. If that's the case you should use one of the other approaches (as stated above)
oTable.fnOpen( this, result, "info_row_");
});
这篇关于数据表:实时添加额外的行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!