比较并隐藏除表格相等列之外的所有列

时间:2014-07-25 11:37:11

标签: jquery

我与Jquery和桌子斗争。

我试图编写比较列(而不是行)的函数,并隐藏所有相等的列,但留下1。

<table id="item-sizes-table">
<tbody>
<tr>
    <td>
        פריט (סיית)
    </td>
    <td data-product-id="43" data-duplicate="false">
        <span title="XS (32-34)">XS</span>
    </td>
    <td data-product-id="41" data-duplicate="false">
        <span title="S (34-36)">S</span>
    </td>
    <td data-product-id="40" data-duplicate="false">
        <span title="M (36-38)">M</span>
    </td>
    <td data-product-id="46" data-duplicate="false">
        <span title="L (38-40)">L</span>
    </td>
    <td data-product-id="47" data-duplicate="true">
        <span title="L (38-40)">L</span>
    </td>
    <td data-product-id="45" data-duplicate="true">
        <span title="L (38-40)">L</span>
    </td>
    <td data-product-id="39" data-duplicate="true">
        <span title="L (38-40)">L</span>
    </td>
    <td data-product-id="44" data-duplicate="true">
        <span title="L (38-40)">L</span>
    </td>
    <td data-product-id="42" data-duplicate="false">
        <span title="XL (40-42)">XL</span>
    </td>
</tr>
<tr>
    <td>
        אורך
    </td>
    <td data-product-id="43" data-duplicate="true">
        <span title="XS (32-34)"></span>
    </td>
    <td data-product-id="41" data-duplicate="false">
        <span title="S (34-36)">1S</span>
    </td>
    <td data-product-id="40" data-duplicate="false">
        <span title="M (36-38)">1M</span>
    </td>
    <td data-product-id="46" data-duplicate="false">
        <span title="L (38-40)">1L</span>
    </td>
    <td data-product-id="47" data-duplicate="true">
        <span title="L (38-40)">1L</span>
    </td>
    <td data-product-id="45" data-duplicate="true">
        <span title="L (38-40)">1L</span>
    </td>
    <td data-product-id="39" data-duplicate="true">
        <span title="L (38-40)">1L</span>
    </td>
    <td data-product-id="44" data-duplicate="true">
        <span title="L (38-40)">1L</span>
    </td>
    <td data-product-id="42" data-duplicate="false">
        <span title="XL (40-42)">length</span>
    </td>
</tr>
<tr>
    <td>
        Sleeve
    </td>
    <td data-product-id="43" data-duplicate="false">
        <span title="XS (32-34)">2XS</span>
    </td>
    <td data-product-id="41" data-duplicate="false">
        <span title="S (34-36)">2S</span>
    </td>
    <td data-product-id="40" data-duplicate="false">
        <span title="M (36-38)">2M</span>
    </td>
    <td data-product-id="46" data-duplicate="false">
        <span title="L (38-40)">2L</span>
    </td>
    <td data-product-id="47" data-duplicate="true">
        <span title="L (38-40)">2L</span>
    </td>
    <td data-product-id="45" data-duplicate="true">
        <span title="L (38-40)">2L</span>
    </td>
    <td data-product-id="39" data-duplicate="true">
        <span title="L (38-40)">2L</span>
    </td>
    <td data-product-id="44" data-duplicate="true">
        <span title="L (38-40)">2L</span>
    </td>
    <td data-product-id="42" data-duplicate="false">
        <span title="XL (40-42)">sleeve</span>
    </td>
</tr>
<tr>
    <td>
        Chest
    </td>
    <td data-product-id="43" data-duplicate="false">
        <span title="XS (32-34)">3XS</span>
    </td>
    <td data-product-id="41" data-duplicate="false">
        <span title="S (34-36)">3S</span>
    </td>
    <td data-product-id="40" data-duplicate="false">
        <span title="M (36-38)">3M</span>
    </td>
    <td data-product-id="46" data-duplicate="false">
        <span title="L (38-40)">3L</span>
    </td>
    <td data-product-id="47" data-duplicate="true">
        <span title="L (38-40)">3L</span>
    </td>
    <td data-product-id="45" data-duplicate="true">
        <span title="L (38-40)">3L</span>
    </td>
    <td data-product-id="39" data-duplicate="true">
        <span title="L (38-40)">3L</span>
    </td>
    <td data-product-id="44" data-duplicate="true">
        <span title="L (38-40)">3L</span>
    </td>
    <td data-product-id="42" data-duplicate="false">
        <span title="XL (40-42)">chest</span>
    </td>
</tr>
<tr>
    <td>
        Scope
    </td>
    <td data-product-id="43" data-duplicate="true">
        <span title="XS (32-34)"></span>
    </td>
    <td data-product-id="41" data-duplicate="true">
        <span title="S (34-36)"></span>
    </td>
    <td data-product-id="40" data-duplicate="true">
        <span title="M (36-38)"></span>
    </td>
    <td data-product-id="46" data-duplicate="true">
        <span title="L (38-40)"></span>
    </td>
    <td data-product-id="47" data-duplicate="true">
        <span title="L (38-40)"></span>
    </td>
    <td data-product-id="45" data-duplicate="true">
        <span title="L (38-40)"></span>
    </td>
    <td data-product-id="39" data-duplicate="true">
        <span title="L (38-40)"></span>
    </td>
    <td data-product-id="44" data-duplicate="true">
        <span title="L (38-40)"></span>
    </td>
    <td data-product-id="42" data-duplicate="false">
        <span title="XL (40-42)">scope</span>
    </td>
</tr>
</tbody>

我有这个html表,其中一些列有重复的列(它们几乎相同,只有data-product-id属性不同)基本上我需要检查同一列中的所有td元素是否具有属性data-duplicate with value是(比较服务器端在行中)

2 个答案:

答案 0 :(得分:1)

这应该合而为一:

$("tr").find("td[data-duplicate=true]").not(":first").hide()

演示:

http://jsfiddle.net/Rr4D6/7/


这是一个更复杂的版本,现在我更好地理解了这个问题。这个小提琴首先显示标记为红色的dupes的单元格,并为表格添加边框。现在,我们可以更容易地看到我们需要隐藏哪些列:除了第一列之外,所有列都是红色的。

现在点击按钮即可完成!

http://jsfiddle.net/Rr4D6/13/

hideDupeCols = function() {
    found_first = false
    var num_cols = $("tr").first().find("td").length, i
    for (i = 0; i < num_cols; i++) {
        var hide = true
        $("tr").each(function(j, tr) {
            hide = hide && $($(tr).find("td").get(i)).attr("data-duplicate") == "true"
        });
        if (hide) {
            if (found_first) $("tr td:nth-child(" + (i+1) + ")").hide()
            else found_first = true
        }
    }
}

答案 1 :(得分:0)

你去吧:

首先我们计算我们有多少列, 接下来我们遍历循环中的每一列并检查其所有单元格
如果所有单元格都重复,我们隐藏整个列:

Example

的javascript:

$(function(){
    //count how many columns we have
    var totalColumns = $("td",$("#item-sizes-table tr").first()).size();
    //go over all the columns
    for(i=1;i<=totalColumns;i++){
        //set a temporary var
        var isDuplicate=true;
        //go over every cell in the column
        $("#item-sizes-table td:nth-of-type(" + parseInt(i+1) + ")").each(function(){
            //if a non-duplicate cell found, set flag to false
            if($(this).attr("data-duplicate")=="false"){
                isDuplicate=false;
            }
        });
        //if all cells in a column are duplicates, hide the column
        if(isDuplicate==true){
            $("#item-sizes-table td:nth-of-type(" + parseInt(i+1) + ")").hide();
        }
    }

});