【问题标题】:Hide entire column(s) right of specific column accounting for rowspans and colspans隐藏特定列右侧的整列,以考虑行跨度和列跨度
【发布时间】:2014-07-08 20:45:03
【问题描述】:

我有这张表http://codepen.io/MetCastle/pen/lxceL,我想根据input type="number" 使用jQuery 隐藏/显示列。表示整个列:

Proveedor 1
Proveedor 2
Proveedor 3
Proveedor 4
Proveedor 5
Proveedor 6

我不知道该怎么做。我试过:nth-child() Selector,但我不明白它是如何工作的。

我做了这个功能,但显然它不完整:

$(document).ready(function() {

  if ($( ".size").val() == 1)
    // hide Proveedor 1
  else if  ($( ".size").val() == 2)
    // hide Proveedor 2

  });

【问题讨论】:

    标签: javascript jquery html css dom-manipulation


    【解决方案1】:

    在这里非常灵活地考虑 colspans... 我认为这对你有用:

    JSFiddle: (我在.size 值中添加了 2,因此您会看到它删除了“Proveedor 2”及其下方的列,您可以在小提琴的 HTML 部分中更改它以使用不同的数字进行测试)

    $(document).ready(function () {
        var column = 0;
        var colspan;
        $("tr").eq(0).find("th").each(function () {
            $this = $(this);
            if ($.trim($this.text()) === "Proveedor "+$(".size").val()) {
                $this.hide();
                colspan = +$this.attr("colspan") || 1;
                return false;
            }
            column += +$this.attr("colspan") || 1;
        });
        $("tr:gt(0)").each(function () {
            var currCol = 0;
            $(this).find("td, th").each(function () {
                $this = $(this);
                if (column === currCol) {
                    $this.hide();
                    for(i = +$this.attr("colspan") || 1; i < colspan;) {
                        $this = $this.next();
                        $this.hide();
                        i += +$this.attr("colspan") || 1;
                    }
                    return false;
                }
                currCol += +$this.attr("colspan") || 1;
            });
        });
    });
    

    注意:

    +$this.attr("colspan") || 1
    

    前面的+ 是把这个转换成一个数字(从一个字符串)。如果 colspan 未定义,它将等同于错误值 (NaN),然后将移动到 1(如果未定义 colspan,这是默认值)...所以如果定义 colspan,它将是任何定义的,否则它将是 1。


    在明确了一些要求之后,这里是一个修改。处理 colspans 和 rowspans 的代码变得相当复杂......

    JSFiddle

    $(":input").on('change', function () {
        $("td, th").show();
        var column = 0;
        var colspan = 0;
        var rowspanning = [];
        $("tr").eq(0).find("th").each(function () {
            $this = $(this);
            if ($.trim($this.text()) === "Proveedor " + $(".size").val()) {
                $this.nextAll().hide();
                colspan = +$this.attr("colspan") || 1;
                rowspanning[column] = $this.attr("rowspan")-1 || 0;
                return false;
            }
            column += +$this.attr("colspan") || 1;
        });
        if (column) {
            $("tr:gt(0)").each(function () {
                var currCol = 0;
                $(this).find("td, th").each(function () {
                    $this = $(this);
                    if(rowspanning[currCol] > 0) {
                        --rowspanning[currCol];
                        ++currCol;
                    }
                    rowspanning[currCol] = $this.attr("rowspan")-1 || 0;
                    if (column === currCol) {
                        for (i = +$this.attr("colspan") || 1; i < colspan;) {
                            $this = $this.next();
                            i += +$this.attr("colspan") || 1;
                        }
                        $this.nextAll().hide();
                        return false;
                    }
                    currCol += +$this.attr("colspan") || 1;
                });
            });
        }
    });
    

    【讨论】:

    • 感谢您的回答,对不起,我没有解释我需要什么,看,当页面加载时应该出现第一列“Proveedor 1”,如果用户更改输入类型="number" 应该出现与输入相同的列。我为此 $(":input").bind('keyup mouseup', function () look 更改了第一行
    • 啊...您已准备好文档...无论如何,我仍然不能 100% 确定我理解,但我认为您的意思是您想要与 @987654333 中的号码关联的“Proveedor” @ 在.size 更改时隐藏...以及其他所有内容再次显示...这是您现在想要的吗? jsfiddle.net/fK8sn/3
    • 对不起,但不是,如果我在输入中选择 3 应该存在,provider 1,proveedor 2 和proveedor 3,y 我选择1 只是proveedor 1 和其他相同,5 = 前5 列等。 ..我有一个问题,为什么不将整个列消失到底部?是不是可能?再次感谢
    • 仍然无法理解您...也许是这个? jsfiddle.net/fK8sn/4
    • 是的,你是最棒的男人!但是我有同样的问题,为什么不将整个列消失到底部?是不可能的,因为当您删除下面的列时,该列仍然存在 td。
    【解决方案2】:

    使用:nth-child() 选择器隐藏每行tr 元素之后的“第n 个”元素。例如,

      if ($( ".size").val() == 1){
          $('tr td:nth-child(1)').hide();
          //include this line only if you also have a thead element with headings
          $('tr th:nth-child(1)').hide();
      }
      else if ($( ".size").val() == 2){
          $('tr td:nth-child(2)').hide();
          //include this line only if you also have a thead element with headings
          $('tr th:nth-child(2)').hide();
      }
    

    【讨论】:

    • 当涉及到 colspan 时,并不像使用索引那么简单
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2012-10-24
    • 2016-09-08
    相关资源
    最近更新 更多