因此,我试图像文档中那样使用Datatable实现多个选项卡。 从服务器端,我有如下数据。 会话数数组,例如:session1 session2。 对于每个会话,它将具有诸如名称,年龄之类的列。 现在,如果我仅通过一个会话,则数据表将起作用。 将会话数组传递到我的pug ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我想将数据表与动态加载的jquery选项卡结合起来,但不知道该怎么做。
这是我的代码:
index.jsp :
$(document).ready(function() {
$( "#tabs" ).tabs({
beforeLoad: function( event, ui ) {
ui.jqXHR.fail(function() {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. "+
"If this wouldn't be a demo." );
});
}
});
$('#example').DataTable();
});
<div id="tabs">
<ul>
<li><a href="ajax/tab.jsp?a=1">Tab 1</a></li>
<li><a href="ajax/tab.jsp?a=2">Tab 2</a></li>
<li><a href="ajax/tab.jsp?a=3">Tab 3</a></li>
<li><a href="ajax/tab.jsp?a=4">Tab 4</a></li>
</ul>
</div>
tab.jsp :
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
</tr>
</tbody>
</table>
当我们单击Tab时,它将在动态div中加载ajax/tab.jsp?a=1
。
因此,每次单击选项卡时都会发生这种情况。
但是您的Datatable
代码只编写了一次,将在加载jsp
文件之前执行
因此,您的数据表不会显示。
解决这个问题
$('#example').DataTable();
每次单击选项卡 您可以使用Tabs的tabsbeforeload事件。
注意:
我添加了超时只是为了延迟事情,以便加载jsp。 如果您的jsp需要更多时间来加载,请尝试增加超时值。
码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Tabs - Default functionality</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
$(function () {
$("#tabs").tabs({
beforeLoad: function (event, ui) {
// load first time
setTimeout(function () {
$('#example').DataTable();
}, 30);
ui.jqXHR.fail(function () {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. " +
"If this wouldn't be a demo.");
});
}
});
// before tabload
$("#tabs").on("tabsbeforeload", function (event, ui) {
console.log("dd");
$("#example").remove(); // to avoide duplicate id as Datatable will not load for other Tabs
setTimeout(function () {
$('#example').DataTable();
}, 30);
});
});
</script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="ajax/tab.jsp?a=1">Tab 1</a></li>
<li><a href="ajax/tab.jsp?a=2">Tab 2</a></li>
<li><a href="ajax/tab.jsp?a=3">Tab 3</a></li>
<li><a href="ajax/tab.jsp?a=4">Tab 4</a></li>
</ul>
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.