Live search through table rows

前端 未结 15 1862
失恋的感觉
失恋的感觉 2020-11-29 00:52

I want to do a live search through the table rows, using jQuery, the \"live\" word is the key, because I want to type the keywords in the text input, on the same site and I\

15条回答
  •  盖世英雄少女心
    2020-11-29 01:13

    Old question but i find out how to do it faster. For my example: i have about 10k data in my table so i need some fast search machine.

    Here is what i did:

    $('input[name="search"]').on('keyup', function() {
    
            var input, filter, tr, td, i;
    
            input  = $(this);
            filter = input.val().toUpperCase();
            tr     = $("table tr");
    
            for (i = 0; i < tr.length; i++) {
                td = tr[i].getElementsByTagName("td")[0]; // <-- change number if you want other column to search
                if (td) {
                    if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                        tr[i].style.display = "";
                    } else {
                        tr[i].style.display = "none";
                    }
                }
            }
        })
    

    Hope it helps somebody.

提交回复
热议问题