1

I have a table with student details and I want to filter each row. Like when "ID" and "Name" is entered and filter button is clicked the row with that details has to be displayed. I tried using JavaScript it does filter single rows separately. Is there any approach where I can filter them together or how can I write one single function for separate filter textboxes.

enter image description here

<thead>
  <th>
    <input
      type="text"
      id="myInput1"
      class="form-control filter-input"
      placeholder="Search by ID..."
      data-column="0"
    />
  </th>

  <th>
    <input
      type="text"
      id="myInput"
      onkeyup="myFunction()"
      class="form-control filter-input"
      placeholder="Search by Name..."
      data-column="1"
    />
  </th>

  <th>
    <input
      type="text"
      id="myInput2"
      onkeyup="myFunction2()"
      class="form-control filter-input"
      placeholder="Search by YOP..."
      data-column="3"
    />
  </th>

  <th>
    <input
      type="text"
      id="myInput3"
      onkeyup="myFunction3()"
      class="form-control filter-input"
      placeholder="Search by Status..."
      data-column="4"
    />
  </th>

  <th>
    <button
      onclick="myFunctionID()"
      class="btn btn-success btn-block mobile-width"
    >
      <i class="fa fa-filter"></i> Filter
    </button>
  </th>
</thead>

<script>
  function myFunction() {
    var input, filter, table, tr, td, i, txtValue

    input = document.getElementById("myInput")
    filter = input.value.toUpperCase()
    table = document.getElementById("myTable")
    tr = table.getElementsByTagName("tr")

    for (i = 0; i < tr.length; i++) {
      td = tr[i].getElementsByTagName("td")[1]

      if (td) {
        txtValue = td.textContent || td.innerText

        if (txtValue.toUpperCase().indexOf(filter) > -1) {
          tr[i].style.display = ""
        } else {
          tr[i].style.display = "none"
        }
      }
    }
  }

  function myFunctionID() {
    var input, filter, table, tr, td, i, txtValue

    input = document.getElementById("myInput1")
    filter = input.value.toUpperCase()
    table = document.getElementById("myTable")
    tr = table.getElementsByTagName("tr")

    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>

I have used same function and have changed only the array index for each column.

9
  • 1
    best way use datatable,js sample : datatables.net/examples/plug-ins/range_filtering.html Commented Dec 15, 2022 at 9:14
  • my toggle button doesnot work inside datatable. Commented Dec 15, 2022 at 9:15
  • why ? solving this problem easier than make filtering table - this comment is a suggestion Commented Dec 15, 2022 at 9:26
  • So, you want to filter on keyup or on button click? Commented Dec 15, 2022 at 9:30
  • with great trouble I made it, I don't want to go back there again... stackoverflow.com/questions/74488254/… Commented Dec 15, 2022 at 9:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.