Using VueJS to dynamically add a CSS class if a certain filter is

Can you apply this filter rule in VueJS?

If an element has the filter discount, the parent will be styled with the class color-green (in this case, the span tag)

Check out my JSFIDDLE for more details :)

HTML

<div id="app">
  <h3>50% Discount =)</h3>
  <ul>
    <li v-for="product in products">
      {{ product.name }} - <span> {{ product.price | discount }} </span>
    </li>
  </ul>
</div>

JS

Vue.filter('discount', function (value) {
  return  value * 0.5;
})

new Vue({
    el: '#app',
  data: {
    products: [
      {name: 'Angular', price: 75},
      {name: 'VueJS', price: 60},
      {name: 'React', price: 40}
    ]
  }
})

CSS

.color-green{
  color: green;
}

Answer №1

Do you have a discount on every element in your case?

Checking the filter is not necessary. The filter simply transforms input into output.

It would be best to define the discount information within your product data. Then, you can implement something like this:

Vue.filter('discount', function (value) {
  return  value * 0.5;
})

new Vue({
el: '#app',
  data: {
  products: [
      {name: 'Angular', price: 75, discount: 0.3},
      {name: 'VueJS', price: 60},
      {name: 'React', price: 40, discount: 0.7}
    ]
  }
})
.color-green{
  color: green;
}

.product-price--discounted {
  text-decoration: line-through;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e5e6f6d3a1bda6bda2a5">[email protected]</a>/dist/vue.js"></script>
<div id="app">
  <h3>Products</h3>
  <ul>
    <li v-for="product in products">
      {{ product.name }} - 
      <span :class="product.discount && 'product-price--discounted'">{{ product.price }}</span>
      <span v-if="product.discount" class="color-green">
        {{ product.price - product.price * product.discount}}
      </span>
    </li>
  </ul>
  
  <h3>All elements have discount:</h3>
  <ul>
    <li v-for="product in products">
      {{ product.name }} - <span class='color-green'>{{ product.price | discount }}</span>
    </li>
  </ul>
</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

Tips for aligning a button to the bottom of a card in Vuetify, regardless of the text length

I'm having trouble aligning buttons in my cards. I have a layout with 3 cards in a row, but the button's position changes when text is added to a specific card. I've tried passing different props and using various classes, but nothing seems ...

Storing data values from a specific object key into an array in Vue: Step-by-step guide

Just dipping my toes into the world of Vue framework here. I managed to create a selectable table that stores data in an object. I want this function to run in the background, so I figured it should be in the computed section. The object structure is as fo ...

Problem with spawning Python when using VUE2, Electron, and Flask

After completing my first project using Vue, Electron, and Flask, I am encountering difficulties packaging it. While everything works perfectly with "npm run electron:serve," there seems to be an issue when running "npm run electron:build" as Flask is not ...

Creating a Vue.js component that integrates a Bl.ocks.org graph

I'm a rookie in the world of D3 and I am trying to implement this cool d3 element into my Vue.js component. However, I've encountered an issue with the periodic rotation that I require not functioning properly. It seems to work initially but then ...

transferring values/properties to child component within a customized component

I am curious about the most efficient method for passing numerous props to a component that is enclosed within an external component. For instance: code <template> <div> <v-text-field :error="summonerNameError" ...

Encountering a bug in my JSON or object tree viewer in Vue.js 3 where duplicate keys are present when there are multiple similar values

Encountering an issue with the following code: Attempting to create a tree viewer from an object, it works well until encountering identical values which cause key duplication and replacement. View on CodePen: https://codepen.io/onigetoc/pen/rNPeQag?edito ...

Heroku Woes: Vue-CLI Deployment Woes

I have been facing persistent challenges while trying to deploy my Node.js application with a Vue.js frontend on Heroku. Despite numerous attempts and different configurations, my builds consistently fail due to module resolution errors. Application Infor ...

Methods to retrieve an array's value list dynamically using the 'id' in Vuejs

**User.vue** <template> <div> <div v-for="list in lists" :key="list.id">{{ list.val }} {{ list.kk }}</div> </div> </template> <script> import { datalist } from "./datalist"; export default { name: "User ...

Harnessing the power of two-way data binding in VueJS

I am looking to utilize Vue's two-way data binding to dynamically update the values of amount and total. The price of a given product is fixed. When users modify the amount, the total = amount * total will be automatically calculated. Similarly, users ...

Vue.js App encounters Recaptcha contact form 7 with a status of "spam"

I recently developed a Vue.js application that operates on a subdomain within the same main domain as my WordPress site. To submit a form to the contact form 7 rest API, I utilized the axios package. However, every time I submit the form, I receive a respo ...

What is the best way to use a computed property as a style value for a component in Vue.js?

My component's template includes the following HTML element: .grid-item(:style="{ width: columnWidth, backgroundColor: 'blue' }") I want to dynamically set the width of this element using a computed property: computed: { columnWidth () ...

Trigger event when v-show is toggled

Currently, I am working with Laravel and Vuejs. I am trying to trigger an event when a component is shown or hidden. Can anyone provide guidance on how to accomplish this? <album-images v-show ="!gallery" :album = "album" :image-arr = "imageArr">& ...

Troubleshooting Problem with QRCode Scanner in the Ionic Vue Framework with Capacitor

While working on my Vue.js project with Ionic and Capacitor, I encountered a challenge in reading QR Codes from the camera. Despite searching extensively, I couldn't find any specific solutions for Ionic Vue.js. However, I stumbled upon a small packag ...

Having trouble setting a cookie. Sharing the complete code on codeSandBox for both backend and frontend. Can anyone spot the issue? Appreciate the help! Using Express with Vue 3

Presenting a sandbox of a backend, featuring a simple setup with one route and one controller. Curious to explore the full code for the backend? Check it out here. As for the frontend, it's equally straightforward. I began by creating a new Vue 3 pr ...

In the Vue framework, the navbar is capable of selecting two drawers simultaneously in the event that one path is

I am working on a Vue.js application that includes a sidebar. Within the v-list-item component, in the path :to="link.route", I'm utilizing route names like: { name: "dashboard" }. An issue arises when one link's path is con ...

Creating a column that adjusts dynamically in PDFMAKE

Currently, I am utilizing PdfMake within my VueJS app to generate PDFs. I am curious as to whether it is possible for me to have control over the columns displayed in my template by using the data that is being printed as a variable. I am aiming to achiev ...

Tips for dynamically updating the value of a variable in Vue by importing a JavaScript file

There is a file for an app that imports ymaps.js where YmapsComponent.vue is declared. import '../js/ymaps.js'; import { createApp } from 'vue'; const app = createApp({}); import YmapsComponent from './components/YmapsComponent.vue ...

Having trouble with integrating jsPlumb with Nuxtjs and encountering the error "document is not defined"?

I've seen some similar questions, but none of the solutions have worked for me so here's my issue. I'm attempting to integrate jsPlumb into my Nuxt.js/Vue.js application. Here are the steps I've taken: npm i jsplumb --save Create a ne ...

What determines which code Nuxt3 will run on the server-side versus the client-side?

Looking to dive into the world of Nuxt3. As far as I know, it employs universal rendering, blending elements of CSR and SSR. But before embarking on this journey, a couple of queries are on my mind. How does Nuxt3 differentiate between client-side and s ...

The processing time for this request is unreasonably long

My website is built using Laravel and Vue.js. I am facing an issue where a function takes more than 2 minutes to execute, resulting in the following error: 500 Internal Server Error Request Timeout This request takes too long to process and is timed out b ...