我有以下代码可使用datatables.net插件创建数据表:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js'></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link type="text/css" href="https://cdn.datatables.net/1.10.3/css/jquery.dataTables.css" />
<link type="text/css" href="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.css" />
<script src="https://cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<script type='text/javascript' src="https://datatables.net/release-datatables/extensions/KeyTable/js/dataTables.keyTable.js"></script>
</head>
<body>
<script type="text/javascript">
// function day2Date( day, year ) {
// return new Date(year,0,day);
//}
$(document).ready(function() {
$('#example').dataTable({
"ajax": "table1.php",
"columns": [{
"data": "ID"
}, {
"data": "naziv"
}, {
"data": "vrsta"
},
],
"columnDefs": [{
"targets": 2,
"data": "download_link",
"render": function(data, type, full, meta) {
// return data;
return '<button class="btn btn-success">' + data + '</button>';
}
}]
});
});
var table = $('#example').DataTable();
$(document).ready(function() {
$('#example tbody').on('click', 'td', function() {
console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
// alert('Row:'+$(this).parent().find('td').html().trim());
//alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());
});
$('#example tbody').on('click', 'tr', function() {
console.log('Row index: ', table.row(this).index());
});
});
</script>
<div class="container">
<table id="example" class="table table-striped table-bordered table-responsitive" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Naziv</th>
<th>Vrsta</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Naziv</th>
<th>Vrsta</th>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
我需要获取行索引,因此我可以从上面的代码中看到:
$('#example tbody').on( 'click', 'tr', function () {
console.log( 'Row index: '+table.row( this ).index() );
就像我在datatables网站上的文档中看到的那样,但是此代码仅返回我 [object Object]
例子:
Data:12Row:2Column:Naziv
Row index: [object Object]
为什么?有人有解释吗?
您已经在任何DOM ready处理程序之外但在发生该事件的元素之前包含了一行关键代码。这意味着$('#example')
不返回匹配项:
将此行放在DOM ready处理程序中:
var table = $('#example').DataTable();
例如
$(document).ready(function () {
var table = $('#example').DataTable();
$('#example tbody').on('click', 'td', function () {
console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
// alert('Row:'+$(this).parent().find('td').html().trim());
//alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());
});
$('#example tbody').on('click', 'tr', function () {
console.log('Row index: ', table.row(this).index());
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句