waiting to display information until it is necessary

I am currently working on optimizing my website for improved loading speed and responsiveness. Users can scroll through up to 4k images, apply filters, and sort them based on their preferences.

Below is the code snippet for my filtering function:

function filter(){
// NEED A WAY TO IMPLEMENT MULTI-FILTERING HERE
var input, filter, span, txtValue, i, a;
input = document.getElementById('userFilter'); // user's input
filter = input.value.toUpperCase(); // capitalize user input
itemWrappers = document.getElementsByClassName('itemWrapperColumns');

for (i = 0; i < itemWrappers.length; i++){
    a = itemWrappers[i];
    txtValue = a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
        itemWrappers[i].style.display = ""; // need to hide the item container, not just the span as per the comment
    }else{
        itemWrappers[i].style.display = "none";
    }
}

}

The above code worked fine when all images were loaded with a long scroll bar. However, I implemented a way to defer image rendering until the user scrolls down using

content-visibility: auto;

Since making this change, the filtering functionality has been affected. The filter now only applies to the items visible on the screen, while those yet to be rendered are not included in the filter logic.

Even though the elements of these unrendered items exist on the page, they do not show up due to the content-visibility property.

If anyone has suggestions on how to efficiently render a large number of images with sorting and filtering capabilities, I am open to alternative solutions.

Thank you in advance for your help.

Edit: For additional context, I am looking to hide the top-level div wrapper that contains everything see image description here

Answer №1

In this section, I have included a visual representation with cards for your images, allowing you to add headings to describe the scenes they represent and enabling users to search through each heading. You can also hide (but not remove) so that searching can be done in the background based on the headings.
Comments have been added in the code for clarity.

document.getElementById("search").addEventListener("input", searchDeviceFunc);

function searchDeviceFunc() {
  let wordFound = false; // This flag will turn true when a matching word is found
  var deviceCardHeadHTML = document.getElementsByClassName("deviceCardHead");
  
  // Loop through each heading text
  for (let i = 0; i < deviceCardHeadHTML.length; i++) {
    var deviceCardHeadText = deviceCardHeadHTML[i].textContent;
    var searchBarValue = document.getElementById("search").value; // Get input from the search bar
    var searchBarValueNeutral = searchBarValue.toLowerCase();
    
    if (searchBarValue != '') { // Execute only if search bar is not empty
      deviceCardHeadHTML[i].innerHTML = deviceCardHeadText.replace(new RegExp(searchBarValue, 'gi'), (match) => `<span class="highlight">${match}</span>`);
      
      if (deviceCardHeadHTML[i].textContent.toLowerCase().indexOf(searchBarValueNeutral) > -1) {
        wordFound = true; // Set flag to true as word is found
        deviceCardHeadHTML[i].parentElement.style.display = "block"; 
      } else {
        deviceCardHeadHTML[i].parentElement.style.display = "none"; // Hide element if search term does not match
      }
    } else {
      deviceCardHeadHTML[i].innerHTML = deviceCardHeadText;
      deviceCardHeadHTML[i].parentElement.style.display = "block"; // Show all elements
    }
  }
}
.highlight {
  background: yellow;
}

.deviceCardFlex {
  display: flex;
  flex-wrap: wrap;
  padding-left: auto;
  padding-right: auto;
  justify-content: flex-start;
}

.deviceCard {
  position: relative;
  width: 300px;
  height: 300px;
  margin: 2% 2%;
  padding: 0 1% 1%;
  border: 2px solid rgb(126, 126, 126);
  border-radius: 5px;
  background-color: white;
  transition: 0.3s;
}
<div id="devicesBtnData">
  <div class="searchDevice">
    <span class="searchDeviceBtn">Search Device</span>
    <input id="search" type="search" placeholder="Try it">
    <br>
  </div>

  <div class="deviceCardFlex">
    <div class="deviceCard">
      <h3 class="deviceCardHead">Laptop Pro</h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Lenova Yoga </h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Yatch</h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Aeroplane</h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Lenova Yoga Laptop Pro</h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Vistara</h3>
    </div>
    <div class="deviceCard">
      <h3 class="deviceCardHead">Quwait Airliner</h3>
    </div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS background images struggle to display properly in Safari, often not appearing even when checked in the debugger

I'm currently working on a website that I want to make responsive. I have set up a div ID that is supposed to appear once the width of a mobile device reaches a certain point. Here is the CSS code: #mobilenavbuttons1 { max-width: 750px; padding-top: ...

Ensure that text is aligned alongside an image only when both elements are enclosed within <p> tags

When using an API to pull markdown content from Contentful and converting it to HTML with ReactMarkdown, a common issue arises. The resulting HTML structure often places the text content in separate paragraphs such as: <p>Some text content</p> ...

A guide on efficiently inserting multiple rows containing JSON data into a MySQL database simultaneously using node.js

I'm facing an issue with inserting multiple values into columns simultaneously. Let's say I have JSON data consisting of two rows of information, and I want to insert both rows into my table at one go. My attempt looks like this: var data = [&apo ...

Enhancing Win8 apps with AppendTo/jquery-win8

Recently, I've been intrigued by the ToDoMVC samples and decided to try porting them into a Windows 8 JS app. I thought it would be as simple as copying and pasting the code while making sure to reference the necessary WinJS libraries. However, I soo ...

Include the <script> tag in the HTML code for an iframe without running it

Currently, I am working on an HTML code that involves memory for an Iframe. Interestingly, whenever I use the append function, it not only executes the code but also carries out its intended task. html = $(parser.parseFromString($("#EHtml").val(), "text/h ...

Modifying the input field's name attribute while utilizing multiple datasets in typeahead.js

I am currently utilizing typeahead.js with multiple datasets, following the guidance provided here. I have chosen not to employ Bloodhound in my implementation, resulting in some differences in my code structure, outlined below: $('#search .typeahead ...

Having trouble initializing a variable in a function and accessing it from a separate function

Currently, I am executing a node js file which is receiving a post request as shown below. app.post('/', function (req, res) { var firstLine = req.body.firstLine; var secondLine = req.body.secondLine; var previewID = req.body.preview ...

Injecting multiple instances of an abstract service in Angular can be achieved using the following techniques

I am fairly new to Angular and currently trying to make sense of the code written by a more experienced developer. Please excuse me if I'm not adhering to the standard communication practices and vocabulary. There is an abstract class called abstract ...

Tips on when to display the "Email Confirmation" input text box only after updating the old email

Oh no!! Yes, that's exactly what I desire! I've been facing obstacles in trying to understand how to display the "Email Confirm" input text-box ONLY when the old email has been updated. Can someone point out where I might have gone wrong? :( ...

Analyzing - Dynamically Tagging Method - Implementing direct call regulations- Erase enduring variables

https://i.sstatic.net/elGJz.jpg Hello there, I am currently utilizing a Direct call rule within DTM. When I click on a href link that opens in a new window, I want to remove or clear the eVars and events associated with the click. I have implemented custo ...

Reorganize code in JavaScript and TypeScript files using VSCode

Is there a way to efficiently organize the code within a .js / .ts file using Vscode? Specifically, when working inside a Class, my goal is to have static variables at the top, followed by variables, then methods, and so on automatically. I did some resea ...

Inadvertent scroll actions causing unexpected value changes in Material UI sliders

I am currently working on a React application that utilizes Material UI, with slider components integrated throughout the interface. However, I have encountered an issue when using a touch screen device - unintentional changes to the slider values occur wh ...

Error: The function $(...).froalaEditor is not recognized | This issue is occurring within the $(document).ready(function(){}) block in J

When attempting to set HTML content of the Froala editor within a JSP using $(document).ready(), this error occurs. TypeError: $(...).froalaEditor is not a function | $(document).ready(function(){}) in JSP I read on a GitHub issue that placing jQuery sc ...

My function doesn't seem to be cooperating with async/await

const initialState={ isLoggedIn: false, userData: null, } function App() { const [state, setState]= useState(initialState) useEffect(()=>{ async function fetchUserData(){ await initializeUserInfo({state, setState}) // encountering an ...

Is there a simple method in JavaScript to combine, structure, and join numerous multi-dimensional arrays in a specific manner (from right to left)?

Looking for a simple solution to merge, flatten, and concatenate multiple multi-dimensional arrays in JavaScript in a specific manner (from right to left) # Example [['.class1', '.class2'], ['.class3', ['.class4', & ...

Full-width sub menu within the menu

I'm trying to make a sub menu appear below my main menu that is the same width as the main menu, which has a fixed width. I want the sub menu to adjust its width based on the number of links it contains. Is this even possible? Here's the code I h ...

What is the most reliable method for converting a 32-bit unsigned integer to a big endian byte array?

Looking for a simple and reliable method to convert an unsigned integer into a four-byte-array in big endian format, with padding if necessary? Take a look at this example: Input value: 714 Output: Resulting byte array [ 0xca, 0x02, 0x00, 0x00 ]; By the ...

Insert HTML content into an iframe with a callback function

We are receiving information from the backend server and need to transfer it to an iframe. In order to accurately set the height of the iframe to match the content, we must wait for the content to be loaded into the iframe. However, this process may not ha ...

An exploration on integrating a controller into an Angular directive class using Typescript

Here's the TypeScript code for an Angular directive class I've been working on: I'm wondering how I can incorporate a controller into this directive without creating a separate controller class. My goal is to write and inject the ISOLATE SC ...

Is Your CanvasJS Chart Traveling in Reverse?

My charts are displaying dates in reverse order, can anyone help me figure out what's causing this issue? I've checked the documentation but couldn't find anything that would explain this problem. Link to documentation: Here is a screensh ...