【发布时间】:2017-06-24 20:35:10
【问题描述】:
如何使用 JavaScript 以编程方式选择 Bootgrid 表中的所有行?
我在操作栏中添加了一个按钮,并且我有一个函数循环遍历 JQuery Bootgrid 表中的所有行。
到目前为止,我正在获取表中的所有行 id (data-row-id),甚至在 bootgrid 表中的其他页面上也是如此。
如何从这里选择所有行并以编程方式保持选择?
谢谢
代码如下:
// JS
<script>
$(document).ready(function () {
var count = 0;
var dt = $("#table1").bootgrid({
selection: true,
multiSelect: true,
keepSelection: true,
rowSelect: true
}).on("selected.rs.jquery.bootgrid", function (e, rows) {
var rowIds = [];
for (var i = 0; i < rows.length; i++) {
count++;
}
$('#NumSelected').html(count);
console.log(count);
}).on("deselected.rs.jquery.bootgrid", function (e, rows) {
var rowIds = [];
for (var i = 0; i < rows.length; i++) {
count--;
}
$('#NumSelected').html(count);
console.log(count);
});
var rows = dt.data('.rs.jquery.bootgrid').rows;
// Get all Ids in the table and log
for(var i = 0; i < rows.length; i++)
{
console.log(rows[i].ID);
}
// append button in action bar
$(".bootgrid-header .actionBar").find('.actions.btn-group').append('<button id="selectAll" class="btn btn-default" type="button" title="Select All Clients Listed"><span class="icon glyphicon glyphicon-saved"></span></button>');
// Select all rows in table
// This is not working, I have data paginated in 8 pages (80 rows)
// and the code below only select the data in the first page
// I want to select all rows in the table and keep the selection by using jquery bootgrid
$("#selectAll").click(function () {
$('#table1 tbody > tr').each(function() {
$(this).addClass('active').attr('aria-selected', true);
$(this).find(":checkbox").prop('checked', true);
});
// get all selected rows id in array
var selectedRowsId = $("#table1").bootgrid('getSelectedRows');
console.log(selectedRowsId);
});
});
</script>
// html
<table id="table1">
<thead>
<tr>
<th data-column-id="ID" data-identifier="true" data-type="numeric">ID</th>
<th data-column-id="Name" data-type="string">Name</th>
<th data-column-id="OtherDetails" data-type="string">Other Details</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.OtherDetails</td>
</tr>
}
</tbody>
</table>
【问题讨论】:
-
发布您的代码。
-
编辑:发布代码
标签: javascript jquery jquery-bootgrid