Adamhumbug Posted November 14, 2019 Share Posted November 14, 2019 Hi all i have a few questions about a function i have to search my tables. The first questions is, how do i open this up to search any of the columns rather than just the first one. The second question is how do i open this function up to be used to search any of my tables. The code i have at the minute that works to search the first column is: function searchTable() { // Declare variables var input, filter, table, tr, td, i, txtValue; input = document.getElementById('customerTableSearch'); filter = input.value.toUpperCase(); table = document.getElementById('customerTable'); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { //column to search set below td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } I have tried quite a few things to open this up to more columns but i dont know how to get it to look at more than one column at a time. The second question about making this a more generic search function uses the below code: function searchTable(x, y) { var searchbox = x; var tablename = y; // Declare variables var input, filter, table, tr, td, i, txtValue; input = document.getElementById(searchbox); filter = input.value.toUpperCase(); table = document.getElementById(tablename); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } and then on the search box i have onkeyup='searchTable(customerTableSearch, customerTable)' this does not work and i am not sure where i am going wrong Quote Link to comment Share on other sites More sharing options...
denno020 Posted November 14, 2019 Share Posted November 14, 2019 The offending line that is only allowing you to search the first column is this one: td = tr[i].getElementsByTagName("td")[0]; The `[0]` portion of that statement means to literally get the first column only. An easy fix is to add a nested loop, that does essentially the same as the for loop for the `tr` element, but do it for `td`, provided `td` is assigned: td = tr[i].getElementsByTagName("td"); Simply move the if block inside that inner loop, and it should work (some variable names inside that second for loop will need to be updated) As for making this work with any table, I feel like the call to the function needs to pass a string. Without the quotes around customerTable, the code is going to assume that's a variable name, so maybe try updating to: onkeyup='searchTable("customerTableSearch", "customerTable")' These are all suggestions that require a minimal amount of refactoring of the current code, this doesn't mean I endorse that way it's written , there's lots of improvements that could be made for this to be much more maintainable in the future, but we'll leave that for another discussion Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted November 16, 2019 Author Share Posted November 16, 2019 I would deffo like any pointers about how its written for sure. I will try and follow your advice on this. Thanks all for your pointers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.