DataTables 复选框无法正确显示
DataTables Checkboxes cannot show properly
我已经创建了 jQuery 个 DataTables 复选框,现在我的问题是我无法在数据 table 中向 select 显示多个复选框。我试图为 jQuery DataTables 库添加一个复选框扩展,它提供了一个通用的解决方案来处理 table 中的复选框。但错误显示消息是 Script error
希望有人能指出我遇到的错误。谢谢
下面是我的示例代码:
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/css/dataTables.checkboxes.css" />
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>2</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>3</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
这是我的预期结果:
此代码有效。
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>2</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>3</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
我已经创建了 jQuery 个 DataTables 复选框,现在我的问题是我无法在数据 table 中向 select 显示多个复选框。我试图为 jQuery DataTables 库添加一个复选框扩展,它提供了一个通用的解决方案来处理 table 中的复选框。但错误显示消息是 Script error
希望有人能指出我遇到的错误。谢谢
下面是我的示例代码:
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/css/dataTables.checkboxes.css" />
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>2</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>3</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
这是我的预期结果:
此代码有效。
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>2</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>3</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>