Share via

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

Microsoft 365 and Office | Development | Office JavaScript API
Developer technologies | ASP.NET Core | Other

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Lan Huang-MSFT 30,221 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.

    Was this answer helpful?