bootstrap table scrollTo滚动到指定位置

bootstrap table scrollTo滚动滚动条到指定位置,可以设置px或者行的索引来实现,参数 object或value二选一。

scrollTo方法

参数名称
value需要滚动到的位置,单位为px,如果设置为bottom表示滚动到底部
object{unit: 'px', value: 100}  滚动到100px位置
{unit: 'rows', value: 6}  滚动到第6行

代码例子

//滚动到100px的位置,测试后好像没有效果
$('#table').bootstrapTable('scrollTo',100);
//滚动到底部 有效
$('#table').bootstrapTable('scrollTo','bottom');

//滚动到800px的位置,测试后有效果但是感觉不准确
 $('#table').bootstrapTable('scrollTo',{unit: 'px', value: 800});
 //滚动到第8行 这个方法最准确
 $('#table').bootstrapTable('scrollTo',{unit: 'rows', value: 8});

在线试一试 

完整例子

<!doctype html>
<html>
<head>
       <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="https://www.itxst.com/package/jquery-3.3.1/jquery.js"></script>
    <link href="https://www.itxst.com/package/bootstrap-4.3.1/css/bootstrap.css" rel="stylesheet" />
    <link href="https://www.itxst.com/package/bootstrap-table-1.15.3/bootstrap-table.css" rel="stylesheet" />
    <script src="https://www.itxst.com/package/bootstrap-table-1.15.3/bootstrap-table.js"></script> 
    <title>bootstrap table scrollTo在线例子</title>
    <style>
        .table-demo {
            width: 80%;
            margin: 30px auto 0px auto;
        }
        .fixed-table-header {
            border-right: solid 1px #ddd;
            border-top: solid 1px #ddd;
        }
            .fixed-table-header table  {
                border-top: solid 0px #ddd !important;
                margin-top:-1px;
            }
    </style>
</head>
<body>
   <div id="toolbar">
    <button onclick="f1()">滚动到第8行</button> 
    <button onclick="f2()">滚动到500px</button> 
    <button onclick="f5()">获取当前位置</button> 
  </div>
    <div class="table-demo">
        <table id="table"></table>
    </div>
    <script>
        //设置需要显示的列
        var columns = [{
            field: 'Id',
            title: '编号'
        }, {
            field: 'ProductName',
            title: '名称'
        }, {
            field: 'StockNum',
            title: '库存'
        }];
 
        //bootstrap table初始化数据
        $('#table').bootstrapTable({
            toolbar:"#toolbar", 
            columns: columns,
            data: getData(),
           
           height:500, 
           pagination:true,
           pageNumber:1,
           pageSize:100,  
        });

       function getData()
        {
            var data = [];
            for (var i = 0; i < 300; i++)
            {
                var item = {
                    Id: i,
                    ProductName: 'iphone',
                    StockNum:i+10000
                };
                data.push(item);
            };
            return data;
        }

     function f1()
      {
       $('#table').bootstrapTable('scrollTo',{unit: 'rows', value: 8});
      }
      function f2()
      {
      $('#table').bootstrapTable('scrollTo',{unit: 'px', value: 500});
      }
      
      function f5()
      {
       var pos= $('#table').bootstrapTable('getScrollPosition');
       alert(pos);
      }
    </script>
</body>
</html>