2017-04-25 100 views
0

我用我的頁面的w3School代碼,它工作正常,但它只過濾一列,不知道如何創建循環,但跳躍有更容易的解決方案。過濾表格多列

td = tr[i].getElementsByTagName("td")[0];  

改變這裏0變爲1列,可以找到任何東西這麼嘗試過許多不同的事情,但不是程序員不知道是否有任何其他的特性,可以讓多列,花了這麼多的搜索,請幫助

function myFunction() { 
 
    var input, filter, table, tr, td, i; 
 
    input = document.getElementById("myInput"); 
 
    filter = input.value.toUpperCase(); 
 
    table = document.getElementById("myTable"); 
 
    tr = table.getElementsByTagName("tr"); 
 
    for (i = 0; i < tr.length; i++) { 
 
    td = tr[i].getElementsByTagName("td")[0]; 
 
    if (td) { 
 
     if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
 
     tr[i].style.display = ""; 
 
     } else { 
 
     tr[i].style.display = "none"; 
 
     } 
 
    } 
 
    } 
 
}
* { 
 
    box-sizing: border-box; 
 
} 
 

 
#myInput { 
 
    background-image: url('/css/searchicon.png'); 
 
    background-position: 10px 10px; 
 
    background-repeat: no-repeat; 
 
    width: 100%; 
 
    font-size: 16px; 
 
    padding: 12px 20px 12px 40px; 
 
    border: 1px solid #ddd; 
 
    margin-bottom: 12px; 
 
} 
 

 
#myTable { 
 
    border-collapse: collapse; 
 
    width: 100%; 
 
    border: 1px solid #ddd; 
 
    font-size: 18px; 
 
} 
 

 
#myTable th, 
 
#myTable td { 
 
    text-align: left; 
 
    padding: 12px; 
 
} 
 

 
#myTable tr { 
 
    border-bottom: 1px solid #ddd; 
 
} 
 

 
#myTable tr.header, 
 
#myTable tr:hover { 
 
    background-color: #f1f1f1; 
 
}
<h2>My Customers</h2> 
 

 
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search    for names.." title="Type in a name"> 
 

 
<table id="myTable"> 
 
    <tr class="header"> 
 
    <th style="width:60%;">Name</th> 
 
    <th style="width:40%;">Country</th> 
 
    </tr> 
 
    <tr> 
 
    <td>Alfreds Futterkiste</td> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Berglunds snabbkop</td> 
 
    <td>Sweden</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Island Trading</td> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Koniglich Essen</td> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Laughing Bacchus Winecellars</td> 
 
    <td>Canada</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Magazzini Alimentari Riuniti</td> 
 
    <td>Italy</td> 
 
    </tr> 
 
    <tr> 
 
    <td>North/South</td> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Paris specialites</td> 
 
    <td>France</td> 
 
    </tr> 
 
</table>

+0

請使用除w3school之外的其他資源,這不是特別好(例如[* tables *]的MDN(https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement)和[* rows *](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/rows))。您可以使用'tr [i] .cells [0]'而不是'tr [i] .getElementsByTagName(「td」)[0]''。輸入少得多。 ;-)另外,請格式化你的代碼,閱讀起來越容易,人們就越有可能提供幫助。 – RobG

回答

7

有網絡比W3學校上significantly better資源,你真的應該避免網站海事組織。話雖這麼說,你只需要看看tr而非td S的一個,如果你想將整行匹配:

function myFunction() { 
    var input = document.getElementById("myInput"); 
    var filter = input.value.toUpperCase(); 
    var table = document.getElementById("myTable"); 
    var tr = table.getElementsByTagName("tr"); 
    for (var i = 0; i < tr.length; i++) { 
     if (tr.textContent.toUpperCase().indexOf(filter) > -1) { 
      tr[i].style.display = ""; 
     } else { 
      tr[i].style.display = "none"; 
     }  
    } 
} 

如果要篩選,而不是整個行多列,只需要使用OR (||)在你的病情:

function myFunction() { 
    var input = document.getElementById("myInput"); 
    var filter = input.value.toUpperCase(); 
    var table = document.getElementById("myTable"); 
    var tr = table.getElementsByTagName("tr"); 
    var tds = tr.getElementsByTagName('td'); 

    for (var i = 0; i < tr.length; i++) { 
     var firstCol = tds[0].textContent.toUpperCase(); 
     var secondCol = tds[1].textContent.toUpperCase(); 
     if (firstCol.indexOf(filter) > -1 || secondCol.indexOf(filter) > -1) { 
      tr[i].style.display = ""; 
     } else { 
      tr[i].style.display = "none"; 
     }  
    } 
} 

一些原因,本教程不是很大:您應該避免使用innerHTML而應使用textContent,因爲它可能是你的細胞將有HTML,並且用戶可以鍵入一個標籤當試圖搜索可見的文本並命名時的名稱由什麼匹配融合。你應該給你的函數名稱指明它們的作用(例如filterTable而不是myFunction)。此外,還有更簡單的方法來訪問表數據(例如tr.cells)。如果您將keyup事件偵聽器添加到#myInput,則每次調用此函數時都不需要查找該DOM節點。這裏有一個例子:

function filterTable(event) { 
 
    var filter = event.target.value.toUpperCase(); 
 
    var rows = document.querySelector("#myTable tbody").rows; 
 
    
 
    for (var i = 0; i < rows.length; i++) { 
 
     var firstCol = rows[i].cells[0].textContent.toUpperCase(); 
 
     var secondCol = rows[i].cells[1].textContent.toUpperCase(); 
 
     if (firstCol.indexOf(filter) > -1 || secondCol.indexOf(filter) > -1) { 
 
      rows[i].style.display = ""; 
 
     } else { 
 
      rows[i].style.display = "none"; 
 
     }  
 
    } 
 
} 
 

 
document.querySelector('#myInput').addEventListener('keyup', filterTable, false);
<input id="myInput" type="text" /> 
 
<table id="myTable"> 
 
<thead> 
 
    <tr class="header"> 
 
    <th style="width:60%;">Name</th> 
 
    <th style="width:40%;">Country</th> 
 
    </tr> 
 
</thead> 
 
<tbody> 
 
    <tr> 
 
    <td>Alfreds Futterkiste</td> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Berglunds snabbkop</td> 
 
    <td>Sweden</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Island Trading</td> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Koniglich Essen</td> 
 
    <td>Germany</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Laughing Bacchus Winecellars</td> 
 
    <td>Canada</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Magazzini Alimentari Riuniti</td> 
 
    <td>Italy</td> 
 
    </tr> 
 
    <tr> 
 
    <td>North/South</td> 
 
    <td>UK</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Paris specialites</td> 
 
    <td>France</td> 
 
    </tr> 
 
</tbody> 
 
</table>

+0

Rob u the man !!!我花了一些時間來弄清楚你做了什麼,但現在它的工作,多虧了一個密爾(最後一個工作) –

+0

值得一票就在第一句話。 ;-) – RobG

0

你只需要添加一個循環來在細胞上,所以:基於可能是你遵循相同的教程

table = document.getElementById("myTable"); 
// tr = table.getElementsByTagName("tr"); 
// Easier to use the rows collection: 
var tr = table.rows; 

for (i = 0; i < tr.length; i++) { 

    // Easier to use the cells collection 
    // td = tr[i].getElementsByTagName("td")[0]; 
    cells = row.cells; 

    // Loop over all the cells 
    for (var j=0, jLen=cells.length; j<jLen; j++) { 
    td = cells[j]; 

    // Existing loop 
    if (td) { 
     if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
     tr[i].style.display = ""; 
     } else { 
     tr[i].style.display = "none"; 
     } 
    } 
    } 
} 
+0

僅僅使用整個行的'innerHTML'然後遍歷每個單元格會更有意義嗎? –

+0

也感謝你RobG –

+0

@RobM。-sure或* textContent *,但我認爲循環遍歷單元格提供了更好的學習機會。 ;-) – RobG

0

,接受三種不同的文本輸入並相應地對所有行進行排序:

function SortByMultiple() { 
 

 
    //Declare needed variables 
 
    var dateInput, nameInput, locationInput, dateFilter, nameFilter, locationFilter, table, tr, tdN, tdD, tdL, i; 
 

 
    //Set inputs by getElementById 
 
    nameInput = document.getElementById('nameInput'); 
 
    dateInput = document.getElementById('dateInput'); 
 
    locationInput = document.getElementById('locationInput'); 
 
    //Set filters 
 
    nameFilter = nameInput.value.toUpperCase(); 
 
    dateFilter = dateInput.value.toUpperCase(); 
 
    locationFilter = locationInput.value.toUpperCase(); 
 
    //Set the table and tr variables 
 
    table = document.getElementById("UL"); 
 
    tr = document.getElementsByTagName("tr"); 
 

 
    //Loop through items and hide those that don't match the query --> 
 
    for (i = 0; i < tr.length; i++) { 
 

 
     //Name is at index 0 
 
     tdN = tr[i].getElementsByTagName("td")[0]; 
 
     //Date is at index 2 
 
     tdD = tr[i].getElementsByTagName("td")[2]; 
 
     //Location is at index 1 
 
     tdL = tr[i].getElementsByTagName("td")[1]; 
 

 
     if (tdN.innerHTML.toUpperCase().indexOf(nameFilter) > -1 && tdD.innerHTML.toUpperCase().indexOf(dateFilter) > -1 && tdL.innerHTML.toUpperCase().indexOf(locationFilter) > -1) { 
 
      tr[i].style.display = ""; 
 
     } 
 
     else { 
 
      tr[i].style.display = "none"; 
 
     } 
 
    } 
 

 
}

通過在AND之後設置條件,它們都將在過濾器中考慮在內!