繁体   English   中英

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

[英]Apply class to parent row, if child cell contains a specific value

问题

如果某个值出现在其任何子单元格中,我想隐藏一行。

所需效果

  • 如果其子单元格之一包含特定值,则将类应用于行
  • 额外挑战:隐藏包含该值的列,即“ admin-hide”

范例程式码

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

为什么?

对于包含以下信息的表而言,这是非常宝贵的:

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

我遇到的困难

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

我正在使用的技术

  • WordPress的3.5.1
  • jQuery 1.10.1
  • Tablepress插入(将DataTables插件用于Jquery)

尝试#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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM