show the content depending on the result given by a connected class to a function

Currently, I am executing a loop where each item contains a button with a specific class that is linked to a method. The text displayed on these buttons depends on the value returned by the mentioned method.

HTML Template

<button v-for="(item, index) in items" 
       :key="index" 
       :class="isComplete(item.status)"
> {{ text_to_render_based_on_isComplete_result }} 
</button>

Method

methods: {
    isComplete(status) {
        let className
        // The logic to determine the className value is more complex but simplified here for this instance
        className = logic ? "btn-complete" : "btn-progress"
        return className
    }
}

The goal is to show "DONE" as the button text if the binded class is "btn-completed", and display "ON-GOING" if it is "btn-in-progress"

Initially, I tried accessing the button in each iteration using event.target, which resulted in undefined.

Another approach suggests creating a new method that identifies all generated buttons, reads their class, and modifies the textContent accordingly.

newMethod() {
    const completed = document.getElementsByClassName('btn-done')
    const progress= document.getElementsByClassName('btn-progress')

    Array.from(completed).forEach( item => {
        item.textContent = "DONE"
    })

    Array.from(progress).forEach( item => {
        item.textContent = "PROGRESS"
    })
}

However, there might be synchronization issues between this new method and isComplete()

Answer №1

My solution involved returning an array from the isComplete function and then accessing the values using their respective indices.

<template>
    <button v-for="(item, index) in items" 
       :key="index" 
       :class="isComplete(item.status)[0]"
       :v-html="isComplete(item.status)[1]"
    >
    </button>
</template>
<script>
export default {
    methods: {
        isComplete(status) {
            let className, buttonText
            // Additional code logic here to determine the actual value of className – simplified for this explanation
            if (className == condition) {
                className = "btn-complete"
                buttonText = "DONE"
            }
            else if (className != condition) {
                className = "btn-progress"
                buttonText = "ON-GOING"
            }

            return [ className, buttonText ]
        }
    }
}
</script>

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

Altering child components' properties from the parent component in Vue.js

Presenting the Tabs.vue component featuring four tabs: <template> . . <v-tab href="#tab-1" @click="showFirstTabFunc"> First Tab <v-icon>check_box_outline_blank</v-icon> </v-tab> <v-tab hr ...

The error message thrown by React JS webpack is "Module not found: Error: Unable to resolve 'src/content/Login/LoginAuthentication'"

Currently working with web "webpack" version "^5.74.0" for my React js project. During the npm start command, webpack is throwing the following error: ERROR in ./src/layouts/SidebarLayout/Sidebar/SidebarMenu/items.ts 21:0-83 Module not ...

Passing a return value to ajax success within an Express application

I am trying to retrieve a value from an included file and use it in the success function of an ajax call within Express. Below is my code for app.js: var userdetail = { "useremail":req.body.useremail, "fname" : req.body.fname, "lname" : req.bo ...

Tips for creating a simulated asynchronous queue with blocking functionality in JavaScript or TypeScript

How about this for a paradox: I'm looking to develop an asynchronous blocking queue in JavaScript/TypeScript (or any other language if Typescript is not feasible). Essentially, I want to create something similar to Java's BlockingQueue, but inste ...

How about, "Enhance your website navigation with a sleek anchor

After many attempts to implement smooth scrolling on my Bootstrap project, I have tried numerous Youtube tutorials and Google search results without any success. The latest attempt I made was following this Stack Overflow post Smooth scrolling when clickin ...

how to use an object as a key in the groupBy function with underscore.js

My JSON structure is as follows: I am attempting to group by NodeGroup using the underscore library. vm.populatedNodeGroups = _($scope.nodes).groupBy(function (o) { return o.NodeGroup.Name; }); Within vm.populatedNodeGroups, ...

Troubleshooting the issues with testing AngularJS using Jasmine and Karma, particularly when $httpBackend is

I'm currently in the process of writing unit tests for my Angular app using Jasmine with Karma. One of the functionalities of the app is to make a call to the GitHub API, retrieve a user's repository names, and store them in an array. Although I& ...

Tips for transferring information from a promise into an array

I'm trying to populate an array (coursesArray) with data from a promise and then use these values elsewhere. I am using the node-fetch library to make API queries. The issue I'm facing is that when I log the array inside the promise, it contains ...

Assistance needed for iterating through JSON data

I need assistance retrieving specific data from a JSON response in my JavaScript code. Unfortunately, the current approach is not providing the desired results: This is my JavaScript code snippet: function retrieveArtists(response){ var artistList ...

Navigating through a Vue app and hovering over a particular element within a v-for loop

<v-toolbar-items> <v-btn color="primary" active-class="default-class" flat v-for="item in horizontalNavItems" :key="item.title" :to="item.link">{{item.title}}</v-btn> </v-toolbar-items> export default { ...

a problem with mini-css-extract-plugin arises only in certain Vue projects

Encountering conflicting order warnings from mini-css-extract-plugin during the build process on two Vue projects. Here's an example of one such warning: Conflicting order. Following module has been added: * css ./node_modules/css-loader/dist/cjs.js? ...

Error: JSONP Label Validation Failed

My JSON URL is: The above URL returns the following JSON: { token: "2cd3e37b-5d61-4070-96d5-3dfce0d0acd9%a00a5e34-b017-4899-8171-299781c48c72" } Edit: Changed it to {"token": "2cd3e37b-5d61-4070-96d5-3dfce0d0acd9%a00a5e34-b017-4899-8171-299781c48c72"} ...

What is the best way to supply JSON data to the "The Wall" MooTools plugin while feeding?

I came across this amazing plugin called "The Wall" but unfortunately, neither the documentation nor the examples demonstrate how to utilize JSON objects with it. Imagine we have a JSON array like: [ { href : "/my/photo/image1.jpg", title : "Me an ...

Adjusting the width of innerHtml within a React router link to match the parent element's width

My current challenge involves a table where a cell is represented as a link. Within this setup, I am incorporating html content into the text of the link: <TableCell align="left" classes={{root: classes.cellPadding}}> <Link className={classes.l ...

Texture on plane is not displaying properly in Three.JS

Seeking assistance! I've been struggling all day to successfully load a texture onto my plane in Three.JS. Every time I attempt to add the desired link, it either fails or *poof* my plane disappears mysteriously. Please lend me your expertise with Thr ...

Using three.js to create a hover effect that highlights the edges of a cube with LineSegmentsGeometry

I am currently utilizing LineSegmentsGeometry and LineMaterial to render thick cube edges. My objective is to dynamically change the color of the edge when it is hovered over. const edgesGeometry = new LineSegmentsGeometry().fromEdgesGeometry( new THREE. ...

Tips on incorporating live data into charts using MVC?

Recently I started working with ASP.NET MVC on my own project and have encountered some difficulties. One particular challenge I am facing is how to create a line chart using data from a database. Let me explain the issue in more detail: Within my databa ...

What might be causing the in-viewport javascript to not work in my code?

Why is my in-viewport JavaScript code not functioning properly? Link to JSFiddle code When the Click to move button is clicked, the cat image will slide correctly. However, when implementing the following code: if($("#testtest").is(":in-viewport")) ...

Tips for concealing a div element once the final section is reached

My website has a sticky footer with a clickable arrow that allows users to navigate through the sections. However, I am facing an issue where the arrow does not disappear once the last section is reached. Being new to jQuery and JS, I'm unsure how to ...

Error: The fetch API encountered an unexpected termination before completing the request

A new issue has come up in my project, and although I found a similar problem on this link, it does not address my specific issue. What I have set up is a straightforward API using nodejs, express-framework, and mongoose. The issue lies with the fetch API ...