bootstrap table onPostBody表格渲染前事件

onPostBody是在bootstrap table表格的body渲染之后执行的事件,该事件参数返回需要渲染的data数据。

onPostBody事件

返回参数如下。

参数名称 说明
data 需要渲染的data数据

onPostBody事件绑定方式一

$('#table').bootstrapTable({
   columns: columns,
   data: data, 
    onPostBody:function(data)
    { 
     alert(JSON.stringify(data));
    }
});

onPostBody事件绑定方式二

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

$('#table').on('post-body.bs.table', function (e,data){
   alert(JSON.stringify(data));
});

完整代码

<!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>
    <title>bootstrap table onPostBody表格渲染后事件例子</title>
    <style>
        .table-demo {
            width: 80%;
            margin: 60px 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 class="table-demo">
        <table id="table"></table>
    </div>
    <script>
        //设置需要显示的列
        var columns = [  {
            field: 'Id',
            title: '序号'
        }, {
            field: 'Model',
            title: '型号'
        }, {
            field: 'Num',
            title: '库存'
        }];
 
      
        //bootstrap table初始化数据
        $('#table').bootstrapTable({
            columns: columns,
            data: getData(),
            classes: "table table-bordered table-striped table-sm ", //设置表格样式
            height: 500,
            onPostBody:function(data)
           { 
            alert(JSON.stringify(data));
             }
        });

        function getData() {
            var data = [];
            for (var i = 0; i < 50; i++) {
                var item = {
                    Id: i,
                    Model: 'P0' + i,
                    Num: i + 5
                };
                data.push(item);
            };
            return data;
        }
    </script>
</body>
</html>

在线试一试