简体   繁体   English

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

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

Problem 问题

I want to hide a row if a value appears in any of its child cells. 如果某个值出现在其任何子单元格中,我想隐藏一行。

Desired Effect 所需效果

  • Apply class to row, if one of its child cells contains a specific value 如果其子单元格之一包含特定值,则将类应用于行
  • Bonus Challenge: Hide column containing the value, ie "admin-hide" 额外挑战:隐藏包含该值的列,即“ admin-hide”

Example Code 范例程式码

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

Why? 为什么?

Invaluable for tables containing information that needs to be: 对于包含以下信息的表而言,这是非常宝贵的:

  • toggled on/off without losing the back-end data, ie member roster, with lapsed member rows having display: none; 在不丢失后端数据(即成员花名册)的情况下打开/关闭,失效的成员行显示:无;
  • highlighting particular rows, ie premium sponsors 突出显示特定行,即高级赞助商

Difficulties I've Faced 我遇到的困难

Hiding the column is problematic. 隐藏列是有问题的。 If necessary, I can stick to just have hiding rows with child elements containing a string. 如有必要,我可以坚持只隐藏带有包含字符串的子元素的行。

Tech I've Working w/ 我正在使用的技术

  • Wordpress 3.5.1 WordPress的3.5.1
  • Jquery 1.10.1 jQuery 1.10.1
  • Tablepress Pluging (which uses DataTables plugin for Jquery) Tablepress插入(将DataTables插件用于Jquery)

Attempt #1 尝试#1

This is what I have in the page editor in WordPress: 这就是我在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>

Attempt #2 - @adeneo 尝试#2-@adeneo

This hides the row but breaks Datatables/Tablepress: 这会隐藏行,但会破坏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>

Attempt #3 - @SpenserJ 尝试#3-@SpenserJ

This hides the row, allows for Datatables. 这隐藏了行,允许使用数据表。 However, it doesn't hide the column. 但是,它不会隐藏该列。

<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 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');
});

Something like this? 像这样吗

// 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');

If you are using dataTables - you can set visibility like this example. 如果您使用的是dataTables,则可以像下面的示例一样设置可见性。 Also remove the +1 because the index for the method is 0 based also - http://www.datatables.net/examples/api/show_hide.html 还要删除+1,因为该方法的索引也是基于0的-http: //www.datatables.net/examples/api/show_hide.html

using oTable 使用oTable

manually hiding visibility 手动隐藏可见性

using hide() works fine - i don't know why it was messing up your sorting 使用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