순수한 JavaScript를 사용하여 테이블의 항목을 검색하고 필터링합니다.

다음은 테이블에서 항목을 검색하고 필터링하는 코드입니다.
테이블을 생성하는 HTML 코드입니다.


<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <title>Search and Filter Using JavaScript!</title>
</head>

<body>
    <div class="main w-50 my-4 py-4 m-auto">
        <input type="search" class="form-control" placeholder="Type Here To Search" id="inp" onkeyup="searchFunction()">
        <table id="table" class="my-4 py-4 table table-dark table-hover">
            <thead>
                <tr>
                    <th scope="col">S.no</th>
                    <th scope="col">Name</th>
                    <th scope="col">Profession</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th scope="row">1</th>
                    <td>Sudip</td>
                    <td>FullStack Web Developer</td>
                </tr>
                <tr>
                    <th scope="row">2</th>
                    <td>Ram</td>
                    <td>Frontend Web Developer</td>
                </tr>
                <tr>
                    <th scope="row">3</th>
                    <td>Ramesh</td>
                    <td>Backend Web Deveoper</td>
                </tr>
                <tr>
                    <th scope="row">4</th>
                    <td>Shyam</td>
                    <td>App Developer</td>
                </tr>
                <tr>
                    <th scope="row">5</th>
                    <td>Hari</td>
                    <td>Graphics Designer</td>
                </tr>
                <tr>
                    <th scope="row">6</th>
                    <td>Arjun</td>
                    <td>Video Editor</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
        crossorigin="anonymous"></script>
</body>

</html>


이 코드는 JavaScript 코드를 추가하여 작동하도록 하는 것보다 간단한 검색 표시줄과 테이블을 생성합니다.
**

JS 코드



function searchFunction() {
      // Declare variables
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("inp");
      filter = input.value.toUpperCase();
      table = document.getElementById("table");
      tr = table.getElementsByTagName("tr");

      // Loop through all table rows, and hide those who don't match the search query
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[0];
        if (td) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }
      }
    }

JS 코드는 작업을 수행하는 논리를 제공합니다.

최종 파일 index.html :


<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <title>Search and Filter Using JavaScript!</title>
</head>

<body>
    <div class="main w-50 my-4 py-4 m-auto">
        <input type="search" class="form-control" placeholder="Type Here To Search" id="inp" onkeyup="searchFunction()">
        <table id="table" class="my-4 py-4 table table-dark table-hover">
            <thead>
                <tr>
                    <th scope="col">S.no</th>
                    <th scope="col">Name</th>
                    <th scope="col">Profession</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th scope="row">1</th>
                    <td>Sudip</td>
                    <td>FullStack Web Developer</td>
                </tr>
                <tr>
                    <th scope="row">2</th>
                    <td>Ram</td>
                    <td>Frontend Web Developer</td>
                </tr>
                <tr>
                    <th scope="row">3</th>
                    <td>Ramesh</td>
                    <td>Backend Web Deveoper</td>
                </tr>
                <tr>
                    <th scope="row">4</th>
                    <td>Shyam</td>
                    <td>App Developer</td>
                </tr>
                <tr>
                    <th scope="row">5</th>
                    <td>Hari</td>
                    <td>Graphics Designer</td>
                </tr>
                <tr>
                    <th scope="row">6</th>
                    <td>Arjun</td>
                    <td>Video Editor</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
        crossorigin="anonymous"></script>
</body>
<script>
    function searchFunction() {
      // Declare variables
      var input, filter, table, tr, td, i, txtValue;
      input = document.getElementById("inp");
      filter = input.value.toUpperCase();
      table = document.getElementById("table");
      tr = table.getElementsByTagName("tr");

      // Loop through all table rows, and hide those who don't match the search query
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[0];
        if (td) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }
      }
    }
    </script>

</html>

좋은 웹페이지 즐겨찾기