Jquery http://datatables.net/tn/3 :无法重新初始化数据表
我想对数据表中来自ajax的数据执行行分组。 这是我的html代码:-Jquery http://datatables.net/tn/3 :无法重新初始化数据表,jquery,html,datatable,jquery-datatables,Jquery,Html,Datatable,Jquery Datatables,我想对数据表中来自ajax的数据执行行分组。 这是我的html代码:- <html> <head> <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.0-beta.2/css/jquery.dataTables.css"> <link rel="stylesheet" type="text/css" href="NewFile.css"> <
<html>
<head>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.0-beta.2/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="NewFile.css">
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.0-beta.2/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function() {
$('#example').dataTable( {
"ajax": 'arrays.txt'
} );
} );
</script>
<script>
</script>
<script>
$(document).ready(function() {
var table = $('#example').DataTable({
"columnDefs": [
{ "visible": false, "targets": 2 }
],
"order": [[ 2, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(2, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
}
} );
// Order by the grouping
$('#example tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
table.order( [ 2, 'desc' ] ).draw();
}
else {
table.order( [ 2, 'asc' ] ).draw();
}
} );
} );
</script>
</body>
</html>
当我尝试运行时,会收到错误消息DataTables warning:table id=example无法重新初始化datatable。有关此错误的详细信息,请参阅
那么错误在哪里呢
*******************************************更新****************************************
现在,如果我加入2个javascript代码,我不会得到错误,数据表会被填充,但行分组不会发生。那么我的错误在哪里呢?请帮忙!!!!我的代码:-
<html>
<head>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.0-beta.2/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="NewFile.css">
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>SECTION</th>
<th>REPORT NAME</th>
<th>CONTACT PERSON</th>
<th>LINK TO DOWNLOAD</th>
<th>METRICS</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.0-beta.2/js/jquery.dataTables.js"></script>
<!-- <script>
$(document).ready(function() {
$('#example').dataTable( {
"ajax": 'arrays.txt'
} );
<!-- } );
</script>
<script>
</script> -->
<script>
$(document).ready(function() {
$('#example').dataTable( {
"ajax": 'arrays.txt'
} );
table.destroy();
var table = $('#example').DataTable({
"columnDefs": [
{ "visible": false, "targets": 2 }
],
"order": [[ 2, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(2, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
}
} );
// Order by the grouping
$('#example tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
table.order( [ 2, 'desc' ] ).draw();
}
else {
table.order( [ 2, 'asc' ] ).draw();
}
} );
} );
</script>
</body>
</html>
tn/3消息是因为您试图初始化同一个表两次。尝试在与其他函数相同的函数中执行数据加载。大概是这样的:
$(document).ready(function() {
table = $('#example').DataTable({
"ajax": "arrays.txt",
"columnDefs": [
{ "visible": false, "targets": 2 }
],
"order": [[ 2, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(2, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
}
} );
// Order by the grouping
$('#example tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
table.order( [ 2, 'desc' ] ).draw();
}
else {
table.order( [ 2, 'asc' ] ).draw();
}
} );
} );
$(document).ready(function() {
table = $('#example').DataTable({
"ajax": "arrays.txt",
"columnDefs": [
{ "visible": false, "targets": 2 }
],
"order": [[ 2, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(2, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
}
} );
// Order by the grouping
$('#example tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
table.order( [ 2, 'desc' ] ).draw();
}
else {
table.order( [ 2, 'asc' ] ).draw();
}
} );
} );