【发布时间】:2020-04-03 18:29:42
【问题描述】:
如何在 DataTables 中自定义查看更多详细信息按钮?是的,我知道 DataTables 已经有一个查看更多详细信息按钮,但我想要一个自定义设计的按钮。
我希望在单击自定义视图更多按钮时显示子行。
这是我一直在尝试使用的代码,使用 '.table-toggle' 作为类名,以将其激活到我应用此类名的任何内容。
以下是脚本:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.20/r-2.2.3/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.20/r-2.2.3/datatables.min.js"></script>
这是我的 HTML 代码:
<table class="table">
<thead>
<tr>
<th class="never">ID</th>
<th>Category</th>
<th>Amount</th>
<th class="none">Date</th>
<th class="desktop">Time</th>
<th class="desktop">Balance </th>
<th class="none">Payment Type</th>
<th class="none">Note</th>
<th class="desktop">Action</th>
</tr>
</thead>
<tbody>
<tr class="table-toggle">
<td>1</td>
<td>
<h5>Charles Smith</h5>
<span>Lending</span>
</td>
<td>
-$500.00
</td>
<td>02-January-2020</td>
<td></i> 11:01 am </td>
<td>$67,256.00</td>
<td>Cash</td>
<td>Collected and deposited</td>
<td>
<a href="javascript:void(0)" class="table-toggle">View More</a>
<a href="javascript:void(0)"></a>Edit</span>
<a href="javascript:void(0)"></a>Delete</span>
</td>
</tr>
</tbody>
</table>
这是我的 Javascript 代码:
<script>
// Add event listener for opening and closing details
$('.table tbody').on('click', '.table-toggle', function () {
var tr = $(this).closest('tr');
var table = $('.table').DataTable();
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
</script>
这是我在按钮上的 HTML 代码
<a href="javascript:void(0)" class="table-toggle">View More</a>
【问题讨论】: