Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using this javascript code for my grid view in asp.net (webform) To search Vehicle no column, code work Slower for 2000 records how can I make it faster this is my code.

this is my code.
JavaScript
function Search_GridviewVehicleNo(strKey) {
    var strData = strKey.value.toLowerCase().split(" ");
    var tblData = document.getElementById('<%= GridViewVehicle.ClientID %>');
    var rowData;
    for (var i = 1; i < tblData.rows.length; i++) {
        rowData = tblData.rows[i].cells[2].innerText;
        var styleDisplay = 'none';
        for (var j = 0; j < strData.length; j++) {
            if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
                styleDisplay = '';
            else {
                styleDisplay = 'none';
                break;
            }
        }
        tblData.rows[i].style.display = styleDisplay;
    }
}

tblData.rows[i].cells[2].innerText; is column Vehicle number.

thanks

What I have tried:

C#
function Search_GridviewVehicleNo(strKey) {
   var strData = strKey.value.toLowerCase().split(" ");
   var tblData = document.getElementById('<%= GridViewVehicle.ClientID %>');
   var rowData;
   for (var i = 1; i < tblData.rows.length; i++) {
       rowData = tblData.rows[i].cells[2].innerText;
       var styleDisplay = 'none';
       for (var j = 0; j < strData.length; j++) {
           if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
               styleDisplay = '';
           else {
               styleDisplay = 'none';
               break;
           }
       }
       tblData.rows[i].style.display = styleDisplay;
   }
}
Posted
Updated 10-Oct-20 10:10am
v2
Comments
Richard MacCutchan 10-Oct-20 11:06am    
Why do you want 2000 records in a view? Your users are not going to be happy scrolling through that lot.
F-ES Sitecore 10-Oct-20 12:53pm    
I would implement an api that lets you search for data rather than searching through the html. The api would search the raw data and would probably be pretty quick. You could use the results of the api to manipulate the table you have, or you could simply create a new table from the results.

1 solution

Richard & F-ES had already pointed better way of doing it. In case you still want to continue current approach, I would suggest you to make use of jQuery Filters[^]

Example usage:
JavaScript
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>

Quote:
We use jQuery to loop through each table rows to check if there are any text values that matches the value of the input field. The toggle() method hides the row (display:none) that does not match the search. We use the toLowerCase() DOM method to convert the text to lower case, which makes the search case insensitive (allows "john", "John", and even "JOHN" on search).


if above is still slow, try using mix of .find & .filter, like:
JavaScript
var allCells = table.find('td');
selectedCells = allCells.filter(....);

Refer: jQuery find() Method[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900