【Bootstrap Table】表格列合并 && 设置表格内容为勾选框

效果:1.列合并:

1.1写在$('#bootstrap-table').bootstrapTable中

        onLoadSuccess: function () {//当所有数据被加载时触发处理函数
            var data = $('#bootstrap-table').bootstrapTable('getData', true);//获取当前页数据
            mergeCells(data,'number',1,$('#bootstrap-table')); //列名
            mergeCells(data,'item',1,$('#bootstrap-table'));
            mergeCells(data,'severity',1,$('#bootstrap-table'));
        },
        onPageChange: function (){//当页面更改页码或页面大小时触发
            var data = $('#bootstrap-table').bootstrapTable('getData', true);
            mergeCells(data,'number',1,$('#bootstrap-table'));
            mergeCells(data,'item',1,$('#bootstrap-table'));
            mergeCells(data,'severity',1,$('#bootstrap-table'));
        },

1.2写在$('#bootstrap-table').bootstrapTable外面

function mergeCells(data,fieldName,colspan,target){
        //声明一个map计算相同属性值在data对象出现的次数和
        var sortMap = {};
        for(var i = 0 ; i < data.length ; i++){
            for(var prop in data[i]){
                if(prop == fieldName){
                    var key = data[i][prop]     //fieldName的value
                    if(sortMap.hasOwnProperty(key)){
                        sortMap[key] = sortMap[key] * 1 + 1;
                    } else {
                        sortMap[key] = 1;
                    }
                    break;
                }
            }
        }
        //合并单元格
        var index = 0;
        for(var prop in sortMap){
            var count = sortMap[prop] * 1;
            $(target).bootstrapTable('mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count});
            index += count;
        }
    }

2.设置勾选框(在columns中)

            {
                field: 'problem',
                title: '检查问题',
                width: '50px',
                formatter: function (value, row , index) {
                    return '<input type="checkbox"/> ';
                }
            },

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在使用Bootstrap Table时,在第一添加勾选框,可以通过定义自定义的方式来实现。 首先,确保你已经引入了BootstrapBootstrap Table的相关CSS和JavaScript文件。 然后,在HTML中创建一个表格,并在第一添加一个勾选框,同时给表格添加一个唯一的ID用于后续操作。例如: ```html <table id="myTable" data-toggle="table" data-select-item-name="selectedItems"> <thead> <tr> <th data-checkbox="true"></th> <th data-field="name">姓名</th> <th data-field="age">年龄</th> <th data-field="email">邮箱</th> </tr> </thead> <tbody> <!-- 表格数据 --> <tr> <td></td> <td>John Doe</td> <td>25</td> <td>[email protected]</td> </tr> <tr> <td></td> <td>Jane Smith</td> <td>30</td> <td>[email protected]</td> </tr> </tbody> </table> ``` 在上述代码中,我们在第一使用了`data-checkbox="true"`来添加勾选框。同时,我们在表格上使用了`data-select-item-name="selectedItems"`来定义勾选框的名称。 最后,在JavaScript中初始化表格,并获取勾选的行数据。例如: ```javascript $(document).ready(function() { $('#myTable').bootstrapTable(); // 获取勾选的行数据 var selectedRows = $('#myTable').bootstrapTable('getSelections'); console.log(selectedRows); }); ``` 在上述代码中,我们使用`bootstrapTable`函数来初始化表格,然后使用`getSelections`方法获取勾选的行数据。你可以根据实际需求,将获取到的数据用于后续操作。 通过以上步骤,你就可以在使用Bootstrap Table时,在第一添加勾选框了。记得根据实际情况配置表格的其他和数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值