0
我想使用嵌套的ajax調用來填充表,並且一旦表被構建,使用jQuery的dataTable插件來漂亮起來。使用jQuery的dataTable插件與嵌套的Ajax調用
我遇到的問題是操作順序問題。我什麼時候調用dataTable函數,以便我可以確定該表是在填充值後構建的?當我嘗試下面的代碼時,數據表是在構建行之前創建的。
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url:"http://totalrockregistration.com/feeds/bands.php",
dataType:"jsonp",
success: function(jsonData){
$.each(jsonData.bands, function(i,bands){
if (bands.barID == "<?php echo $_GET["barID"]; ?>"){
var songIdFromBandJson = bands.song;
var bandNameFromJson = bands.name;
var bandScoreFromJson = bands.score;
$.ajax({
url:"http://totalrockregistration.com/feeds/songs.php",
dataType:"jsonp",
success: function(songsJsonData){
$.each(songsJsonData.songs, function(i,songs){
if (songIdFromBandJson == songs.id){
var songName=(songs.name);
$("#leaderBoardTable tbody").append("<tr><td>"+bandNameFromJson+"</td><td>"+bandScoreFromJson+"</td><td>"+songName+"</td></tr>");
}
});
}
});
}
});
makeLeaderTable();
},
});
function makeLeaderTable(){
$('#leaderBoardTable').dataTable({
"aaSorting": [[ 1, "desc" ]],
"iDisplayLength": 50
});
}
});
</script>