Realtime table searching optimization
I have developed a realtime table filter that works very well on small tables, but when you get table that has many rows it gets choppy. It runs especially slow on older computers/browsers. By looking at my code below, is there anything that can be done to make it speedier? As a last resort, i considered using ajax to reach out to the server instead…
- //Start of document.ready
- $(document).ready(function () {
- var selectedAccountGroup = $("#filter").val();
- var accountsTableData = $("table#" + selectedAccountGroup + " tbody").find('tr.searchable'); //Real-time Filter
- $("#filter").keyup(function (e) {
- var currentFilter = $(this).val();
- var blankString = ""; //Run Optimized Search
- searchValueOptimized(currentFilter);
- });
- function searchValueOptimized(currentFilter) {
- var rowCounter = 0;
- for(i = 0; i < accountsTableData.length; i++) {
- var $currentRow = accountsTableData.eq(i);
- var $cellsInRow = $currentRow.find('td');
- if($cellsInRow.text().search(new RegExp(currentFilter, "i")) < 0) {
- $currentRow.hide();
- } else {
- $currentRow.show(); //Set Even Row Colors
- if(rowCounter % 2 == 0) {
- $cellsInRow.css("background-color", "#E6E6E6");
- } //Set Odd Row Colors
- else {
- $cellsInRow.css("background-color", "#FFFFFF");
- }
- rowCounter++;
- }
- }
- }
- });