当添加一行时,数据表滚动到底部
jQuery Datatables scroll to bottom when a row is added
我希望我的DataTable在添加一行时滚动到底部。我已经尝试了多个修复这个问题,但没有一个工作。
测试解决方案:
- 如何在数据表中加载表,并在加载时自动滚动到最后记录 Jquery DataTable auto scroll to bottom on load
等等……
我认为我的案例与其他案例的区别在于,我使用的是DataTable
, D
大写。
总之,这是我当前的代码:
var table = $('#example').DataTable({
"createdRow": function( row, data, dataIndex )
{
$(row).attr('id', 'row-' + dataIndex);
},
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bInfo": false,
"bAutoWidth": false,
"scrollY": $(window).height()/1.5,
"scrollCollapse": true,
"paging": false,
});
for(var i = 1; i <= 20; i++){
table.row.add([
i,
'action'+i,
]);
}
table.draw();
table.rowReordering();
每当添加新行时,如果表滚动到底部就好了。
SOLUTION
要滚动到表格底部,使用下面的代码:
var $scrollBody = $(table.table().node()).parent();
$scrollBody.scrollTop($scrollBody.get(0).scrollHeight);
演示$(document).ready( function () {
var table = $('#example').DataTable({
"createdRow": function( row, data, dataIndex ) {
$(row).attr('id', 'row-' + dataIndex);
console.log($(row).closest('table').parent());
},
"scrollY": $(window).height()/1.5,
"scrollCollapse": true,
"paging": false
});
$('#btn-add').click(function(){
for(var i = 1; i <= 10; i++){
table.row.add([
i,
i + '.2',
i + '.3',
i + '.4',
i + '.5',
i + '.6'
]);
}
table.draw();
// Scroll to the bottom
var $scrollBody = $(table.table().node()).parent();
$scrollBody.scrollTop($scrollBody.get(0).scrollHeight);
});
table.rowReordering();
} );
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>jQuery DataTables</title>
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<script src="https://cdn.rawgit.com/mpryvkin/jquery-datatables-row-reordering/95b44786cb41cf37bd3ad39e39e1d216277bd727/media/js/jquery.dataTables.rowReordering.js"></script>
</head>
<body>
<button id="btn-add" type="button">Add records</button>
<table id="example" class="display" 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>
<tbody>
</tbody>
</table>
</body>
</html>
相关文章:
- 用程序搜索JQuery数据表中的文本
- 具有rowGrouping的数据表无法隐藏列
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 将单击事件添加到数据表
- 使用带有参数的数据表sAjaxSource
- 对角度数据表中括号内的数字进行排序
- jquery数据表滚动条定位
- 蒲公英数据表滚动X没有'不起作用
- 数据表垂直滚动问题
- 数据表 Javascript 显示水平滚动条
- 数据表在页面更改时滚动顶部
- 滚动到 iframe 中数据表的顶部
- 顶部水平滚动条数据表javascript
- 如何使Bootstrap jQuery数据表响应和增加/减少滚动条高度根据页面的大小
- jQuery数据表和jScrollPane -表头不滚动
- 滚动到选定的行在数据表jQuery插件
- 数据表水平滚动条将显示在顶部而不是底部
- 当添加一行时,数据表滚动到底部
- coldfusion中数据表的无限滚动
- 如何使用动态数据制作滚动表