1

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, i.e. "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, i.e. member roster, with lapsed member rows having display: none;
  • highlighting particular rows, i.e. 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
  • Jquery 1.10.1
  • Tablepress Pluging (which uses DataTables plugin for Jquery)

Attempt #1

This is what I have in the page editor in 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

This hides the row but breaks 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

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>
4

4 回答 4

2

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();
});
于 2013-06-21T20:55:46.630 回答
1
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');
});
于 2013-06-21T20:36:53.500 回答
0

像这样的东西?

// 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()
于 2013-06-21T20:52:56.350 回答
-1
$('table .classForRowThatCouldBeHidden').find('.someClassForHiddenValue').parent().hide();
于 2013-06-21T20:33:56.450 回答