gpt4 book ai didi

jquery - 数据表子行不显示

转载 作者:行者123 更新时间:2023-12-01 02:19:08 25 4
gpt4 key购买 nike

我正在关注这篇关于数据表的博客文章 Sliding Child Rows但我无法在单击时显示我的行。我只想在单击第一列时能够显示子行。我做错了什么?

我的 jQuery

<script type="text/javascript">
$(document).ready(function(){
$('#products').DataTable({
"lengthMenu": [[20, 50, 100, 500, -1], [20, 50, 100, 500, "All"]],
"columnDefs": [{ className: "details-control", "targets": [ 0 ] }]
});
});
</script>

<script type="text/javascript">
$('#products 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
$('div.slider', row.child()).slideUp( function () {
row.child.hide();
tr.removeClass('shown');
} );
}
else {
// Open this row
row.child( format(row.data()), 'no-padding' ).show();
tr.addClass('shown');

$('div.slider', row.child()).slideDown();
}
} );
</script>

然后表格本身带有一些要显示的虚拟数据

<table id="products" class="table table-striped table-hover table-bordered 
table-condensed">
<thead>
<tr>
<th> </th>
<th> </th>
<th><a>TITLE</a></th>
<th><a>ASIN</a></th>
<th><a>PRICE</a></th>
<th><a>SKU</a></th>
<th> </th>
<th> </th>
</tr>
</thead>

<tbody class="product-index">

<% @merchant.products.each do |product| %>
<tr>
<td>+
<div class="slider" name>
<table>
<tr>
<td>
... Data to be shown ...
</td>
</tr>
</table>
</div>
</td>
<td><%= check_box_tag('sellersku[]', product.sellersku) %></td>
<td><%= product.title %></td>
...
<% end %>
</tbody>
</table>

最佳答案

首先,您缺少table引用,即var table = $('#products').DataTable()

您还需要实际插入子行的内容。 dataTables 子行是动态插入的 - 您不能按照标记指示使用静态内容。通常,您会使用一个返回子行内容的函数,通常称为 format(data) (并且您实际上在复制的代码中引用了此类函数),其中 data 是父行本身。但你可以用任何你喜欢的方式来做。下面是您对问题的标记:

function format(data) {
return '<div class="slider" name>'+
'<table>'+
'<tr>'+
'<td>'+
'... Data to be shown ...'+
'</td>'+
'</tr>'+
'</table>'+
'</div>'
}

这两项更改使您的代码可以正常工作,包括幻灯片效果 -> http://jsfiddle.net/LmudsL36/

关于jquery - 数据表子行不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743556/

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