1. 程式人生 > >Bootstrap Table 定時重新整理固定滾動條的位置

Bootstrap Table 定時重新整理固定滾動條的位置

場景:一張內容很大的表格,每隔一段時間重新整理資料,使用者看資料的時候突然重新整理了,由於重新整理後滾動條彈到頂部,這時客戶再找剛才看的內容,就比較困難了,如何解決了?

思路:首先獲取滾動條的位置,然後定時向後臺請求資料的時候,把獲取的滾動條的位置設成滾動到的位置。

主要程式碼:

var scollPostion = $('#tableTest1').bootstrapTable('getScrollPosition');

$('#tableTest1').bootstrapTable('scrollTo', scollPostion);  注意此程式碼要在setTimeout裡面執行,原因是重新獲取資料後還要生成dom節點,需要時間

完整的程式碼:

<table class="table-striped table-hasthead" id="tableTest1" data-search="true">
    <thead>
        <tr>
            <th data-sortable="true" data-field="id">Id</th>
            <th data-field="name">Name</th>
            <th data-sortable="true" data-field="url">Website</th>
            <th data-field="alex">Texa</th>
            <th data-field="country">Country</th>
        </tr>
    </thead>
</table>
    $(function() {
        var url = "selectBtTable.php?action=init_data_list";
        $('#tableTest1').bootstrapTable({
            height: $(window).height() - 460,
            url: url
        });
        setInterval(refreshData, 3000)
        function refreshData() {
            var scollPostion = $('#tableTest1').bootstrapTable('getScrollPosition');
            $('#tableTest1').bootstrapTable('refresh', { silent: true, url: "selectBtTable.php?action=init_data_list" });
            setTimeout(function() {
                $('#tableTest1').bootstrapTable('scrollTo', scollPostion);
            }, 60);
        }
        $(window).resize(function() {
            $('#tableTest1').bootstrapTable('resetView');
        });
    });

微信公眾號:前端之攻略  ,定時更新前端有關知識。