Filter three columns together in an HTML table prefilled based on the URL using JavaScript

Viewed 58

I would like to use the trailing content of a URL to filter an HTML table on three columns simultaneously. I want to do this so that a list of automatically generated URLs can be used in an external dataframe to produce a filtered table, rather than generating separate HTML files for each combination of levels for these columns.

So far, I've been able to parse the trailing URL text and fill in an input field that individually filters on three columns, and the filter functions but only on keyup:

https://eviemsm4.github.io/tabletemplate2.html?var1=a&var2=f&var3=germany

I'd like to filter all three columns together on page load, but I'm not quite sure how...

Any help would be much appreciated.

Here follows the code (note that the snippet will not pull in trailing information to pre-populate the column filters - please see the GitHub link above to the format of the trailing information):

function prefill() {
  var loc = location.href;
  var locationField1 = document.getElementById('myInput1');
  var locationField2 = document.getElementById('myInput2');
  var locationField3 = document.getElementById('myInput3');
  const var1 = loc.split('var1=').pop();
  var part1 = var1.substring(
    var1.lastIndexOf("var1=") + 1,
    var1.lastIndexOf("&var2=")
  );
  const var2 = loc.split('&var2=').pop();
  var part2 = var2.substring(
    var2.lastIndexOf("&var2=") + 1,
    var2.lastIndexOf("&var3=")
  );
  const var3 = loc.split('&var3=').pop();
  var part3 = var3.substring(
    var3.lastIndexOf("&var3=") + 1,
    var3.lastIndexOf("")
  );
  part1 = part1.replace(/%20/g, " ");
  part1 = part1.replace(/%2F/g, "/");
  part2 = part2.replace(/%20/g, " ");
  part2 = part2.replace(/%2F/g, "/");
  part3 = part3.replace(/%20/g, " ");
  part3 = part3.replace(/%2F/g, "/");
  locationField1.value = part1;
  locationField2.value = part2;
  locationField3.value = part3;
}

function performReset() {
  document.getElementById("inputName").value = "";
  document.getElementById("inputCity").value = "";
  document.getElementById("inputCountry").value = "";
  filterTable(event, 0);
}

function filterTable(event, index) {
  var filter = event.target.value.toUpperCase();
  var rows = document.querySelector("#myTable tbody").rows;
  for (var i = 0; i < rows.length; i++) {
    var firstCol = rows[i].cells[0].textContent.toUpperCase();
    var secondCol = rows[i].cells[1].textContent.toUpperCase();
    var thirdCol = rows[i].cells[2].textContent.toUpperCase();
    if ((firstCol.indexOf(filter) > -1 && index == 0) || (secondCol.indexOf(filter) > -1 && index == 1) || (thirdCol.indexOf(filter) > -1 && index == 2)) {
      rows[i].style.display = "";
    } else {
      rows[i].style.display = "none";
    }
  }
}


document.querySelectorAll('input.w3-input').forEach(function(el, idx) {
  el.addEventListener('keyup', function(e) {
    filterTable(e, idx);
  }, false);
});
<body onload="prefill()">

  <h2 class="w3-center">Filter Table Test</h2>

  <p class="w3-center"><button id="buttonReset" onclick="performReset()" class="w3-button w3-grey w3-hover-light-grey">Reset</button></p>

  <table id="myTable" class="w3-table w3-striped w3-hoverable">
    <!--Column Headings-->
    <thead>
      <tr>
        <td><input id="myInput1" class="w3-input" placeholder="Name..."></td>
        <td><input id="myInput2" class="w3-input" placeholder="City..."></td>
        <td><input id="myInput3" class="w3-input" placeholder="Country..."></td>
      </tr>
    </thead>
    <!--Row Data-->
    <tbody>
      <tr class="item">
        <td>Alfreds Futterkiste</td>
        <td>Berlin</td>
        <td>Germany</td>
      </tr>
      <tr class="item">
        <td>Berglunds snabbköp</td>
        <td>Lule </td>
        <td>Sweden</td>
      </tr>
      <tr class="item">
        <td>Centro comercial Moctezuma</td>
        <td>México D.F.</td>
        <td>Mexico</td>
      </tr>
      <tr class="item">
        <td>Ernst Handel</td>
        <td>Graz</td>
        <td>Austria</td>
      </tr>
      <tr class="item">
        <td>FISSA Fabrica Inter. Salchichas S.A.</td>
        <td>Madrid</td>
        <td>Spain</td>
      </tr>
      <tr class="item">
        <td>Galería del gastrónomo</td>
        <td>Barcelona</td>
        <td>Spain</td>
      </tr>
      <tr class="item">
        <td>Island Trading</td>
        <td>Cowes</td>
        <td>UK</td>
      </tr>
      <tr class="item">
        <td>Königlich Essen</td>
        <td>Brandenburg</td>
        <td>Germany</td>
      </tr>
      <tr class="item">
        <td>Laughing Bacchus Wine Cellars</td>
        <td>Vancouver</td>
        <td>Canada</td>
      </tr>
      <tr class="item">
        <td>Magazzini Alimentari Riuniti</td>
        <td>Bergamo</td>
        <td>Italy</td>
      </tr>
      <tr class="item">
        <td>North/South</td>
        <td>London</td>
        <td>UK</td>
      </tr>
      <tr class="item">
        <td>Paris spécialités</td>
        <td>Paris</td>
        <td>France</td>
      </tr>
      <tr class="item">
        <td>Rattlesnake Canyon Grocery</td>
        <td>Albuquerque</td>
        <td>USA</td>
      </tr>
      <tr class="item">
        <td>Simons bistro</td>
        <td>København</td>
        <td>Denmark</td>
      </tr>
      <tr class="item">
        <td>The Big Cheese</td>
        <td>Portland</td>
        <td>USA</td>
      </tr>
      <tr class="item">
        <td>Vaffeljernet</td>
        <td>Århus</td>
        <td>Denmark</td>
      </tr>
      <tr class="item">
        <td>Wolski Zajazd</td>
        <td>Warszawa</td>
        <td>Poland</td>
      </tr>
    </tbody>
  </table>

1 Answers

A quick fix would be to call your method filterTable function on load by passing one of the args you pass via the url props. You should call it in the prefill function

filterTable(var1 || var2 || var3, 0);

and in the filterTable you should alter the implementation slightly

Instead of var filter = event.target.value.toUpperCase();

You should use something like this:

var filter = typeof event === 'object' ? event.target.value.toUpperCase():
event.toUppercase()
Related