bootstrap table onResetView视图切换事件

onResetView是在bootstrap table表格切换视图后执行的事件,bootstrap table有两种视图模式,一种是列表方式,一种是cardView卡片视图模式,如果不清除视图是什么概念请点击网页底部的试一试按钮在线测试看看。

onResetView事件绑定方式一

$('#table').bootstrapTable({
   columns: columns,
   data: data, 
   onResetView:function()
    { 
     alert("你切换了视图模式");
    }
});

onResetView事件绑定方式二

注意:pre-body.bs.table方式需要的加载数据之前绑定,否则第一次加载数据进行渲染将不会生效

$('#table').on('reset-view.bs.table',function (e){
  alert("你切换了视图模式");
});

完整代码

<!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/bootstrap-table-1.14.1/jquery-3.3.1/jquery.js"></script>
    <link href="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-4.3.1/css/bootstrap.css" rel="stylesheet" />
    <link href="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-table-1.14.1/bootstrap-table.css" rel="stylesheet" />
    <script src="https://www.itxst.com/package/bootstrap-table-1.14.1/bootstrap-table-1.14.1/bootstrap-table.js"></script>
      <link href="https://www.itxst.com/package/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
    <title>bootstrap table onResetView视图切换事件例子</title>
    <style>
        .table-demo {
            width: 80%;
            margin: 30px auto 0px auto;
        }
    </style>
</head>
<body>
  <div id="tb">
    <button onclick="swView()">切换视图</button>
  </div>
    <div class="table-demo">
        <table id="table"></table>
    </div>
    <script>
        //设置需要显示的列
        var columns = [{
            field: 'Id',
            title: '编号'
        }, {
            field: 'ProductName',
            title: '产品名称'
        }, {
            field: 'StockNum',
            title: 'Item 库存'
        }];


        //需要显示的数据
        var data = [{
            Id: 1,
            ProductName: '香蕉',
            StockNum: '100'
        }, {
            Id: 2,
            ProductName: '苹果',
            StockNum: '200'
        }];
        //bootstrap table初始化数据
        $('#table').bootstrapTable({
            columns: columns,
            data: data,
            toolbar:"#tb",
            cardView:true,
            onResetView:function(){
            alert("你切换了视图");
            }
        });
      
    //$('#table').on('reset-view.bs.table',function (e){
    // alert("你切换了视图模式");
    //});
      
      function swView()
      {
       
       var ops=$('#table').bootstrapTable('getOptions');
       
       if(ops.cardView==true)
       {
       $('#table').bootstrapTable('refreshOptions',{cardView:false});
       }
       else
        {
        $('#table').bootstrapTable('refreshOptions',{cardView:true});
        }
      }
    </script>
</body>
</html>

在线试一试