How to dynamically assign width to elements in a v-for loop in Vue.JS

I am working with HTML code that looks like this:

                <div v-for="(s, k) in statistics" v-bind:key="s.id" class="single-stat">
                    <div class="stats">
                        {% verbatim %}
                        <div>{{ s.proc1 }}</div>
                        <div class="statid">{{ s.name }}</div>
                        <div>{{ s.proc2 }}</div>
                        {% endverbatim %}
                    </div>
                    <div class="belt1">
                        <div class="belt2" :style="Style(k)"></div>
                    </div>
                </div>

In my Vue object stored in data, I have the following information:

statistics: [
        {name: 'Possession', proc1: 60, proc2: 40, id: 1},
        {name: 'Shoots', proc1: 65, proc2: 4, id: 2},
        {name: 'Passes', proc1: 64, proc2: 40, id: 3},
        {name: 'Fauls', proc1: 44, proc2: 4, id: 4}
    ],

In my methods section, I have defined the following function:

Style: function(k){
        switch(k)
        {
            case 1:
                return { width: statistics[0].proc1 }
            case 2:
                return { width: statistics[1].proc1 }
            case 3:
                return { width: statistics[2].proc1 }
            case 4:
                return { width: statistics[3].proc1 }
        }
    }

However, I am facing an issue with the code. How can I properly set the percentage of width for each belt using the Style method?

Answer №1

You missed adding px after

{ width: statistics[0].proc1 } -> { width: `${statistics[0].proc1}px` }

new Vue({
  el: '#app',
  data: {
    statistics: [
        { name: 'Possession', proc1: 60, proc2: 40, id: 1 },
        { name: 'Shoots', proc1: 65, proc2: 4, id: 2 },
        { name: 'Passes', proc1: 64, proc2: 40, id: 3 },
        { name: 'Fauls', proc1: 44, proc2: 4, id: 4 }
    ],
  },
  methods: {
    style(i) {
      return { width: `${this.statistics[i].proc1}px` }
    }
  }
})
span { display: block; background: #f5f5f5; padding: .5rem; border: 1px solid }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <span
    v-for="(statistic, i) in statistics"
    :key="i" 
    :style="style(i)"
  >
    {{ statistic.name }} width: {{ statistic.proc1 }}px
  </span>
</div>

Answer №2

My recommendation would be to utilize computed properties in this scenario.

new Vue({
  el: '#app',
  data: {
    statistics: [
        { name: 'Possession', proc1: 60, proc2: 40, id: 1 },
        { name: 'Shoots', proc1: 65, proc2: 4, id: 2 },
        { name: 'Passes', proc1: 64, proc2: 40, id: 3 },
        { name: 'Fauls', proc1: 44, proc2: 4, id: 4 }
    ],
  },
  computed: {
    styling() {
      return i => { return { width: `${this.statistics[i].proc1}px` };}
    }
  }
})

Moreover,

 <div v-for="(s, k) in statistics" v-bind:key="s.id" class="single-stat">
                    <div class="stats">
                        {% verbatim %}
                        <div>{{ s.proc1 }}</div>
                        <div class="statid">{{ s.name }}</div>
                        <div>{{ s.proc2 }}</div>
                        {% endverbatim %}
                    </div>
                    <div class="belt1">
                        <div class="belt2" :style="styling(k)"></div>
                    </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

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

Transforming the hue of a radio-button

When it comes to the default CSS code for radio buttons, they appear gray when unselected and blue when selected: https://i.stack.imgur.com/hsazr.png However, I have a specific requirement for them to be black in both states. In order to achieve this, I ...

Ensure that the floated element stretches to 100% height in order to completely fill the page

I'm working on achieving a height of 100% for the left sidebar so that it fills the page regardless of the size of the "main" div. Currently, it stops at the normal page height and doesn't expand further. Is there any way to make this work as i ...

The 3D Circle Flip feature on a certain webpage designed by Nordstrom is experiencing issues when viewed on Firefox

Click here to see a 3D Circle Flip effect. It works correctly in Chrome, but in Firefox the text does not change when flipping. ...

Eliminate any additional spacing or padding surrounding the text input field

Is there a way to adjust the inner padding of a text field? Currently, when entering text in an HTML text field, there is padding at the beginning and end. The numbers I am inputting have no more than 3 digits, and the current padding takes up about the wi ...

The conditional comment designed for browsers other than Internet Explorer may not work properly on outdated versions of

I find myself in a predicament where I must dynamically load one of two stylesheets depending on the browser accessing the page: If any browser other than IE, then load the "new" stylesheet If IE is version 9 or higher, then load the "new" stylesheet ...

What is the proper way to implement v-model with Vuex in <select> elements?

I included a <select> element in my design: <select v-model="amount" required> <option value="10">10</option> <option value="20">20</option> <option value="25">25</o ...

Element UI is a powerful user interface library designed specifically for Vue

Is there a way to programmatically trigger or hide the dropdown when using the el-dropdown component from , especially when split-button is set to true? In my previous experience with using React and bootstrap, I could accomplish this by utilizing ref or ...

What is the best way to incorporate a third-party element into Vue using a script tag?

I am in the process of developing a website and I would like to include a widget that links to a podcast on BuzzSprout. Initially, I created the site using HTML to test out different designs, but now I am looking to transition it to VueJS. In my HTML vers ...

Modifying the order of Vuetify CSS in webpack build process

While developing a web app using Vue (3.1.3) and Vuetify (1.3.8), everything appeared to be working fine initially. However, when I proceeded with the production build, I noticed that Vue was somehow changing the order of CSS. The issue specifically revol ...

Expanding size on hover without affecting the alignment of surrounding elements

Imagine there are 10 divs on the page styled as squares, collectively known as the home page. Among these 10: 3 divs belong to the .event1 class, 5 divs belong to the .event2 class, and 2 divs belong to the .event3 class. <div class="boxes event1"> ...

Stylish Tabbed Design

Imagine I have this html and css snippet h1{ color: rgb(61, 133, 200); font-size: 3em; margin-left: 0px !important; } h1 ~ *:not(h1) { margin-left: 0px; } h2{ font-size: 1.8em; margin-left: 40px !important; } h2 ~ *:not(h2) { ...

Creating an animated full-width SVG with a background image

This has been quite a challenge for me. I have successfully implemented the main functionality that I wanted in this codepen. The SVG is divided into 3 sections, with the middle section sliding away from the others when you click the "Find Out More" button ...

Having trouble generating a Vue project using vue/cli?

I am currently developing a vuex application using Vue CLI. Unfortunately, the CLI gets stuck during metadata fetching (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb88838480828f8a99abd9c5dac5dd">[email protected]</a& ...

What is the best way to specifically target and style a component with CSS in a React application?

I'm facing a small issue with the React modals from Bootstrap in my application. In index.html, I include the following: <link rel="stylesheet" href="/assets/css/bootstrap.min.css"> <link rel="stylesheet" href=& ...

Utilize a jQuery selector to target the initial element of every alphabet character

I'm seeking some assistance with jQuery selectors and have a specific scenario that I need help with. Here is the HTML structure I am working with: <ul> <li class="ln-a"></li> <li class="ln-b"></li> <li cl ...

What is the solution to removing the bottom margin from the jumbotron in the bootstrap framework?

I am building a website for my high school robotics team and I am new to HTML and CSS (bootstrap). I want to get rid of the gap between my banner and navbar on my site. Is there a way to remove that space? Check out our website: www.robotichive3774.com & ...

Arrangement: Div beside vertically-aligned hyperlinks

Example code: p { color: red; } span { font-weight: bold; } <p>This is a paragraph.</p> <p>Another paragraph here.</p> <span>Bold text</span> This is the desired outcome: https://example.com/image.png It&ap ...

The disappearance of IE7 floats is observed when their parent element is styled with position:relative

The issue I'm encountering in IE7 is related to the CSS properties position:relative;, float: right;, and float: left;. While this problem doesn't seem to occur in newer browsers, it's puzzling why position:relative; impacts the behavior of ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...