gpt4 book ai didi

javascript - 将详细信息/子行添加到数据表中的某些条目

转载 作者:行者123 更新时间:2023-12-03 03:02:31 24 4
gpt4 key购买 nike

来自 docs在数据表中,我们了解如何向数据(JSON/CSV 文件)中的每一行添加子行或详细信息。但是我不想要扩展按钮,扩展名是空的,就像前两个数据条目一样。这就是我的工作内容:

Javascript

function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>Full name:</td>'+
'<td>'+d.name+'</td>'+
'</tr>'+
'<tr>'+
'<td>Extension number:</td>'+
'<td>'+d.extn+'</td>'+
'</tr>'+
'<tr>'+
'<td>Extra info:</td>'+
'<td>And any further details here (images etc)...</td>'+
'</tr>'+
'</table>';
}

$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": "../ajax/data/objects.txt",
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "salary" }
],
"order": [[1, 'asc']]
} );

// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
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');
}
} );
} );

HTML

<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</tfoot>
</table>

JSON

{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": ""
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": ""
},
{
"id": "3",
"name": "Ashton Cox",
"position": "Junior Technical Author",
"salary": "$86,000",
"start_date": "2009/01/12",
"office": "San Francisco",
"extn": "1562"
},
{
"id": "4",
"name": "Cedric Kelly",
"position": "Senior Javascript Developer",
"salary": "$433,060",
"start_date": "2012/03/29",
"office": "Edinburgh",
"extn": "6224"
},
]

正如您所看到的,条目 1 和条目 2 的扩展名都是空的,这里我根本不希望显示展开按钮,只显示空白区域。扩展名非空的另外两个应该仍然有显示此信息的按钮。

最佳答案

您可以使用createdRow()检查详细信息单元格的内容,如果该单元格不为空,则有条件地添加 details-control 类。

例如,这会检查 extn 字段是否具有非空值

$(document).ready(function() {
var table = $('#example').DataTable({
"data": data.data,
"columns": [{
"orderable": false,
"data": null,
"defaultContent": ''
}, {
"data": "name"
}, {
"data": "position"
}, {
"data": "office"
}, {
"data": "salary"
}],
"order": [
[1, 'asc']
],
"createdRow": function(row, data, dataIndex) {
if (data.extn !== "") {
$(row).find("td:eq(0)").addClass('details-control');
}
}
});

这是一个 fiddle 演示:https://jsfiddle.net/zephyr_hex/rhgL0294/20/

关于javascript - 将详细信息/子行添加到数据表中的某些条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com