2

I am looking to modify the bootstrap filter to search for multiple keywords.

In the example below, you can only search for one continuous keyword, such as "john" or "doe. Whereas I am looking to type in "oh do" (or "do oh") and it should return the first row of "john doe john@example.com".

I have been searching for some other examples such as: this and this. but I can't quite get that to work, and the first example is quite slow compare to the example posted above...

I manage to figure if i can somehow do this with toggle:

$(this).toggle($(this).text().toLowerCase().indexOf(value1) > -1 && $(this).text().toLowerCase().indexOf(value2) > -1)

then it would return the result that I am looking for, it would be easy if it is always two keywords, then this would work perfectly fine. But in a real world scenario, in a larger table, we can never predict how many keywords an user will put in, it could be 4 or 5, or could be 1... and being new in coding and only have been working in python/django doesn't help to understand and modify this jquery code to achieve the result...

is there anyone out there that can give it a shot?

$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Filterable Table</h2>
  <p>Type something in the input field to search the table for first names, last names or emails:</p>  
  <input class="form-control" id="myInput" type="text" placeholder="Search..">
  <br>
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody id="myTable">
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@mail.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@greatstuff.com</td>
      </tr>
      <tr>
        <td>Anja</td>
        <td>Ravendale</td>
        <td>a_r@test.com</td>
      </tr>
    </tbody>
  </table>
  
  <p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
</div>
</body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ksl
  • 23
  • 5

1 Answers1

1

Not short code but you can try this

function checkInput(element, value) {
  value = value.split(' ');
  for (var i = 0; i < value.length; i++) {
    if (value[i] && element.text().toLowerCase().indexOf(value[i]) == -1) return false;
  }
  return true;
}

$(document).ready(function() {
  $("#myInput").on("input", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle(checkInput($(this), value))
    });
  });
});

using es6 every() all browser and IE9+

$(this).toggle(
    value.split(' ').every(val => $(this).text().toLowerCase().indexOf(val) > -1)
)
ewwink
  • 18,382
  • 2
  • 44
  • 54