bootstrap table 方法

bootstrap table getAllSelections获取所有选中行数据的方法

getAllSelections 是一个bootstrap table一个经常用到的方法,该方法获取获取你所有选中行的数据,需要注意和getSelections方法的区别,getSelections不会返回包含搜索刷选后的选中的数据。
方法返回的对象格式如下:

[{"Id":1,"ProductName":"香蕉","StockNum":"100","checked":true},
{"Id":3,"ProductName":"车厘子","StockNum":"2010","checked":true}]

getAllSelections用法

如果使用的是前端分页模式,无效把属性maintainSelected设置为ture,getSelections也可以同时获取多页选中的数据。

//获取所有选中行的数据
 var rows = $('#table').bootstrapTable('getAllSelections ');
//rows选中行的数据对象数组
alert(JSON.stringify(rows));
// rows对象的格式
// [{"Id":1,"ProductName":"香蕉","StockNum":"100","checked":true},{"Id":3,"ProductName":"车厘子","StockNum":"2010","checked":true}]

服务器端分页getAllSelections 

服务器端分页如果需要同时获取多给分页选中的行数据,需要先设置保持状态属性。

//首先,设置maintainSelected :true开启保持分页状态
$('#table').bootstrapTable({
            url: '/package/bootstrap-table-1.14.1/data.json',
            pagination: true,
            search: true, 
            columns: columns,
            pageSize:2,
            maintainSelected :true,
            toolbar:"#toolbar"
 });
//获取选中的数据
var rows = $('#table').bootstrapTable('getAllSelections ');

在线试一试

完整代码

<!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-4.3.1/css/bootstrap.css" rel="stylesheet" />
    <link href="https://www.itxst.com/package/bootstrap-table-1.15.3/bootstrap-table.css" rel="stylesheet" />
    <script src="https://www.itxst.com/package/bootstrap-table-1.15.3/bootstrap-table.js"></script>
    <title>Bootstrap Table getAllSelections 例子</title>
    <style>
        .table-demo {
            width: 80%;
            margin: 30px auto 0px auto;
        }
    </style>
</head>
<body>
  <div id="toolbar">
    <button onclick="getSels()">获取所有选中项目 </button>
    <span>测试流程,先随便选中第1行,任何搜索框输入200再选中这行,点击按钮看结果</span>
  </div>
    <div class="table-demo">
        <table id="table"></table>
    </div>
    <script> 
       // 设置需要显示的列
        var columns = [{
            field :"checked", 
            title: '编号', 
            checkbox:true
        }, {
            field: 'ProductName',
            title: '名称'
        }, {
            field: 'StockNum',
            title: '库存'
        }];


        $('#table').bootstrapTable({
            url: '/package/bootstrap-table-1.14.1/data.json',
            pagination: true,//开启分页
            search: true, //开启刷选
            columns: columns,
            pageSize:2,
            maintainSelected :true,
            toolbar:"#toolbar"
        });
      
      function getSels()
      {
      var rows = $('#table').bootstrapTable('getAllSelections');
       alert(JSON.stringify(rows));
      }
 
    </script>
</body>
</html>


Catalog
bootstrap table方法 bootstrap table事件 bootstrap table配置 bootstrap table下载 基础方法 getOptions获取配置对象 refreshOptions刷新配置 getData获取表格数据方法 getSelections获取选中行方法 getAllSelections所有选中数据 load加载数据方法 mergeCells合并行和列 checkAll全选的方法 uncheckAll反选所有行方法 checkBy根据条件选中行 uncheckBy条件取消中行 refresh重新加载数据的方法 checkInvert反选的方法 destroy注销表格方法 resetView更新表格UI的方法 resetWidth重新适配宽度 showLoading显示加载提示 hideLoading隐藏加载提示 togglePagination隐藏显示分页 toggleFullscreen全屏显示 toggleView切换卡片视图 resetSearch重置搜索框 filterBy数据刷选过滤器 scrollTo滚动到指定位置 getScrollPosition获取当前位置 增删改操作 append追加数据到末尾方法 prepend追加数据到头部方法 remove删除数据的方法 removeAll删除所有数据 insertRow插入一行数据 updateRow更新行数据方法 getRowByUniqueId获取行数据 updateByUniqueId更新行数据 removeByUniqueId删除数据 updateCell更新单元格数据 updateCellByUniqueId更新数据 隐藏显示操作 showRow显示行 hideRow显示行 getHiddenRows获取隐藏的行 show/hide Column隐藏显示列 getVisibleColumns获取显示列 getHiddenColumns获取隐藏列 showAllColumns显示所有列 hideAllColumns隐藏所有列