javascript - 如何更改自定义 div 中/表外的数据表导出按钮的位置
问题描述
我想在表外显示导出按钮。
我在 stackoverflow 中看到了一个示例,但这是一种选择选项方法,请参阅该示例链接
如果有人知道该怎么做,请修改它并分享 jsfiddle 链接。
这是我使用 bootstrap4 数据表的代码。
Javasript
$(document).ready(function () {
$("#datatable").DataTable(),
$("#datatable-buttons")
.DataTable( {
lengthChange: !1,
buttons: [
{
extend: 'copy',
text: '<i class="fas fa-copy"></i>',
titleAttr: 'Copy',
className: 'btn btn-md mr-2 btn-copy'
},
{
extend: 'excel',
text: '<i class="fas fa-file-excel"></i>',
titleAttr: 'Excel',
className: 'btn btn-md mr-2 btn-excel'
},
{
extend: 'pdf',
text: '<i class="fas fa-file-pdf"></i>',
titleAttr: 'PDF',
className: 'btn btn-md mr-2 btn-pdf'
},
{
extend: 'print',
text: '<i class="fas fa-print"></i>',
titleAttr: 'Print',
className: 'btn btn-md mr-2 btn-print'
},
{
extend: 'colvis',
text: '<i class="fas fa-eye"></i>',
titleAttr: 'Visibility',
className: 'btn btn-md mr-2 btn-colvis'
},
],
lengthChange: true,
searching: true
})
.buttons()
.container()
.appendTo("#datatable-buttons_wrapper .col-md-6:eq(0)");
});
html代码
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<table id="datatable-buttons" class="table table-striped table-hover table-bordered dt-responsive nowrap"
style="border-collapse: collapse; border-spacing: 0; width: 100%;">
<thead class="thead-dark">
<tr class="text-center">
<th>Id</th>
<th>User Image</th>
<th>Username</th>
<th>Email</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php foreach ($userdetails as $key => $userdetail): ?>
<tr>
<td><?php echo $userdetail['id']; ?></td>
<td><?php echo $userdetail['user_image']; ?></td>
<td><?php echo $userdetail['username']; ?></td>
<td><?php echo $userdetail['email']; ?></td>
<td><?php echo $userdetail['role']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div>
</div> <!-- end col -->
</div> <!-- end row -->
解决方案
您给定的代码几乎可以appendTo()
在 Javascript 的最后一行之外开箱即用。将目标从更改#datatable-buttons_wrapper .col-md-6:eq(0)
为任何有效appendTo()
目标会将按钮移动到适当的位置。请参阅jQuery 的文档appendTo()
。您还可以使用其他 DOM 操作方法,例如prependTo()
.
这是一个工作示例,其中表格按钮位于外部 div 中。
推荐阅读
- c# - 调用不同的基本构造函数时如何避免构造函数之间的重复
- javascript - Django 渐进式 Web 应用程序在缓存文件后在所有浏览器中都出现错误
- django - Django下一页和上一页不起作用
- rest - 在 microsoft 团队中发送用户通知
- entity-framework - Update database in Entity Framework Core
- apache-kafka - 是否可以在 Kakfa Streams 中使用相同的状态存储来创建“快速”队列和“慢速”队列?
- google-sheets - 有或有条件的 SUMIFS
- javascript - 数据变化时主题变化 AmCharts
- c# - C# 使用其他方法的更新数组 - 创建控制台游戏“Nim”
- vhdl - vhdl 中没有实际的常量接口