Picture unloads from a refined roster

I am working on a filtered image list that has buttons to change the filter, which in turn changes the rendered list of images.

The issue I am facing is that sometimes when switching back and forth between filters, the images get deloaded and need to be downloaded again.

Even after trying to use v-once, the problem still persists.

Check out my Codepen project here

<button v-on:click="filter('all')">All</button>
  <button v-on:click="filter('tag1')">Tag 1</button>
  <button v-on:click="filter('tag2')">Tag 2</button>

  <div class="list-complete" tag="section">
    <div
      v-for="item in filteredItems"
      v-bind:key="item.id"
      class="list-complete-item"
    >
      <img class="list-complete-img" :src="item.img" alt="" />
  </div>
</div>
methods: {
  filter(tag) {
    this.currentTag= tag;
  }
},
computed: {
  filteredItems: function() {
    var filter = this.currentTag;
    return this.items.filter(function(item) {
        return item.tags.indexOf(filter) !== -1;
    });
  }
}

Strange enough, the issue is not always reproducible in the Codepen example (images deloading only sometimes), but in my local development environment, the images deload every time I toggle the filters.

I am considering whether it would be possible to modify the Vue computed filters to just make the images display none instead of directly removing them from the DOM like with v-show?

Answer №1

The main issue here is that when filtering, the img elements are re-rendered, causing the images to be fetched again.

To address this problem, I have made some changes. Instead of using a computed property, I now render the full list. The filtering is achieved by toggling visibility with the help of the v-show directive on the .list-complete-item element.

new Vue({
  el: '#list-complete-demo',
  data: {
    items: // Array containing various image objects with different tags and URLs
    currentTag: 'all'
  },
  methods: {
    shuffle: function() {
      // Shuffles the items in the list
    },
    filter: function(tag) {
      this.currentTag = tag;
    }
  }
})
.list-complete {
  /* CSS styling for displaying the items */
}

.list-complete-item {
  /* CSS styling for individual item container */
}

.list-complete-img {
  /* CSS styling for the images within each item */
}

/* Additional CSS transitions for adding/removing items */

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script>
<div id="list-complete-demo" class="demo">
  <button v-on:click="shuffle">Shuffle</button> Filter:
  <button v-on:click="filter('all')">All</button>
  <button v-on:click="filter('tag1')">Tag 1</button>
  <button v-on:click="filter('tag2')">Tag 2</button>

  <div class="list-complete" tag="section">
    <template v-for="item in items">
    <div
      v-bind:key="item.id"
      class="list-complete-item"
      v-show="item.tags.includes(currentTag)"
    >
      <img class="list-complete-img" :src="item.img" alt="" />
  </div>
  </template>
  </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

Link to download an Excel spreadsheet

My server is capable of generating excel files dynamically, and I am utilizing AJAX to download these dynamic excel files. Upon successful completion in the callback function, I receive the data for the excel file. $.ajax({ url: exporting. ...

Javascript is handling the JSON Request flawlessly, however, when using Nodejs, an error is encountered with a status

I'm trying to fetch a simple JSON File in NodeJS. Using Javascript and jQuery, it functions perfectly: $(document).ready(function() { $.getJSON('https://www.younow.com/php/api/broadcast/info/curId=0/user=Peter', function(json) { if ...

React component is being rendered, but it is not mounting properly, so it is unable

In my FillForm functional component, I am calling a list of objects to be rendered sequentially within the FormFiller function. The components are rendering correctly, but I encounter an error when trying to change their internal state. Warning: Can&apos ...

Tips to prevent the return of undefined when a condition is not satisfied

I'm currently working on a react app and I have an array of objects (items) that I want to iterate over in order to display each object based on its index. I am using useState to set the index, which is updated when the user clicks a button. const ...

The "computed" property in the component options should be defined as an object

I encountered an error in Vue v2.4.1 and I'm unsure about its meaning: [Vue warn]: component option "computed" should be an object. This is how my code appears: export default { data() { return { bookings: [], ...

update and refill the dropdown menu

I have successfully created a select box using PHP code to display items, but now I need help with dynamically refreshing this select box when new items are added. Specifically, I want the select box to update without refreshing the entire page when an i ...

When using JavaScript's async await, a promise is being returned instead of the expected result

Can anyone help me understand why I am receiving Promise { <state>: "pending" } when I call GetProfile("username")? Any suggestions on what steps I should take? Here is the function I am referring to: const GetProfile = async ( ...

Requesting information from a NodeJs endpoint

I have a NodeJs application that requires calling an end-point (http:\localhost:9900\get\employee - asp.net web-api) to retrieve data. What are some options available for achieving this task? Is it possible to utilize promises in this scenar ...

discord.js embed message not displaying text correctly in Upper and Lower case

I created a custom "test" command and I want it to be case-insensitive so that it works regardless of whether the user types -test, -Test, -TEST, etc. Currently, it only responds to lowercase -test commands. I tried changing the toLowerCase(); method to to ...

Getting inaccurate information via email from an HTML form using PHP

Check out this Live Form- I encountered an issue with the Contact Number section in the form. Despite adding JavaScript validation to ensure numerical values are entered, I am receiving incorrect values in the email body for the Contact Number field. < ...

Retrieving data from Vuex using the slim gem in a Ruby on Rails environment

Is there a way to transfer data when receiving an array through Slim? regions-list :region=@regions regions-list is my Vue component :region represents an array with items @regions stores the items from the backend I am new to vuex and believe I need s ...

Error encountered upon opening an Excel file created using the table2excel jQuery plugin

I am trying to export an HTML table to Excel using the table2excel plugin, but I'm encountering an error in the generated Excel file. How can I resolve this issue? I have set up a JSFiddle demo. To use the plugin, you simply need to follow these ste ...

Stripping CSS from my HTML using TinyMCE

I have created a custom Javascript function that retrieves the content of an HTML file from my server and then integrates it into a TinyMCE editor. Here is the function: function LoadTemplate(url) { $.post(url, function (data) { // Access the ...

My browser freezes when trying to load images with a JavaScript function

Currently, I am in the process of creating a basic game using Javascript and HTML. To load sprites for this game, I need an image function that can wait until the image is fully loaded before proceeding to the next one. However, the code I have written see ...

AngularJS to validate image dimensions (width and height) on upload

Hello, I am new to working with Angular JS. I have been experimenting with a login form that includes a file upload field for the user picture. The input field code looks like this: <input type="file" file-model="userPic"/> When I submit the form i ...

Exploring the combination of React event management with vanilla JavaScript operations

I am facing a scenario where I need to integrate React controls into code that I do not have full control over. Specifically, I am working with a Kendo UI grid that is created using jQuery. My goal is to render custom React components within the cells of t ...

MongoDB has incorrect date and time records

I've been working on a blog site where entries are logged with the time and date they were posted and stored in MongoDB. Everything was working fine on my local machine, but when I deployed the site to Heroku, I noticed that the date displayed is 8 ho ...

Utilizing markers for gaming in a data table with Vue and Vuetify

I am struggling to retrieve the games of teams from two objects with arrays in Vue. One object contains headers for a data table, while the other object contains status information. Despite my efforts, I have not been successful in fetching the game detail ...

The select2 plugin seems to be having trouble recognizing the Li click functionality

I have a select menu that is using the select2 plugin. I am trying to add a class to the <select> element when a menu option is clicked, but it doesn't seem to be working as expected even after testing with an alert. https://jsfiddle.net/ysm4qh ...

Encountering an error while attempting to launch an AngularJS application on Node.js? Let's

Currently, I am in the process of learning Angular. Whenever I attempt to run my code, an error message pops up: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7a737a7c6b6d70715f2b312f3115">[email protected]< ...