gpt4 book ai didi

javascript - 展开时将背景颜色应用于数据表行

转载 作者:行者123 更新时间:2023-11-30 00:22:50 25 4
gpt4 key购买 nike

展开行时,我想更改行的背景颜色,但并非总是如此。我申请了该类(class),但效果不佳。当我展开另一行时,它只会突出显示第一行。我做错了什么吗?我的代码如下。

var table = $('#meterDataTable').DataTable();
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');
tr.removeClass('highlightExpanded');
} else {
// Open this row
var elementName = row.data()[0];
row.child(metersAttributesCache[elementName]).show();
tr.addClass('highlightExpanded');
tr.addClass('shown');
//$('#unitAttributeDataTable').DataTable({ "bFilter": false });
$('.toggleCheckBox').bootstrapToggle({});
}

最佳答案

CAUSE

您的代码似乎缺少某些部分,它应该在附加到 td.details-control 的点击事件处理程序中。还要确保您在 .highlightExpanded 的 CSS 规则中使用 !important 以确保它优先。

DEMO

/* Formatting function for row details - modify as you need */
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_data = [{"name":"Tiger Nixon","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"},{"name":"Garrett Winters","position":"Accountant","salary":"$170,750","start_date":"2011/07/25","office":"Tokyo","extn":"8422"},{"name":"Ashton Cox","position":"Junior Technical Author","salary":"$86,000","start_date":"2009/01/12","office":"San Francisco","extn":"1562"}];

var table = $('#example').DataTable( {
"data": table_data,
"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');
tr.removeClass('highlightExpanded');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
tr.addClass('highlightExpanded');
}
} );


} );
td.details-control {
background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;
}

tr.highlightExpanded {
background-color:#F4CCCC !important;
}
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<table id="example" class="display">
<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>

关于javascript - 展开时将背景颜色应用于数据表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552583/

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