简体   繁体   中英

Filter/Search Table HTML

I have a problem with search table. I'm using code from W3 and this code is for filtering one column. Will someone make it so that it filters two columns at a time? For example, Name and Lastname

Code:

<script> 
function myFunction() { 

    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"; 
            } 
        }
     }
 } 
 </script>

Link to code: https://www.w3schools.com/howto/howto_js_filter_table.asp

THANK YOU!

You should search for second td also.

function myFunction() {    
    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]; 
        td2 = tr[i].getElementsByTagName("td")[1];
        if (td && td2) { 
           if (td.innerHTML.toUpperCase().indexOf(filter) > -1 || td2.innerHTML.toUpperCase().indexOf(filter) > -1) { 
             tr[i].style.display = ""; 
           } 
           else { tr[i].style.display = "none"; } 
        }
    }
} 

You can use a simple js framework called Listjs

http://listjs.com/ to search, sort or filter data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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