简体   繁体   English

简单的显示/隐藏在Javascript中出错

[英]Simple show/hide gone wrong in Javascript

I'm trying to create a show/hide function for data within a table populated with data from a mssql database. 我正在尝试为填充来自mssql数据库的数据的表中的数据创建显示/隐藏功能。 The function should look for rows with the same value in the "capability" column and onclick, hide all rows with the same value. 该函数应在“功能”列中查找具有相同值的行,然后单击以隐藏具有相同值的所有行。 After this, a row is inserted into the table with the same capability value, but summarizes the data in the hidden rows. 之后,将具有相同功能值的一行插入到表中,但汇总了隐藏行中的数据。 This should work in the way that grouping cells together works in excel. 这应该以将单元格分组在excel中工作的方式工作。

I've managed to get this to work, but it only works for the first click and I receive a "cannot read innerHTML property of NULL" for any of the function's calls after that. 我设法使它起作用,但是它仅适用于第一次单击,此后任何函数调用都收到“无法读取NULL的innerHTML属性”。

function compactRows(thisrow) {
  var totalRows = document.getElementById("DataTable").getElementsByTagName("tr").length;

  var summaryVal1= [];
  var summaryVal2= [];

  for(var i = 1; i < totalRows;i++) {
    var trID = "capability" + i;
    if(thisrow.innerHTML == document.getElementById(trID).innerHTML) { //The error gets returned on this line
        summaryVal1.push(document.getElementById(trID).parentNode.children[5].innerHTML);
        summaryVal2.push(document.getElementById(trID).parentNode.children[14].innerHTML);
        document.getElementById(trID).parentNode.style.display = 'none';
    }
  }
  createNewRow(thisrow, summaryVal1, summaryVal2);
}

//I took out the logic for the data summarizing in the createNewRow function because I don't think its relevant to the issue I'm having. Also, I didn't want to crowd the area with unrelated code
function createNewRow(row, ibxMobile, overallStatus) {
var table = document.getElementById("DataTable");
var localRow = table.insertRow(row.parentNode.rowIndex);

var cell1 = localRow.insertCell(0);
cell1.setAttribute("id", "entry1");

var cell2 = localRow.insertCell(0);
cell2.setAttribute("id", "Capability");
cell2.innerHTML = row.innerHTML;

var cell3 = localRow.insertCell(0);
cell3.setAttribute("id", "entry3");
}

The function called at the bottom, createNewRow, handles making the row to be entered after all the rows are hidden. 在底部调用的函数createNewRow处理隐藏所有行之后输入的行。 It also, handles the logic for summarizing the hidden rows. 它还处理汇总隐藏行的逻辑。

All help is greatly appreciated! 非常感谢所有帮助! Thank you 谢谢

Edit 1: example table set up 编辑1:示例表设置

<table>
<tr>
<th>Entry1 </th>
<th>Capability</th>
<th>Entry3 </th>
</tr>
<tr>
<td>1.1</td>
<td id="Capability1" onclick="compactRows(this)">Lasers</td>
<td>stuff</td>
</tr>
<tr>
<td>1.2</td>
<td id="Capability2" onclick="compactRows(this)">Lasers</td>
<td>things</td>
</tr>
<tr>
<td>2.1</td>
<td id="Capability3" onclick="compactRows(this)">Beams</td>
<td>more things</td>
</tr>
</table>

//The Below table is what it looks like after clicking either of the first two entries

<table>
<tr>
<th>Entry1 </th>
<th>Capability</th>
<th>Entry3 </th>
</tr>
<tr>
<td>1</td>
<td id="Capability">Lasers</td>
<td></td>
</tr>
<tr>
<td>2.1</td>
<td id="Capability3" onclick="compactRows(this)">Beams</td>
<td>more things</td>
</tr>
</table>

There is no "Capability1" id for any rows after the "CreateNewRow" is called. 调用“ CreateNewRow”之后,任何行都没有“ Capability1” ID。 Therefore, the second time "CompactRows()" is called a null reference is thrown when accessing "document.getElementById(" Capability1 ").innerHTML". 因此,第二次调用“ CompactRows()”时,访问“ document.getElementById(” Capability1 “).innerHTML”时将引发空引用。 Rework the CreateNewRow to inlcude the increment for the Capability id value or test that "getElementById" actually returns an object before attempting to access the innerHTML method. 重做CreateNewRow以包括Capability id值的增量,或者在尝试访问innerHTML方法之前测试“ getElementById”是否实际上返回了对象。

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

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