Vue: Utilizing computed properties to monitor changes in offsetHeight of elements

I am working on a component that requires an array of 50 objects to be passed as a prop.

<template>
   <div v-for="(item,index) in items" ref="items" :key="index"gt;
      //
   </div>

</template>

props: {
    items: {
      type: Array,
      required: true,
    },
}

To check the offsetHeight of each item in the prop items, I created a computed property. Initially, I retrieved all the items using their ref and returned them as is.

computed: {
  allItems() {
    const all = this.$refs.items;
    return all;
  }
}

When inspected in VueTools, the computed property correctly displays an array of HTMLDivElement's. However, when I attempted to filter out items based on their offsetHeight:

computed: {
  allItems() {
    const all = this.$refs.items;
    return all.filter(x => {
       x.offsetHeight > 300;
    })
  }
}

The computed property now shows only "15" instead of the expected values. Even after changing the window height, the displayed offsetHeight values do not update accordingly. Shouldn't the computed property continuously reflect the current values?

Answer №1

Is it possible for this to constantly display the current values?

No, as HTMLElement.offsetHeight is an inherent property that Vue cannot monitor. The height of an element can change at any time without Vue being aware. Other methods like using ResizeObserver or listening for the window resize event should be utilized to react to changes in an element's size.

In Vue 2, the reactivity system functions exclusively with plain objects. Visit Reactivity in Depth for further details.

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

Sending a variable from the server to the client using express rendering in Node.js

When using a Node.js express server, I am attempting to pass a variable to the index.ejs file on the client side. In my server code, I have added: res.render("dashboard/index", { hidePayment: true }); The problem arises when trying to access the hidePayme ...

"Utilizing FileReader to seamlessly integrate data into FormData without the risk

Currently, I'm in the process of constructing an ajax file uploader. This is made possible thanks to the recently introduced FormData interface. Everything seems to work as expected when using the original file. However, I encounter issues when conver ...

Looking to prevent printing and saving a webpage directly from the file menu using JQUERY or JavaScript

I am in need of assistance to find a code that can disable the ability to print and save a webpage using JQUERY or JAVASCRIPT ...

Achieving both positive and negative styling in AngularJS with ng-class: A guide

I'm currently working on an application that requires indicating red for negative values and blue for positive values in a calculation. <td class="amount debit"> <input type="text" class="form-control" ng-model="vm.model.form.amount_debi ...

Sending an Ajax request to a nearby address

I'm feeling a bit lost on how to achieve this task. I've been searching online, but it seems like my search terms may not have been quite right. My goal is to set up a RESTful api. $.ajax({ type: "POST", url: "https//my.url/", data: ...

Tips for accessing the selected button in notification.confirm() within a PhoneGap app

Recently, I implemented the Phonegap notification.confirm in this way: navigator.notification.confirm( 'Do you wish to proceed?', function() { console.log("Function Called"); }, 'Game Over', 'Continu ...

Divide the string by spaces

One of the challenges I am facing involves a textarea where users can input messages. The goal is to split the message into an array of words after detecting an '@' symbol, and then search for specific words in that array such as @person1 and @pe ...

Guide to generating an array in JSON format within a dropdown menu option

Struggling to create a JSON array with select options instead of text fields? You're not alone. Spend hours trying to figure it out but still no luck? Take a look at this code snippet: function createJSON() { result = []; $("select[class=emai ...

Using TypeScript with Vue: Safely accessing component properties in a type-safe manner

I am currently exploring Vue's setup API and there is one aspect that I am struggling with: I need to retrieve properties of a child component from a parent component. Everything seems to be functioning correctly, but I am facing difficulties with the ...

How about I visit the campgrounds/edit page for a change?

In this get route, the previous link it was redirected from is stored in the req.session object under returnTo. Once redirected, it goes to the /login and we are able to view the object in the console. router.get('/login', (req, res) => { ...

Leveraging callback functions for dynamically updating a JSON structure

Recently, I've been working on a db_functions.js file that includes several functions designed to interact with a database. Here's an excerpt from the script: function getUsers(client, fn){ //var directors = {}; client.keys('*' ...

Sorting through an array using a different array of values

Looking to filter one array with another, where values in the first array should match 'id' in the second array for filtering. The arrays in question are: const array1 = [a, b, c, d] The array to be filtered based on matching 'id' va ...

Creating an Active Link in Bootstrap 5.0 (Beta 3) Navbar Using JavaScript

I am currently working with Bootstrap 5 (Beta 3 - no JQuery) on a static website, and I am facing the challenge of making a navbar link appear active. I prefer to achieve this using JavaScript instead of creating a separate navbar for each page. For instan ...

Nested ng-repeat using td to display multiple items in AngularJS

I am having an issue with the code as it is producing a table with the elements all in a single column. Below is an example of the data: var data = [[{"id":"1","value":"One"},{"id":"2","value":"Two"},{"id":"3","value":"three"}],[{"id":"4","value":"four"} ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

The requested resource at http://127.0.0.1:8000/storage/profiles/ could not be located (Error 404: Not

I have successfully implemented a feature on my website that allows users to upload their avatar picture using Vue.js and Laravel as the backend. The image is stored in the storage directory, and I can display it on the user's profile page using Vue.j ...

Executing the command "node <app name>" does not result in any action

I am experiencing an issue with my bot app called "vgen.js". When I run the command "node vgen", the prompt/directory disappears and the cursor just blinks on the far left of the screen. I have been following instructions from a tutorial found here: https ...

Can Nuxt accurately identify which page is accessing the /slug path?

In my project, I have a post page, a category page, and the usual internal pages all set up. Here's the current structure in the pages folder: pages/category/_id.vue - Sends request for a category pages/post/_id.vue - Sends request for a post pages/p ...

Exploring an alternative perspective on successful login in angular.js

Today was the beginning of my project to convert a website to angular.js. The main page is served by index.html and includes ng-view for other views such as login and signup. Working closely with a backend developer, I have successfully integrated a rest c ...

Removing an item from a React (Hooks) array state: A step-by-step guide

In my code, I have a list of text inputs populated from an array and I am trying to delete a specific element based on its index. The issue I am facing is that even though the console log correctly shows the updated array without the removed element, visua ...