Adjusting the width of a <div> element dynamically using two array variables within a Vue.js loop

As a beginner in vuejs, I am facing a challenge in understanding its functionality.

Currently, I have implemented a 'vuejs for each-loop' within a div to fetch data from a JSON object. My goal is to calculate the current distance relative to a maximum distance and reflect this ratio as the width of a div.

Current HTML code:

<div id="visuals" class="col-lg-9 col-sm-12 wow fadeInUp pt-5 pt-lg-0">
    <div v-for="element in sortedClubs">
        <div class="progessbar-title">{{ element.Name }}</div>
        <div class="progress progressvisual">
            <div class="progress-bar progress-bar-striped progress-bar-animated bg-warning" role="progressbar" style="width: 50%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">
                {{ element.km }} km
            </div>
        </div>
    </div>
</div>

Current Vuejs code:

var app = new Vue({
        el: '#visuals',
        data: {elements: [
            {"Name":"a","km":"1361"},
            {"Name":"b","km":"6409"},
            {"Name":"c","km":"1067"}]},
        computed: {
            sortedClubs: function() {
                function compare(a, b) {
                    if (a.km_lopen > b.km_lopen)
                        return -1;
                    if (a.km_lopen < b.km_lopen)
                        return 1;
                    return 0;
                }

                return this.elements.sort(compare);
            }
        }
    });

As the list is sorted from high to low, I can determine the maximum value with sortedClubs[0].km

However, I am unsure about how to adjust the width of the progress-bar div based on the formula element.km/sortedClubs[0].km*100 to represent each div's percentage. Any insights on this would be appreciated!

Answer №1

If you want to make the style more dynamic, simply add a colon before it. Then use :style to pass the percentage instead of hardcoding 50%.

For example:

:style="`width: ${element.km/sortedClubs[0].km * 100}%;`"

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

Switch between various 3D models in three.js by utilizing dat.gui interface

I'm currently attempting to switch between multiple 3D models (loaded with OBJMTLLoader.js) that are rendered in my three.js scene. Using dat.gui, I've created a dropdown list of model names where selecting one will add the corresponding obj mode ...

It is not possible to utilize PopOver within the Header Menu Item

I am looking to create a header with a popover component. import React from "react"; import { Layout, Menu, Button } from "antd"; const { Header, Popover } = Layout; const { SubMenu } = Menu; const Index = (props) => { const content = ( & ...

Another option for handling a series of conditional statements instead of a bulky

Can someone help me with a coding issue I'm facing? I have an application that contains a large number of buttons which I need to trigger using keyboard presses. Currently, I am using a switch statement for this purpose. However, as the number of butt ...

Conceal a request URL within JavaScript through the use of Laravel/Ajax

I have an idea that I'm not sure is great or even feasible, but I really need to make it work. I am attempting to obfuscate a URL that needs to be used in a Javascript function as a parameter. This is what I currently have: <script> myFunction ...

Obtaining connection data in jsPlumb can be accomplished through a variety of

I have created a compact table of nodes that allow me to drag and drop connections or manually input node IDs to establish connections between them. Despite searching through the documentation and scouring the internet for examples, I am struggling to fin ...

Issue with retrieving POST body from Ajax call in Play Framework

I am currently attempting to send a POST request to my backend using JSON data. The frontend call appears like this: function register() { var user = $("#form_reg_username").val(); var pass = $("#form_reg_password").val(); var referal = $("#form_reg ...

Creating a custom HTML5 pattern for formatting Pakistani phone numbers

For my HTML5 pattern for Pakistan Phone Number Format, I have the following criteria: 03xx-xxxxxxx My current regex code is as follows: /03[0-9]{2}-[0-9]{7}/ Now, I want to ensure that the next 7 digits after the hyphen meet the following conditions: ...

Tips for structuring nested properties in VueJS?

Currently, I'm facing a complex architectural dilemma that's puzzling me. Picture this scenario: 3 components - A -> B -> C. Here, A acts as the grandparent, B as the parent, and C as the child. A passes an array of objects to B. B itera ...

Creating Custom Filters in Angular using Functions

Attempting to filter ng-repeat using a function instead of an actual filter has presented some challenges. The code snippet below demonstrates the attempt: <tr ng-repeat="(key, value) in dataObj| filter:dataFilter"> The intention is to define dataF ...

The JQuery Tooltip fails to function properly following an ajax load operation

I've inserted the following code into my website: $(document).ready(function() { $('.tooltip-game').jBox('Tooltip', { closeOnMouseleave: true, ajax: { url: 'tooltips/tooltip-game-html.jsp', reload: ...

Tips for creating an endless loop within a nested v-for in Vue?

Here is my attempt at solving the problem: HTML <ul class="tree"> <li> <span>First Node</span> <ul> <li v-for="(tree, i) in trees" :key="i"> <span v-text="tree. ...

If a div is hidden, disregard this particular CSS rule

In my current scenario, I am dealing with 3 distinct divs: - Menu - Header (#rt-top-surround) - Showcase (#rt-showcase) - Main Body content (#rt-mainbody-surround) The Menu is set as 'sticky' with position: fixed. As a result, I have to adjust ...

Surprisingly stumbled into the 'restricted' directory while switching the current directory to a temporary folder

I've encountered a strange issue while attempting to create and switch the working directory to a temporary folder using Node.js. Check out the code snippet below: var path = require('path') var fse = require('fs-extra') var TEST ...

jQuery fails to operate on products loaded through AJAX requests

Upon opening the page, jQuery functions correctly. However, when a product is loaded or changed via AJAX, jQuery stops working. I am currently using jquery-1.7.1.min.js $(document).ready(function () { $screensize = $(window).width(); if ($screensi ...

Looking to incorporate multiple accordion drop down menus on your website? Utilize a combination of HTML, CSS, and JavaScript to

I am experiencing a challenge with implementing multiple accordion menus on my website. Whenever I attempt to duplicate the code, the new accordion menu appears but clicking on the first bar simply scrolls me back to the top of the webpage. Below is the H ...

Unexpected Results from WordPress Ajax Request

Currently, I am utilizing the snippets plugin in conjunction with Elementor. To implement an ajax function as a snippet, I have set it up like so: add_action( 'wp_ajax_get_slug_from_id', 'get_slug_from_id' ); add_action( 'wp_ajax_n ...

Creating a Google Captcha with validation is a straightforward process that can enhance the

I am having issues with adding Google Captcha to my form. Despite all fields working properly, the Captcha integration seems to be causing some disruptions. I have included my code below. Thank you in advance for your help. Please also check the following ...

Unable to access property within JSON object sent via POST request

I encountered an issue TypeError: Cannot read property &#39;tasks&#39; of undefined While attempting a new POST request on my API, here is the request body I am using: { "name": "example1", "description": "teaching example1", "rules" ...

What external libraries does Angular 4 utilize during execution, aside from RxJS?

Angular 4 relies on RxJS types in its public API and also internally depends on RxJS. It would be beneficial to explore if Angular utilizes other external packages for certain functionalities, allowing us to incorporate them into our own projects. This ap ...

Laravel - AutoComplete Feature: Populate Input Field with JSON Data Instead of Suggestions

I've been working on adding search functionality to my Laravel 5.4 project, but I'm running into an issue. The suggestions show up fine in the dropdown menu, but when I select one, the input field gets filled with JSON data instead of the suggest ...