我正在开发一个使用MySQL,HTML5,jQuery和Json的工具。在我的网站上我有3个表,但其中一个必须转置。所以我写这个:
$(document).ready(function () {
$('#druckerdetails').dataTable({
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"bAutoWidth": false,
"bProcessing": true,
"bServerSide": false,
"sAjaxSource": 'php/index_druckerdetails.php?druckername=RAGPLM002'
});
$(function () {
var table = $('#druckerdetails');
alert('Besten Dank, dass Sie isyPrint benutzen :)');
table.find('thead tr').detach().prependTo(table.find('tbody'));
var t = table.find('tbody').eq(0);
var r = t.find('tr');
var cols = r.length;
var rows = r.eq(0).find('td,th').length;
var cell, next, tem, i = 0;
var tb = $('<tbody></tbody>');
while (i < rows) {
cell = 0;
tem = $('<tr></tr>');
while (cell < cols) {
next = r.eq(cell++).find('td,th').eq(0);
tem.append(next);
}
tb.append(tem);
++i;
}
table.find('tbody').remove();
$(tb).appendTo(table);
$(table)
.find('tbody tr:eq(0)')
.detach()
.appendTo(table.find('thead'))
.children();
table.show();
});
});
使用此警报一切正常并且有效,因为php文件有足够的时间返回Json-String。但是如果没有警报,JavaScript不会等待MySQL查询的php数据。因此,网站上缺少数据。
没有提醒: http://www.computerbase.de/forum/attachment.php?attachmentid=359923&d=1377067918
所以这是时间轴(isyprint_home.js&amp; index_druckerdetails.php): http://www.computerbase.de/forum/attachment.php?attachmentid=359927&d=1377072271
那么我要做的是,js文件等到json-string被返回?
谢谢,抱歉我的英语不好
答案 0 :(得分:1)
您可以使用延迟渲染:
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "sources/arrays.txt",
"bDeferRender": true
} );
} );
如果数据表无法解决您的问题,我建议您从分离的ajax调用中获取json feed,并在完成后将其放入数据表中。像这样:
var feed;
$.ajax({
type: 'POST',
url: 'www.test.com/mydatatablefeed',
success: function(data){
feed = data;
}
});
$('#example').dataTable( {
"aaData": feed
} );
} );
答案 1 :(得分:0)
使用ajaxComplete或数据表回调,在实际返回数据时应该调用它。 您还可以在加载数据失败时向事件添加回调,以便您可以通知用户有关失败的信息。
答案 2 :(得分:0)
ajax源用于异步加载数据,因此需要这种行为。您需要将“转置”代码链接到加载数据的通知,或者在加载数据时对其进行修改。就个人而言,我会在返回JSON的PHP中解决这个“问题”,因此它以它理解的格式提供表格数据。
但是如果你想使用它,那么回调函数可以覆盖数据提取:http://datatables.net/usage/callbacks
fnServerData 此参数允许您覆盖从服务器获取数据的默认函数($ .getJSON),以便更适合您的应用程序。例如,您可以使用POST数据,或从Gears或AIR数据库中提取信息。
以下示例说明如何使用数据执行其他处理:http://datatables.net/examples/server_side/custom_vars.html