dr5hn / countries-states-cities-database

🌍 Discover our global repository of countries, states, and cities! 🏙️ Get comprehensive data in JSON, SQL, PSQL, XML, YAML, and CSV formats. Access ISO2, ISO3 codes, country code, capital, native language, timezones (for countries), and more. #countries #states #cities
https://dr5hn.github.io/countries-states-cities-database/
Open Data Commons Open Database License v1.0
6.81k stars 2.44k forks source link

Clearing search is not rendering the whole list #785

Open StellarLogic opened 3 weeks ago

StellarLogic commented 3 weeks ago

Clearing search is not rendering the whole list

Steps to reproduce

  1. Search for any country
  2. Then clear the search bar using the clear button inside search bar
  3. Observe whole list is not visible as search is cleared

Screenshot 2024-06-11 095335

dosubot[bot] commented 3 weeks ago

Hello @StellarLogic! I'm a bot here to help you with bugs, questions, and contributions. Let's work together to solve the issue you're facing.

To resolve the issue where clearing the search bar does not render the whole list of items in the Countries, States, and Cities columns, you need to ensure that the filter function handles the case when the search input is empty. Here is an example of how you can modify the filter function to achieve this:

function filter(type) {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById('search-' + type);
  filter = input.value.toUpperCase();
  table = document.getElementById(type);
  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 || filter === "") {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

In this code, the condition txtValue.toUpperCase().indexOf(filter) > -1 || filter === "" ensures that if the search input is empty (filter === ""), all rows will be displayed. This should resolve the issue of not rendering the whole list of items when the search bar is cleared [1][2].

To continue talking to Dosu, mention @dosu.