jquery - 数据表上父行上方的子行
问题描述
我一直在处理数据表子行,我找到了数据表提供的示例
https://datatables.net/examples/server_side/row_details.html
问题是他们提供了要在父行下方生成的子行,但我的要求是在父行上方生成子行
有没有办法做到这一点。
我已经实现了父行下的子行如下
function format ( d ) {
return 'Full name: '+d.first_name+' '+d.last_name+'<br>'+
'Salary: '+d.salary+'<br>'+
'The child row can contain any data you wish, including links, images, inner tables etc.';
}
$(document).ready(function() {
var dt = $('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/ids-objects.php",
"columns": [
{
"class": "details-control",
"orderable": false,
"data": null,
"defaultContent": ""
},
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" }
],
"order": [[1, 'asc']]
} );
// Array to track the ids of the details displayed rows
var detailRows = [];
$('#example tbody').on( 'click', 'tr td.details-control', function () {
var tr = $(this).closest('tr');
var row = dt.row( tr );
var idx = $.inArray( tr.attr('id'), detailRows );
if ( row.child.isShown() ) {
tr.removeClass( 'details' );
row.child.hide();
// Remove from the 'open' array
detailRows.splice( idx, 1 );
}
else {
tr.addClass( 'details' );
row.child( format( row.data() ) ).show();
// Add to the 'open' array
if ( idx === -1 ) {
detailRows.push( tr.attr('id') );
}
}
} );
// On each draw, loop over the `detailRows` array and show any child rows
dt.on( 'draw', function () {
$.each( detailRows, function ( i, id ) {
$('#'+id+' td.details-control').trigger( 'click' );
} );
} );
} );
希望有人可以提供帮助。:-)
解决方案
试试下面的逻辑:在单击 tr 之前添加 tr,class="child"。检查单击的 tr 是否具有类details
,并相应地在 tr 之前添加或删除子项。
var tableData = {
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
{
"DT_RowId": "row_5",
"first_name": "Airi",
"last_name": "Satou",
"position": "Accountant",
"office": "Tokyo",
"start_date": "28th Nov 08",
"salary": "$162,700"
},
{
"DT_RowId": "row_25",
"first_name": "Angelica",
"last_name": "Ramos",
"position": "Chief Executive Officer (CEO)",
"office": "London",
"start_date": "9th Oct 09",
"salary": "$1,200,000"
},
{
"DT_RowId": "row_3",
"first_name": "Ashton",
"last_name": "Cox",
"position": "Junior Technical Author",
"office": "San Francisco",
"start_date": "12th Jan 09",
"salary": "$86,000"
},
{
"DT_RowId": "row_19",
"first_name": "Bradley",
"last_name": "Greer",
"position": "Software Engineer",
"office": "London",
"start_date": "13th Oct 12",
"salary": "$132,000"
},
{
"DT_RowId": "row_28",
"first_name": "Brenden",
"last_name": "Wagner",
"position": "Software Engineer",
"office": "San Francisco",
"start_date": "7th Jun 11",
"salary": "$206,850"
},
{
"DT_RowId": "row_6",
"first_name": "Brielle",
"last_name": "Williamson",
"position": "Integration Specialist",
"office": "New York",
"start_date": "2nd Dec 12",
"salary": "$372,000"
},
{
"DT_RowId": "row_43",
"first_name": "Bruno",
"last_name": "Nash",
"position": "Software Engineer",
"office": "London",
"start_date": "3rd May 11",
"salary": "$163,500"
},
{
"DT_RowId": "row_23",
"first_name": "Caesar",
"last_name": "Vance",
"position": "Pre-Sales Support",
"office": "New York",
"start_date": "12th Dec 11",
"salary": "$106,450"
},
{
"DT_RowId": "row_51",
"first_name": "Cara",
"last_name": "Stevens",
"position": "Sales Assistant",
"office": "New York",
"start_date": "6th Dec 11",
"salary": "$145,600"
},
{
"DT_RowId": "row_4",
"first_name": "Cedric",
"last_name": "Kelly",
"position": "Senior Javascript Developer",
"office": "Edinburgh",
"start_date": "29th Mar 12",
"salary": "$433,060"
}
]
}
function format ( d ) {
return '<tr class="child"><td colspan="5">Full name: '+d.first_name+' '+d.last_name+'<br>'+
'Salary: '+d.salary+'<br>'+
'The child row can contain any data you wish, including links, images, inner tables etc.</td></tr>';
}
$(document).ready(function() {
var dt = $('#example').DataTable( {
"columns": [
{
"class": "details-control",
"orderable": false,
"data": null,
"defaultContent": ""
},
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" }
],
"order": [[1, 'asc']]
} );
dt.clear();
dt.rows.add(tableData.data);
dt.draw();
// Array to track the ids of the details displayed rows
var detailRows = [];
$('#example tbody').on( 'click', 'tr td.details-control', function () {
var tr = $(this).closest('tr');
var row = dt.row( tr );
var idx = $.inArray( tr.attr('id'), detailRows );
if ( tr.hasClass('details') ) {
tr.removeClass( 'details' );
tr.prev('tr.child').remove();
}
else {
tr.addClass( 'details' );
tr.before(format( row.data())).show();
}
} );
// On each draw, loop over the `detailRows` array and show any child rows
dt.on( 'draw', function () {
$.each( detailRows, function ( i, id ) {
$('#'+id+' td.details-control').trigger( 'click' );
} );
} );
} );
td.details-control {
background: url('https://datatables.net/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.details td.details-control {
background: url('https://datatables.net/examples/resources/details_close.png') no-repeat center center;
}
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th></th>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
</tr>
</tfoot>
</table>
推荐阅读
- java - 如何使用 k8s java 库列出所有集群
- java - 如何使用 Java SpringBoot 在 FCM 推送通知中向 IOS 发送图像通知
- sql - 有没有办法从 bigquery 中的给定值查找或确定日期或时间格式?
- python - 打开一个现有的 excel 文件并在 Python 中编写一个新行
- django - 如何通过从序列化程序获取 DRF API 的数据来使用 send_mass_mail()
- authentication - 在本地环境中 github 登录后,无法通过 CLI 使用本地磁盘中的 hcl 文件为 Vault 写入策略
- python-3.x - 如何通过模块 testinfra 修复文本?
- firebase - Firestore 不加载文档
- php - 如何注册用户并在同一个 php 函数中登录?
- r - 如何删除 R 中不同长度的多列中的最后一个值(基于组)?