[英]Apply class to parent row, if child cell contains a specific value
I want to hide a row if a value appears in any of its child cells. 如果某个值出现在其任何子单元格中,我想隐藏一行。
$('tr').each(function(){
if($('td:contains("non-member")', this).length{
$(this).addClass('disabled');
}
});
Invaluable for tables containing information that needs to be: 对于包含以下信息的表而言,这是非常宝贵的:
Hiding the column is problematic. 隐藏列是有问题的。 If necessary, I can stick to just have hiding rows with child elements containing a string.
如有必要,我可以坚持只隐藏带有包含字符串的子元素的行。
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>
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>
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
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.