How can I transform the overall value into a percentage in Vue.js or JavaScript?

Is there a way to create a progress bar in VueJS 3 with Nuxt Js by converting the total value to a percentage and displaying it as a style width value? For example, if 40% out of 100% equals 400USD out of 1000USD, can we achieve this using a function in an array object?

I am using VueJS 3 with Nuxt Js

Below is my VueJs code in < Progressbar /> component

<template>
   <div>
     <div class="progress mt-0 h-1 bg-gray-200 rounded-full w-full overflow-hidden transition-all">
       <div class="bg-blue-600 h-1 progress-bar progress-bar-animated" role="progressbar" style="width: 88%;" :aria-valuenow="deal.collected" aria-valuemin="0" :aria-valuemax="deal.goal"></div>
     </div>
   </div>
</template>

Style

.progress-bar{
    background-image: linear-gradient(45deg, rgba(255,255,255,0.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,0.15) 50%, rgba(255,255,255,0.15) 75%, transparent 75%, transparent);
    background-size: 6px 6px;
}
.progress-bar-animated{
    animation: 1s linear infinite progress-bar-stripes;
}
@keyframes progress-bar-stripes {
    0% {
        background-position-x: 0.5rem;
    }
}
export default {
    data(){
        return{
            deals:[
                // deals
                { 
                    icon: require("../../assets/img/ico/ico-1.jpeg"),
                    name: "Agoric",
                    type: "Blockchain Service",
                    collected: 883673,
                    goal: 1000000,
                    status: "11h 3m" + " left",
                    link: "/"
                }
            ]
        }
    },
}

Answer №1

To find the percentage, simply use the formula deal.collected * 100 / deal.goal

new Vue({
    el: '#app',
    computed: {
      percentage() {
        return (deal) => deal.collected * 100 / deal.goal
      }
    },
    data(){
        return{
            deals:[
                // deals
                { 
                    icon: ''/*require("../../assets/img/ico/ico-1.jpeg")*/,
                    name: "Agoric",
                    type: "Blockchain Service",
                    collected: 883673,
                    goal: 1000000,
                    status: "11h 3m" + " left",
                    link: "/"
                }
            ]
        }
    },
})
.progress-bar{
    background-image: linear-gradient(45deg, rgba(255,255,255,0.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,0.15) 50%, rgba(255,255,255,0.15) 75%, transparent 75%, transparent);
    background-size: 6px 6px;
}
.progress-bar-animated{
    animation: 1s linear infinite progress-bar-stripes;
}
@keyframes progress-bar-stripes {
    0% {
        background-position-x: 0.5rem;
    }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
   <div v-for="deal of deals">
     <div class="progress mt-0 h-1 bg-gray-200 rounded-full w-full overflow-hidden transition-all">
       <div class="bg-blue-600 h-1 progress-bar progress-bar-animated" role="progressbar" :style="{ 'width': percentage(deal) + '%' }" :aria-valuenow="deal.collected" aria-valuemin="0" :aria-valuemax="deal.goal"></div>
     </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

Application experiencing server error when updating MongoDB data

I have encountered an issue while trying to update/modify data that has already been uploaded in a reactjs project using mongoDB as the database. Whenever I attempt to update the data, an error is displayed. How can this problem be resolved? https://i.sta ...

The complete guide to effectively concealing double arrows within a Bootstrap 4 custom-select component

If you're looking to hide the double arrow on the right of the bootstrap 4 custom-select field, there have been solutions provided here. However, even after implementing those changes, the text on the right-hand side still gets obscured by the opaque ...

Currently, I am utilizing Angular 2 to extract the name of a restaurant from a drop-down menu as soon as I input at least two characters

I am currently utilizing Angular 2 and I am trying to retrieve the names of all restaurants from a dropdown menu. Currently, when I click on the text field, it displays all the results, but I would like it to only show results after I have entered at least ...

Unlock the full potential of custom authorization providers when integrating them with Supabse

Supabase user here looking to implement authorization with a third-party service that isn't on the supported list. Any suggestions on how to go about this? ...

What method is most effective for duplicating objects in Angular 2?

Is it just me, or does Angular 1.x have methods on the global angular object like angular.copy and angular.shallowCopy that are missing in Angular 2? It seems like there is no equivalent version in Angular 2 documentation. If Angular 2 doesn't plan on ...

Guide on making jQuery color variables?

Is there a way to achieve CSS variable-like functionality with jQuery? For example, creating reusable CSS attributes in jQuery instead of using SASS variables. Imagine if I could define a variable for the color black like this: I want to make a variable t ...

Run the jQuery script once it has been successfully loaded using jQuery's .load method

On the Index.html page, there is a select box labeled #choose_content_to_load and a div named #get_loaded_content <select id="choose_content_to_load"> <option>Some content from the page content.html and div #content</option> </select ...

Experiencing difficulties with Magento operations

Currently facing an issue while attempting to set up Magento on my server. I have transferred all files from another server to mine, but now encountering an error. Could this be related to the symlinks I generated or is it caused by something else? Encoun ...

Using VueMultiselect with Vue 3: A guide for beginners

I'm currently experimenting with the vue multiselect component, but when I include it in the template, I am encountering a series of warnings and errors. <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

What is the functionality of Mongoose for handling multiple updates?

Array; arr=[ { id: [ '5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af' ], color: [ 'a', 'b' ] } ] Models; const varyant = Models.varyant function; Promise.all( arr.map((item)=>{ return var ...

Retrieving and storing selected checkbox values using both JavaScript and PHP

I have location names and location IDs stored in a database table. I am using a foreach loop to print these values as checkboxes in PHP. When the user clicks on the submit button, it triggers a JavaScript function. I would like to store all the selected ...

The combination of jQuery event handling and accessing undeclared event objects

While debugging someone else's code, I encountered a puzzling situation. The snippet of code provided below illustrates the problem. It seems to me that event should be undefined upon entering the event handler. This is indeed the case in Firefox, bu ...

Save the output of a function in node.js

I have created a function that utilizes the nodejs module recursive-readdir to read all files within a folder recursively. The function is functioning correctly, but I am facing an issue with exporting the 'routes' array using 'module.export ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

The webpage's Document Object Model fails to refresh following a JavaScript modification

I have encountered an issue with a sample webpage where I am attempting to set the wmode of all YouTube elements on the page to "transparent." When inspecting the page using Chrome, I can see that my JavaScript code is functioning properly. However, the b ...

An unexpected { token error was encountered by Jest while running a React application

I am facing a persistent issue that I just can't seem to resolve. Despite trying numerous solutions found online, nothing seems to fix it. Below are the configurations: package.json { "scripts": { "test": "jest --no-cache", ...

The Authorization header in POST and PATCH fetch requests is stripped by Typescript

I have developed an API in C# that utilizes JWT tokens for authorization. On the frontend, I store these tokens in local storage and retrieve them when making a request. GET or DELETE requests work seamlessly, as I can verify through console.log() that t ...

Scrolling text blocks on mobile devices

When viewing the website on a desktop, everything works perfectly. However, when accessing it on a mobile device and trying to scroll down, only the text moves while the page remains stationary. The website utilizes skrollr core for animations. I have alre ...

Sharing $scope Data Between Controller and Directive in AngularJS

Recently, I created a straightforward directive that displays an object: var app = angular.module("myApp", []); app.controller('myCtrl', function($scope) { $scope.users=[ {name:'davidi',age:'23'}, {name:'karen ...

The scroll function is failing to activate at the desired location

I've been trying to fine-tune a window scroll function I created. Initially, I attempted to use waypoints for this, but unfortunately, I couldn't get it to work as expected. The main issue I'm facing is that the function triggers too early ...