You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap Table中获取勾选行索引的问题与解决方法

如何在Bootstrap Table勾选行时获取行索引?

我尝试通过check.bs.table事件获取Bootstrap Table中勾选行的索引,但该事件返回的参数里没有索引相关内容,当前代码如下:

$('#gridModalListeDossiers').on('check.bs.table', lang.hitch( this, function (row , $element) {
    console.log("row", row)
    console.log("elemnt :", $element)
}))

请问有没有其他方法可以获取勾选行的索引?


更新:已找到实现方式

通过先获取表格完整数据,再遍历定位选中数据的索引,代码如下:

$('#gridModalListeDossiers').on('check.bs.table', lang.hitch( this, function (row , $element) {
    var dataTable = $('#gridModalListeDossiers').bootstrapTable('getData')
    $.each(dataTable, function (i, rowData) {
        if (rowData.select_dossier === true) {
            $('#gridDossiers').bootstrapTable('check', i)
        }
    })
}))

更高效的优化方案

不需要遍历整个数据集,直接利用数组的indexOf方法获取选中行的索引:

$('#gridModalListeDossiers').on('check.bs.table', lang.hitch(this, function(row, $element) {
    var dataTable = $('#gridModalListeDossiers').bootstrapTable('getData');
    // 直接通过row对象获取其在数据数组中的索引
    var rowIndex = dataTable.indexOf(row);
    console.log("选中行索引:", rowIndex);
    // 示例:用索引勾选另一表格的对应行
    $('#gridDossiers').bootstrapTable('check', rowIndex);
}))

这个方法省去了遍历操作,当表格数据量较大时性能更优。


内容的提问来源于stack exchange,提问作者Achraf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:35:37