bootstrap table onToggle切换视图事件

当bootstrap table开启搜索输入框,用户输入关键词时的事件,返回text关键词字符串,需要把showToggle属性设置为true才生效。

onToggle 事件

返回参数如下。

参数名称说明
cardView卡片状态 true卡片视图 false表格视图

绑定onToggle事件方法一

$('#table').bootstrapTable({
     columns: columns,
     data: data,
     cardView :true, 
     showToggle:true, //1,注意这里必须设置true,onToggle事件才会生效
     onToggle:function(cardView)
      { 
    alert(JSON.stringify(cardView));
}

绑定onToggle事件方法二

$('#table').on('toggle.bs.table', function (e,cardView){
   alert(JSON.stringify(cardView));
});

完整代码

<!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 onToggle切换视图事件例子</title>
    <style>
        .table-demo {
            width: 60%;
            margin: 60px auto 0px auto;
        }
    </style>
</head>
<body>
    <div class="table-demo">
        <table id="table"></table>
    </div>
    <script>
        //设置需要显示的列
        var columns = [{
            field: 'Id',
            title: 'ID'
        }, {
            field: 'ProductName',
            title: '名称'
        }, {
            field: 'StockNum',
            title: '数量'
        }];


        //需要显示的数据
        var data = [{
            Id: 1,
            ProductName: '甘蔗',
            StockNum: '12'
        }, {
            Id: 2,
            ProductName: '西瓜',
            StockNum: '30'
        }];
        //bootstrap table初始化
        $('#table').bootstrapTable({
            columns: columns,
            data: data,
            cardView :true, 
            showToggle:true,
            onToggle:function(cardView)
           { 
             alert(JSON.stringify(cardView));
           }
        });
    
    </script>
</body>
</html>

在线试一试