jquery根据td值隐藏列

时间:2017-01-03 01:14:37

标签: jquery

我有一个页面,我根据类td的{​​{1}}中的值动态地尝试隐藏类exception_table的表中的列。

如果该值不等于用户选择tdHideShow的值,则从下拉列表中我想隐藏strDMM,从而隐藏我的表格的那一列。

表示我的其他语法中的某些内容无效,td未被隐藏。

td

这是我的示例html。我想要任何没有类Bob的表,并隐藏不是Bob的列。我隐藏了没有鲍勃在任何桌子上的div,隐藏了根本没有Bob的桌子。我需要在包含Billy的第一个div,第一个表中隐藏表列。 html是动态的并且发生变化,用户过滤DMM名称。

$('.exception_table',this).each(function(ind,obj){

                if(!($('tr td.tdHideShow',this).hasClass(strDMM))){
                    $(this).hide();
                }
                else{
                    count++;
                }

                $('tr td.tdHideShow',this).each(function(ind,obj){
                        if(!($(this).hasClass(strDMM))){
                            //trying to hide the column if it is missing the class I am looking for
                        }
                });

            });

            if(count==0){
                $(this).parent.hide();
            }

        });  

1 个答案:

答案 0 :(得分:0)

如果您向每行添加class="tdHideShow Bob"等类,那么这将有效:

function onlyShow(show) {
    $('.exception_table tr td.tdHideShow').each(function (ind, obj) {
        $(this).hasClass(show) ? $(this).show() : $(this).hide()
    });
}
onlyShow('Bob');

或高级版本:https://jsfiddle.net/favvsz9k/

function hideOthers(show) {
    var $table, colNumber, $colHead, $colsCorresponding, visibleCols;
    // loop through each table
    $('.exception_table').each(function (ind, obj) {
        $table = $(this);
        colNumber = 0;
        visibleCols = 0;
        // Check each td of the first column on this table (header elements)
        $table.find('tr:eq(0) td').each(function (ind, obj) {
            $colHead = $(this);
            // Is it a hide/show relevant column?
            if ($colHead.hasClass('tdHideShow')) {
                $colsCorresponding = $table.find('tr td:nth-child(' + colNumber + ')');
                // Should the column be shown?
                if ($colHead.hasClass(show)) {
                    $colsCorresponding.show();
                    visibleCols++;
                } else {
                    $colsCorresponding.hide();
                }
            }
            colNumber++;
        });
        // hide the table if there are no cols visible on it
        visibleCols ? $table.show() : $table.hide();
    });
}
var strDMM = 'Paul';
hideOthers(strDMM);