bootstrap table插件的分页与checkbox使用详解
作者:心似一片青苔
这篇文章主要为大家详细介绍了bootstrap table插件的分页与checkbox使用详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
今天需要实现这样一个功能,有checkbox列表可供选择,要选择不分页之间的行并保存
最终实现的功能如图:(图片来自网上) 

具体实现
首先,来看具体的代码,这里只截取实现功能所需代码
  var selectionIds =[],selectionNames=[];
  var curd = {
    init:function(){
      this._getCheckParam();
    },
    /**
     * 初始化
     * @private
     */
  /**
   * 表格操作
   */
  //表格分页之前处理多选框数据
  _responseHandler:function()(res) {
    $.each(res.rows, function (i, row) {
      row.checkStatus = $.inArray(row.id, selectionIds) != -1; //判断当前行的数据id是否存在与选中的数组,存在则将多选框状态变为true
    });
    return res;
  },
    _list:function(){
      var _this = this
      var settings = {
        url:Path.searchUrl,
        method:'GET',
        responseHandler:_this.responseHandler, //在渲染页面数据     之前执行的方法
        height:Path.tbheight
      };
      bsTable.initTable("#boostrapTable",settings);
      // 其它的boostrapTable参数已经封装在bsTable里面了,这里就不贴出来了
    },
    /**
     * 获取选中ID
     * @returns {*}
     * @private
     */
    _getIdSelections:function() {
        // 用map进行过滤
        return $.map($('#bootstrapTable').bootstrapTable('getSelections'), function (row) {
          return row.id
        });
      },
    /**
     * 获取选中对象并显示
     * @private
     */
    _getCheckParam:function(){
      var union = function(array,ids){
        $.each(ids, function (i, id) {
          if($.inArray(id,array)==-1){
            array[array.length] = id;
          }
        });
        return array;
      };
      //取消选中事件操作数组
      var difference = function(array,ids){
        $.each(ids, function (i, id) {
          var index = $.inArray(id,array);
          if(index!=-1){
            array.splice(index, 1);
          }
        });
        return array;
      };
      var _ = {"union":union,"difference":difference};
      var $table=$('#bootstrapTable');
      //绑定选中事件、取消事件、全部选中、全部取消
      $table.on('check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table', function (e, rows) {
        var ids = $.map(!$.isArray(rows) ? [rows] : rows, function (row) {
          return row.id;
        });
        var names = $.map(!$.isArray(rows) ? [rows] : rows, function (row) {
          return row.name;
        });
        func = $.inArray(e.type, ['check', 'check-all']) > -1 ? 'union' : 'difference';
        selectionIds = _[func](selectionIds, ids);
        selectionNames =_[func](selectionNames,names);
      });
    }
  };
      return curd;
});
比较常用的技巧
使用boostrapTable时候,选择表格的行,返回的rows有很多,这时候需要过滤出我们需要的字段,可以用
 function getIdSelections() {
    return $.map($table.bootstrapTable('getSelections'), function (row) {
      return row.id
      // 想返回什么字段就换成什么
    });
  }
当然,如果需要对选出的数据有限制筛选,用filter过滤也不错
var arr = [1,2,3,4,5,4,3,2,1];
var filterResult = arr.filter(function(item,index,array){
  return (item>2);
});
console.log(filterResult);  
//[3,4,5,4,3],返回所有数值都大于2的一个数组
相关讨论
如果想了解更多细节,可以看看GitHub上的issue
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- bootstrap table表格插件之服务器端分页实例代码
 - bootstrap table 服务器端分页例子分享
 - bootstrap-table后端分页功能完整实例
 - Bootstrap table分页问题汇总
 - 第一次动手实现bootstrap table分页效果
 - BootStrap中Table分页插件使用详解
 - BootStrap Table前台和后台分页对JSON格式的要求
 - bootstrap table分页模板和获取表中的ID方法
 - 使用bootstraptable插件实现表格记录的查询、分页、排序操作
 - bootstrap-table实现服务器分页的示例 (spring 后台)
 - BootStrap Table后台分页时前台删除最后一页所有数据refresh刷新后无数据问题
 - Bootstrap table 服务器端分页功能实现方法示例
 
