如果子单元格包含特定值,则将类应用于父行



问题

如果某个值出现在某个子单元格中,我希望隐藏该行。

预期效果

  • 如果某个子单元格包含特定值,则将类应用于行
  • 奖金挑战:隐藏包含值的列,即"admin-Hide"

示例代码

$('tr').each(function(){
if($('td:contains("non-member")', this).length{       
$(this).addClass('disabled');
}
});

为什么

对于包含需要的信息的表来说是有价值的

  • 在不丢失后端数据(即成员名册)的情况下切换开/关,失效的成员行具有显示:无
  • 突出显示特定行,即高级赞助商

我所面临的困难

隐藏列是有问题的。如果必要的话,我可以坚持只使用包含字符串的子元素来隐藏行。

我一直在工作的技术/

  • Wordpress 3.5.1
  • Jquery 1.10.1
  • Tablepress插件(使用Jquery的DataTables插件)

尝试#1

这是我在WordPress的页面编辑器中拥有的:

[table id=3 /]
<script>jQuery(function($) {
$('#tableID tr').filter(function() {
$('td', this).each(function() {
if ($(this).text().indexOf('admin-hide') != -1)
$('#tableID tr td:eq('+ $(this).index() +')').hide();
});
return $(this).text().indexOf('non-member') != -1;
}).addClass('disabled');
});</script>
<style>
.disabled {display: none;}
</style>

尝试#2-@adeneo

这会隐藏行,但会破坏Datatables/Tablepress:

<script> jQuery(function($) {
$('#tablepress-3 tr:contains("admin-hide")').addClass('disable-cells')
var index = $('td:contains("admin-hide")').index();
$('th,td', '#tablepress-3').filter('nth-child('+(index+1)+')').addClass('disable-cells'); });
</script>
<style>
.disable-cells {display: none;}
</style>

尝试#3-@SpenserJ

这会隐藏行,允许使用数据表。但是,它不会隐藏该列。

<script>
jQuery(function($) {
$('#tablepress-3 td').each(function() {
if ($(this).text().indexOf('admin-hide') !== -1) {
// Hide the column without affecting the table formatting
$(this).css('visibility', 'hidden');
}
});
// Hide the entire row
$('#tablepress-3 tr:contains("admin-hide")').hide();
});
</script>

http://codepen.io/SpenserJ/pen/GqviI

jQuery(function($) {
  $table = $('#tablepress-3');
  $('th, td', $table).each(function() {
    if ($(this).text().indexOf('Admin Only') !== -1) {
      var index = $(this).index();
      $('th:eq(' + index + '), td:eq(' + index + ')', 'tr', $table).hide();
    }
  });
  // Hide the entire row
  $('tr:contains("Membership Expired")', $table).hide();
});
jQuery(function($) {
    $('#tableID tr').filter(function() {
        $('td', this).each(function() {
            if ($(this).text().indexOf('admin-hide') != -1)
                $('#tableID tr td:eq('+ $(this).index() +')').hide();
        });
        return $(this).text().indexOf('non-member') != -1;
    }).addClass('disabled');
});

类似的东西?

// tr that contains the specific word - add class to
$('#tableid tr:contains("specific word")').addClass('yourclass');
// get column index of admin-hide
var index = $('#tableid td:contains("admin-hide")').index();
// and hide all th/tr in that column    
// this is assuming when you initialized datatables you named it oTable as in var oTable = $('table').datatables()
oTable.fnSetColumnVis( index, false ); // for datatables
// or if you want to manually hide them
$('th,td', '#tableid').filter(':nth-child('+(index+1)+')').css('visibility','hidden');

如果您使用的是dataTables,您可以像本例一样设置可见性。同时删除+1,因为该方法的索引也是基于0的-http://www.datatables.net/examples/api/show_hide.html

使用oTable

手动隐藏可见性

使用hide()效果很好-我不知道为什么它会打乱你的排序

$('th,td', '#tablepress-demo').filter(':nth-child(' + (index + 1) + ')').hide()
$('table .classForRowThatCouldBeHidden').find('.someClassForHiddenValue').parent().hide();

最新更新