Home > Enterprise >  How to use multiple search bar in a HTML table with javascript?
How to use multiple search bar in a HTML table with javascript?

Time:10-05

so for the moment i got a table with 2 columns and 2 search bar that each search only in their specific column. Obviously since my 2 search bar uses their specific function i can't search with the 2 search bar at the same time, for exemble if i type 'Alexis' in the name and then 'Germany' in the country it will forget the fact that i have typed 'Alexis' since the last function i use is 'searchCountry'. So i would like to know if their is a way to maybe combinate the 2 function or someting else so that i can use them together.

function searchName() {
  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")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

function searchCountry() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput2");
  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";
      }
    }
  }
}
* {
  box-sizing: border-box;
}

#myInput,
#myInput2 {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,
#myTable tr:hover {
  background-color: #f1f1f1;
}
<input type="text" id="myInput" onkeyup="searchName()" placeholder="Search for names.." title="Type in a name">
<input type="text" id="myInput2" onkeyup="searchCountry()" placeholder="Search for country.." title="Type in a name">
<table id="myTable">
  <tr >
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Alexis</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Alexis</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Alexis</td>
    <td>France</td>
  </tr>
</table>

Thank you for your help.

CodePudding user response:

try this

function search() {
    var inputName,
        inputCountry,
        filterName,
        filterCountry,
        table,
        tr,
        td,
        i,
        name,
        country;
    inputName = document.getElementById("myInput");
    inputCountry = document.getElementById("myInput2");

    filterName = inputName.value.toUpperCase();
    filterCountry = inputCountry.value.toUpperCase();

    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i  ) {
        td = tr[i].getElementsByTagName("td")[0];
        td1 = tr[i].getElementsByTagName("td")[1];
        if (td && td1) {
            name = (td.textContent || td.innerText).toUpperCase();
            country = (td1.textContent || td1.innerText).toUpperCase();
            if (
                name.indexOf(filterName) > -1 &&
                country.indexOf(filterCountry) > -1
            ) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}
* {
  box-sizing: border-box;
}

#myInput,
#myInput2 {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,
#myTable tr:hover {
  background-color: #f1f1f1;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <input type="text" id="myInput" onkeyup="search()" placeholder="Search for names.." title="Type in a name">
    <input type="text" id="myInput2" onkeyup="search()" placeholder="Search for country.." title="Type in a name">
    <table id="myTable">
        <tr >
            <th style="width:60%;">Name</th>
            <th style="width:40%;">Country</th>
        </tr>
        <tr>
            <td>Alfreds Futterkiste</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Berglunds snabbkop</td>
            <td>Sweden</td>
        </tr>
        <tr>
            <td>Island Trading</td>
            <td>UK</td>
        </tr>
        <tr>
            <td>Koniglich Essen</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Alexis</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Alexis</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Alexis</td>
            <td>France</td>
        </tr>
    </table>
    <script src="script.js"></script>
   
</body>
</html>

  • Related