how to filter extact word

RAVI 1,076 Reputation points
2023-08-03T07:27:02.5133333+00:00

Hello

In my aspx page i have one texbox one gridview and im using this code to filter gridview in Client Side but whenever i type my word it should bring all match data only not like For example I have Apple And Apple USA when i give Apple It should show only Apple Data Not Apple USA Data If i type Apple USA then it has to show me only Apple USA Data

whats to change in this javascript please have a look

<script type="text/javascript">
            function Search_Gridview(strKey) {
                var strData = strKey.value.toLowerCase().split(" ");
                var tblData = document.getElementById("<%=GridView1.ClientID %>");
                var rowData;
                for (var i = 1; i < tblData.rows.length; i++) {
                    rowData = tblData.rows[i].innerHTML;
                    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;
                }
            }   
        </script>
        

thanking you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,057 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,181 Reputation points Microsoft External Staff
    2023-08-03T10:09:22.87+00:00

    Hi @RAVI,

    You can modify to the code below.

    JavaScript String indexOf():returns the position of the first occurrence of a value in a string.As long as rowData.toLowerCase() contains strData[j], the judgment of rowData.toLowerCase().indexOf(strData[j]) >= 0 is established.

    It can directly match the data of the gridview column (cell[i], i starts from 0. So i is 1 in my example).

    <script type="text/javascript">
            function Search_Gridview(strKey) {
                var strData = strKey.value.toLowerCase();          
                var tblData = document.getElementById("<%=GridView1.ClientID %>");
                var rowData;
                for (var i = 1; i < tblData.rows.length; i++) {
                    rowData = tblData.rows[i].cells[1].innerHTML;
                    rowData = rowData.toLowerCase();
                    var styleDisplay = 'none';
                    if (rowData == strData)
                        styleDisplay = '';
                    else {
                        styleDisplay = 'none';
    
                    }
                    tblData.rows[i].style.display = styleDisplay;
                }
            }
        </script>
    

    enter image description here

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.