【发布时间】:2011-04-27 21:03:11
【问题描述】:
我在 2 个选项卡下有 2 个表格。默认选项卡下的表格很好地显示了数据表。第二个表是从 Ajax 源(JSON 数组)填充的。问题是我无法使用 DataTables 初始化此表。
$(document).ready(function() {
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
if(activeTab == "#tab2"){
$.getJSON("<url>",
function(data) {
htmlTable = '<table id="table2" cellspacing="1" cellpadding="1" border="1">';
htmlTable+=<data...>;
htmlTable +='</tbody></table>';
$('#table2').remove();
$('#tab2').append(htmlTable);
});
}
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
});
如果我在 getJSON() 调用的成功函数中显式初始化,我会在每次单击选项卡时看到多个这样的 DataTable。如何初始化这个dataTable...
【问题讨论】:
-
我通过显式初始化 dataTables 在检查是否存在并删除它之后修复了这个问题:'if ($("#table2_wrapper").length > 0){ $( '#table2_wrapper').remove(); }'
标签: jquery ajax datatables