Is it possible to utilize a computed property for dynamically styling a table row based on certain conditions?

I have a table of users that I am currently rendering, and my goal is to highlight the entire row only for the current user.

My initial thought was to use a computed property to achieve this, but I seem to be facing some difficulties in making it work.

In the code snippet below, I left a comment outlining my approach, but unfortunately, it's not producing the desired output. Specifically, I want the row to be highlighted in pink only for the user 'nathan'.

new Vue({
  el: '#app',
  data: {
    currentUser: 'nathan',
    users: [{
        name: "nathan",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c6c9dcc0c9c6e8cfc5c9c1c486cbc7c5">[email protected]</a>"
      },
      {
        name: "sally",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8c9e939386bf98929e9693d19c9092">[email protected]</a>"
      },
      {
        name: "joe",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ee4e1ebcee9e3efe7e2a0ede1e3">[email protected]</a>"
      }
    ],
  },
  computed: {
    styles: function(user) {
      let height = 100

      // something like this?
      // if(user === currentUser)
      if (user) {
        return {
          'background-color': 'pink'
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <table>
    <tr v-for="user in users">
      <td v-bind:style="styles">{{ user.name }}</td>
      <td v-bind:style="styles">{{ user.email }}</td>
    </tr>
  </table>
</div>

Answer №1

The computed property cannot receive parameters, so the user parameter will not be populated.

If you want to apply a specific class based on a condition, you can create a class and assign it to the row accordingly.

new Vue({
  el: '#app',
  data: {
    currentUser: 'nathan',
    users: [{
        name: "nathan",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e9e6f3efe6e9c7e0eae6eeeba9e4e8ea">[email protected]</a>"
      },
      {
        name: "sally",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed9e8c818194ad8a808c84c38e8280">[email protected]</a>"
      },
      {
        name: "joe",
        email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad0d5dffaddd7dbd3d694d9d5d7">[email protected]</a>"
      }
    ],
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <table>
    <tr v-for="user in users" :class="{'current-user' : user.name === currentUser}">
      <td>{{ user.name }}</td>
      <td>{{ user.email }}</td>
    </tr>
  </table>
</div>

<style>
  tr.current-user {
    background-color: pink;
  }
</style>

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 dividing HTML code on a page into individual nodes

I am looking to extract the HTML content from a website and then parse it into nodes. I attempted the following code: function load() { $(document).ready(function () { $.get("https://example.com/index.html", function (data) { const loadpage ...

Using Node.js and Typescript to implement a chain of logical AND operations with an array of any length

Setting: I am developing a versatile function that determines a boolean outcome based on logical AND operations. However, the function is designed to handle various types of objects and arrays, each requiring different conditions. Currently, my code look ...

What is the best way to pass a module from the controller to a Jade/Pug template and then use it as an argument in an event handler within the template?

I passed the module 'naija' to a template from the controller in this way: res.render('testing', {title:"filter setting page", nigeria:naija }); The func ...

How to use plain JavaScript to extract the value from a textarea in GWT

Situation: I am currently developing a tampermonkey script to enhance certain GWT pages within a third-party application. Unfortunately, I do not have access to the source code or servers. Challenge: My task is to retrieve the value of a textarea element ...

Close specific child python processes as needed, triggered by a jQuery event

Having trouble managing child processes independently without affecting the main parent process in a web client using jQuery? Look no further. Here's my scenario: I've got a Flask server hosting a basic webpage with a button. Clicking the button ...

The SWT Browser is failing to display Angular 2 pages within the Eclipse view

I attempted to embed Angular 2 HTML pages within the SWT Browser widget, but it seems that the Angular 2 HTML pages are not displaying correctly inside the SWT Browser. However, I had no trouble embedding Angular 1 (or Angular JS) pages within the SWT bro ...

What is the method for incorporating choices into a schema field in Mongoose?

If my Schema is like this: var fooSchema = new Schema({ foo: String }); and I'm looking to implement select: false for foo, how can I achieve this without altering the initial structure? var fooSchema = new Schema({ foo: { type: String, select: ...

Organizing Angular Material Styles using Namespacing

In an attempt to develop reusable components with Angular 1.4.3 and Angular-Material 1.0.5, the goal is to seamlessly integrate these components across various applications. However, a challenge arises as the Angular Material CSS contains styling rules th ...

There are occasions when the Phaser sprite.kill() function fails to execute

Currently, I am developing games using Phaser and have encountered an issue with the sprite.kill() method. At times, when I invoke sprite.kill(), it appears that Phaser destroys the body for collisions/overlapping, but the visual elements (image and dragg ...

How can I make a website enable text selection?

After purchasing access to a language learning website, I was disappointed to discover that I couldn't even select text on the pages. It appears they offer an "upgrade" option for users to download lesson texts, and it seems like the ability to selec ...

Evaluating the conditional rendering of a React child component using Jest and Enzyme

I am currently working on expanding the test coverage for this particular file: import React, { useContext } from 'react'; import UserContext from '../../contexts/user'; import styles from './index-styles.scss'; const UserLog ...

Customize RequireJS dependencies for flexible dependency injection

Currently, I am faced with integrating a component that would greatly benefit from Dependency Injection (DI) into an existing framework where DI was not considered during its initial design. The configuration defining dependencies is sourced from a backend ...

What is the best way to use Jquery ScrollTo() to navigate to an element on a page using a class selector

Building on the information from this source, I have a question: How can I resolve the same issue using a class selector like $('.class')? I am encountering an error message that says Uncaught TypeError: undefined is not a function This occurs ...

A step-by-step guide to effectively stubbing a dependency within an express middleware using a combination of express-validator, mocha, chai, sinon,

**Edit: Re-created with a simple example that works first: I have an example file and two modules. moduleA has a dependency called moduleB // moduleA.js const ModuleB = require('./moduleB'); function functionA() { return 20 + ModuleB.functio ...

Numerous CSS/JS Dropdown Menus

I am seeking the ability to implement a dropdown through CSS in various locations within my HTML prototype. This implies that I require the capability to position multiple dropdowns anywhere on the page. Currently, I utilize this method to generate a sing ...

a guide on configuring a default input value from a database in a React component

In my current project, I have an input field with the type of "checkout" and I am looking to utilize Firestore to retrieve a default value. Once I obtain this value, I plan to store it in the state so that it can be modified and updated as needed. Here i ...

The expiration period set in expireAfterSeconds doesn't seem to be functioning as expected in the time-to-live (ttl) configuration. Rows are

Can you please take a look at my code and provide some feedback? The issue I am facing is that the record is getting deleted without specifying the number of seconds. I have tried changing from createIndex to ensureIndex but it's still not working as ...

How can the value be passed as an Array to the data in Vue.js?

Passing the object value as props into my "data" studentId within the form is a key aspect of my project. data() { return { form: new Form({ studentId: [] }) }; }, In the method, the initial values for classlists are set to ...

Exploring Date Comparisons in AngularJS

Currently, I am in the process of developing a web application using AngularJS and Rails. One of the features involves creating bookings through a bookings function. In the dashboard section of the app, I aim to have two tabs - one for Current Bookings and ...

Upon triggering a GET request to the URL "http://localhost:3000/search", a "404 (Not Found)" error response was received. Interestingly

Can Someone assist me please? I'm having trouble creating a website similar to YouTube and encountering the following error: GET http://localhost:3000/search 404 (Not Found) Uncaught (in promise) Error: Request failed with status code 404 at createEr ...