解决bootstrap table footerFormatter表脚和表体列错位问题

需求:需要把表格的一列的内容在表最下面一行进行汇总,显示合计

实现合计步骤:

1.在初始化表格时,开启显示表脚的属性,如图

 2.在需要合计的列中添加footerFormatter函数,如图

这样就会在表格最下面显示合计,效果如下

 

但是注意看黄色箭头所指的地方,发现合计列和表格主体列错位了 

解决这个问题可以这样办:在表格初始化中加入一个函数onLoadSuccess,这个函数表示是数据加载成功后执行的函数,在这个函数中写入以下内容:

onLoadSuccess:function(data) {//该方法解决了表脚不能和表体列对齐的问题
    //表格表体的td集合
    var body=$("#table_data thead tr th");
    //表脚表格的表头td集合
    var footer=$(".bootstrap-table .fixed-table-container .fixed-table-footer table thead tr th");
    //把表体的td宽度赋值给表脚表格的td
    body.each(function(){
        footer.width($(this).width());
    })
    //刷新视图
    $('#table_data').bootstrapTable('resetView');
}

注意:把红色的table_data替换为你自己的表格的id

下面给出html和js代码

html内容:表格

<table id="table_data"></table>

js内容:初始化bootstrap table

$('#table_data').bootstrapTable('destroy').bootstrapTable({
    classes: "table table-bordered table-hover", //设置表格样式
    //******服务器端分页设置****
    url: "url", //服务器返回数据的网址
    method: 'post',   //数据请求方式
    contentType: 'application/x-www-form-urlencoded',//method为post时,必须设置contentType为application/x-www-form-urlencoded
    sidePagination: "server",                              // 设置在服务端还是客户端分页
    cache: false,                                          // 是否使用缓存
    clickToSelect:true,//点击行选中复选框
    pagination: true,                                      // 是否显示分页
    search: false,                                         // 是否有搜索框
    pageNumber: 1,                                         // 首页页码,默认为1
    pageSize: 60,                                           // 页面数据条数
    pageList: [20, 40, 60, 80, 100, 1000],
    //checkboxHeader:false, //隐藏表头中的复选框
    detailView: true,  //是否显示子表
    queryParamsType: "",
    showFooter:true,//显示表格脚
    queryParams: function (params) {//默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort
        // queryParamsType设置为 '' 在这种情况下传给服务器的参数为:pageSize,pageNumber
        return {
            pageSize: params.pageSize,                     // 每页记录条数
            pageNumber: params.pageNumber,                 // 当前页索引
            month: $('#input_month').val(),
            supplierCode: $('#select_supplierCode').val(),
            departmentId: JSON.stringify($('#select_departmentId').val())
        };
    },
    formatLoadingMessage: function () {
        return "请稍后,正在加载";
    },
    formatNoMatches: function () {
        return "暂无匹配数据";
    },
    height: 456,//窗口高度
    onLoadSuccess:function(data) {//该方法解决了表脚不能和表体列对齐的问题
        //表格表体的td集合
        var body=$("#table_data thead tr th");
        //表脚表格的表头td集合
        var footer=$(".bootstrap-table .fixed-table-container .fixed-table-footer table thead tr th");
        //把表体的td宽度赋值给表脚表格的td
        body.each(function(){
            footer.width($(this).width());
        })
        //刷新视图
        $('#table_data').bootstrapTable('resetView');
    },
    columns: []
})
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap 中,可以使用固定表头和滚动表体的插件来实现这个效果。以下是实现步骤: 1. 引入必要的文件: ``` <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> ``` 2. 创建一个表格,并在表格头部添加 `thead` 标签: ``` <table class="table table-bordered table-striped"> <thead> <tr> <th>列头1</th> <th>列头2</th> <th>列头3</th> </tr> </thead> <tbody> <tr> <td>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</td> </tr> <!-- more rows --> </tbody> </table> ``` 3. 使用 JavaScript 初始化表格,并调用插件: ``` $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); ``` 其中,`fixedHeaderTable` 是插件名称,`fixedColumn: true` 表示需要固定表头。 4. 根据需要调整样式: ``` .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } ``` 其中,`.table-fixed-header` 是插件自动生成的类名,可以根据需要进行修改。 完整代码如下: ``` <!DOCTYPE html> <html> <head> <title>Bootstrap Table 表头固定表体滚动</title> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <style> .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } </style> </head> <body> <div class="container"> <table class="table table-bordered table-striped table-fixed-header"> <thead> <tr> <th>列头1</th> <th>列头2</th> <th>列头3</th> </tr> </thead> <tbody> <tr> <td>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</td> </tr> <!-- more rows --> </tbody> </table> </div> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fixed-header-table@1.3.2/dist/fixed-header-table.min.js"></script> <script> $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值