繁体   English   中英

如何使用javascript按复选框过滤表格?

[英]How to filter a table by checkbox with javascript?

我正在从XML文件创建一个表。 该表有三列(名称,日期,密码)和动态行。 我在秘密列的标题上创建了一个复选框(第三列)。 当我点击复选框时,它应该只显示具有相同“秘密”和相同“名称”的那些人的最早日期的一行。 如果两行有两个相同的秘密,但有两个不同的名称,则应显示两行。 XSLT文件中的主要部分只是复选框。 我不确定xslt代码是否真的需要onchange =“identicalSecrets()”。

请参阅下面的代码以获取javascript。 它不起作用所以我很确定我在javascript代码上有很多错误。

XSLT或HTML:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
    <head><title>Shared Secrets</title></head>

<body>
    <h1>Shared Secrets</h1>
<table id="myTable">
        <colgroup>
            <col width="150" style="background-color:red"></col>
            <col width="165"></col>
        </colgroup>
        <tr  style ="background-color:grey">
            <th>plane
                <select id="modelRangeDropdown" onchange="filterReports()">
                     <option selected="selected">All</option>
                     <xsl:for-each select="logstore/plane">
                        <option>
                         <xsl:value-of select="Name" />
                        </option>
                     </xsl:for-each>                    
                </select>                   
            </th>   
            <th>Datum</th>
            <th>Secret
                <input type="checkbox" id="identicalSecrets" onchange="identicalSecrets()"></input>
                <label for="identicalSecrets">hide identical secrets</label>
            </th>
        </tr>
        <xsl:for-each select="logstore/plane/trigger">
            <tr>
                <td><xsl:value-of select="../Name"/></td>
                <td><xsl:value-of select="date"/></td>
                <td><xsl:value-of select="secret"/></td>
            </tr>
        </xsl:for-each>
    </table>    
    <script type="text/javascript" src="/../../filterReports.js"></script>  
    <script type="text/javascript" src=/../../identicalSecrets.js"></script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

以上代码的主要部分:

<th>Secret
    <input type="checkbox" id="identicalSecrets" onchange="identicalSecrets()"></input>
    <label for="identicalSecrets">hide identical secrets</label>
</th>

Javascript代码:

function identicalSecrets() {
    let checkBox, filter, rows, cells, secret;
    checkBox=document.getElementById('identicalSecrets');
    rows = table.getElementsByTagName("tr");
    filter = checkBox.value;
    for (let row of rows) {
        cells = row.getElementsByTagName("td");
        secret = cells[2];
        if (filter == true) {
            if (secret === row[-1].getElementsbyTagName("td")[2] && secret ===  row[-1].getElementsbyTagName("td")[0]) { // If the actual secret is equal to the secret in the last row and the modelranges are equal then hide these rows.
                row.style.display = "none"; // hide this row
            }
            else { // if secret or modelrange are not equal
                row.style.display = ""; // show this row
            }
        }
    }

}   

我希望当我单击该复选框时,除了具有最早日期(第二列)的行之外,具有相等第一列和第三列的所有行都将消失。

你的javascript有很多问题......

  • 您引用了一个名为table的变量,但尚未对其进行定义。
  • 执行row[-1]无效,因为row不是数组( rows is),而-1不是有效索引,因为数组是从0开始的。 你应该真的设置一个变量(称为index )来保持行索引的计数,然后你可以做rows[index - 1]来获取前一行
  • 您只想开始检查第三行上的行(因为第一行是标题行,第二行没有要检查的先前数据行)
  • 您正在将secret值与第一列和第三列进行比较。 但是你应该检查第一列的名称
  • Javascript函数区分大小写,因此getElementsbyTagName将不起作用,因为小写“by”
  • 您应该使用“innerText”来获取单元格的文本值,因为您要比较文本而不是单元格对象本身。

所以,把它们放在一起,你的javascript应该是这样的:

function identicalSecrets() {
    let table, checkBox, filter, rows, cells, secret, name;
    table = document.getElementById('myTable');
    checkBox=document.getElementById('identicalSecrets');
    rows = table.getElementsByTagName("tr");
    filter = checkBox.checked;
    let index = 0;
    for (let row of rows) {
        if (index > 1) {
            cells = row.getElementsByTagName("td");
            name = cells[0].innerText;
            secret = cells[2].innerText;
            if (filter == true) {
                if (name === rows[index - 1].getElementsByTagName("td")[0].innerText && secret === rows[index - 1].getElementsByTagName("td")[2].innerText) {
                    row.style.display = "none"; // hide this row
                }
                else { // if secret or modelrange are not equal
                    row.style.display = "table-row"; // show this row
                }
            }
            else {
                row.style.display = "table-row"; // show this row
            }
        }
        index++;
    }
}  

请参阅http://xsltfiddle.liberty-development.net/bnnZWi的实际操作

暂无
暂无

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

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