Filtering table multiple columns

前端 未结 14 1680
闹比i
闹比i 2020-12-09 13:31

I used w3School code for my page and it works fine but it only filters one column, don’t know how create loops but hopping there is easier solution.

    td =         


        
14条回答
  •  自闭症患者
    2020-12-09 14:18

    Code that works for filtering multiple columns dynamically. No changes needed.

        function myFunction(elem) {
            var input, filter, table, tr, td, i, txtValue;
            input = elem;
            filter = input.value.toUpperCase();
            table = document.getElementById("YourTableId");
            tr = table.getElementsByTagName("tr");
            for (i = 1; i < tr.length; i++) {
                tds = tr[i].getElementsByTagName("td");
                var matches = false;
    
                for (j = 0; j < tds.length; j++) {
                    if (tds[j]) {
                        txtValue = tds[j].textContent || tds[j].innerText;
                        if (txtValue.toUpperCase().indexOf(filter) > -1) {
                            matches = true;
                        } 
                    }
                }
    
                if(matches == true)
                {
                    tr[i].style.display = "";
                }
                 else {
                        tr[i].style.display = "none";
                    }
    
                }
            }
    

提交回复
热议问题