简体   繁体   English

检查表中的每个单元格是否等于上面的单元格

[英]Check if each cell in a table is equal to the cell above

How do you check in JS what the value of the td above the current one is in a table? 您如何在JS中检查表格中当前td上方的td值是多少? I'm trying to get a table like this: 我试图得到这样的表:

|column1 | column2 | column3 | column4 | 
----------------------------------------
|        |         |         |         |
----------------------------------------
|        |         |  data   |         |
----------------------------------------
|        |         |  data   |   fox   |
----------------------------------------
|        |  bacon  |         |         |
----------------------------------------
|        |         |         |         |

To look like this: 看起来像这样:

|column1 | column2 | column3 | column4 | 
----------------------------------------
|        |         |         |         |
----------------------------------------
|        |         |         |         |
--------------------  data   -----------
|        |         |         |   fox   |
----------------------------------------
|        |  bacon  |         |         |
----------------------------------------
|        |         |         |         |

My plan was to loop through each td element, to check the html of that td, then the one above, and if they are equal, hide the bottom one, and set the rowspan of the top one to 2. 我的计划是遍历每个td元素,检查该td的html,然后检查上面的那个,如果相等,则隐藏底部的td,并将​​顶部的rowpan设置为2。

I'm having trouble iterating through the td elements, I've tried some jQuery like this: 我在遍历td元素时遇到麻烦,我已经尝试过一些类似jQuery的方法:

 $("#myTable td").each(function(){
     console.log($(this).html())
 });

But I can't access easily the cell above/below the current one. 但是我无法轻松访问当前单元格上方/下方的单元格。 Any ideas? 有任何想法吗?

Maybe something like this: 也许是这样的:

$("#myTable td").each(function(){
    var thisRowNum = $(this).closest('tr').prevAll().length;

    var thisColNum = $(this).prevAll().length;

    var cellAbove = $(this)
        .closest('table')
        .find('tr:eq('+thisRowNum+') td:eq('+thisColNum+')'); // the cell

    var thisCell = $(this).html();
    if (thisCell !== "") {
        if ((thisCell === cellAbove.html())) {
            $(this).hide();
            cellAbove.attr("rowspan","2");
        }
    }

 });

如果您想这样做,请获取行和列,然后创建一个数组(对其进行二维排列并循环执行)。它为您提供了更大的灵活性。但是我想说,这项工作应该在服务器端语言下进行您选择了诸如JSP,ASP,PHP等...,它给了您很多控制权。

You could write a traversal algorithm yourself, storing the values from row to row. 您可以自己编写遍历算法,逐行存储值。

var above = [];

$('#myTable tr').each(function()
{
  $(this).children('td').each(function(i)
  {
    if (above.length > i && $(above[i]).text() == $(this).text()) 
    {
      $(above[i]).attr('rowspan', ($(above[i]).attr('rowspan') || 1) + 1);
      $(this).remove();
    }
    else
    {
      above[i] = this;
    }
  });
});

I didn't try it on an actual page, but in theory this should work: 我没有在实际的页面上尝试过,但是从理论上讲这应该可行:

<script type="text/javascript" charset="utf-8">
    var container = ".mytable tbody > tr";
    var td_index, row_index = 0;

    $(container).each(function() {
        td_index = -1;
        row_index++;
        if(row_index > 0) {
            td_index ++;
            var above_tr = $(container+"eq("+(row_index-1)+")");
            var td_val = $(this).find("td:eq("+td_index+")").html();
            var above_td_val = $(above_tr).find("td:eq("+td_index+")").html();
            if(td_val == above_td_val) {
                $(above_tr).find("td:eq("+td_index+")").attr("rowspan", 2);
                $(this).find("td:eq("+td_index+")").remove();
            }
        }

    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM