Learn how to dynamically modify the text and color of a column value within a <v-data-table> component in Vue.js 2.6.11 and Vuetify 2.2.11 based on a specific condition

In my current project where I am developing a web application using ASP.NET CORE for the backend and vue.js for the frontend, I encountered an issue with Vuetify's CRUD Datatable UI Component in a page named "Category". The problem arises when trying to dynamically change the value of the Category's "State" column (either "Active" or "Inactive") based on a certain condition. Here is how the Data Table currently looks:

|---------------------|------------------|---------------------|------------------|
|       Options       |       Name       |     Description     |      State       |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |    Videogames    |  Electronic Device  |       true       |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |      Tablets     |  Electronic Device  |       true       |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |   Flat Screens   |  Electronic Device  |       false      |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |      Laptop      |  Electronic Device  |       true       |
|---------------------|------------------|---------------------|------------------|

My goal is to implement a conditional logic where if the Category's State is true, it should be displayed as "Active" in blue text; otherwise, it should be shown as "Inactive" in red text.

|---------------------|------------------|---------------------|------------------|
|       Options       |       Name       |     Description     |      State       |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |    Videogames    |  Electronic Device  |      Active      |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |      Tablets     |  Electronic Device  |      Active      |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |   Flat Screens   |  Electronic Device  |     Inactive     |
|---------------------|------------------|---------------------|------------------|
|    Edit / Delete    |      Laptop      |  Electronic Device  |      Active      |
|---------------------|------------------|---------------------|------------------|

HTML:

<template>
<v-layout align-start>
    <v-flex>
        <v-data-table
        :headers="headers"
        :items="categories"
        :search="search"
        sort-by="name"
        class="elevation-1"
        ...

JAVASCRIPT

<script>
import axios from 'axios'
export default {
    data(){
        return{
            categories:[],
            dialog: false,
            headers: [
                { text: 'Options', value: 'options', sortable: false },
                { text: 'Name', value: 'name' },
                { text: 'Description', value: 'description', sortable: false },
                { text: 'State', value: 'state', sortable: false },
            ],
            search: '',
            editedIndex: -1,
            id: '',
            name: '',
            description: '',
            valid: 0,
            validMessage: [],
        }
    },
...

If you have any suggestions or solutions, feel free to share them! Your input is greatly appreciated.

Answer №1

By utilizing the item.status slot, you can make it happen like so:

<template v-slot:item.status="{ item }">
{{item.status ? ... : ...}}
</template>

Answer №2

It appears that a dynamic class is all you require to change the color in the specified location:

:class="active? 'text--primary' : 'text--red'"
.

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

Obtaining a result from a switch statement

Here is a solution to populate the generated drop-down menu: $('dropdown_options').innerHTML = '&nbsp;'; jsonResponse.forEach(function(element){ $('dropdown_options').innerHTML += '<option value='+ elemen ...

Browsing through an array of objects in PHP

Currently working on creating an array of objects using jQuery. var selected_tests = $("#selected_tests").find("tr"); jsonLab = []; $.each(selected_tests, function() { jsonLab.push({ test: ($(this).children()).eq(0).text(), amount: ($(this).chil ...

Adjusting a polyline in Google Maps

I've successfully used Google Maps V3 API to create a polyline with 2 points. However, I now need to shorten the polyline. Issue: My goal is to make the polyline extend from the start point to the midpoint between the start and end points. How can I ...

document ready function in the Microsoft AJAX client side library

The Microsoft AJAX client-side library contains various functions that imitate the server-side Page Life Cycle and conditions like if (!Page.IsPostBack). I am unsure about which client-side function corresponds to the equivalent server-side method. What is ...

In the world of node.js, functions almost always have a tendency to

As a beginner in the world of nodejs, I am diving into various guides and screencasts to grasp the basics. One aspect that has caught my attention is the handling of async/sync operations, reading files, and understanding how nodejs deals with callbacks/re ...

After closing and reopening the jQuery modal, the name does not appear

I crafted a custom modal window to display images along with comments and the owner's name. The issue arises when I close the current modal and reopen it; the owner's name disappears. Oddly enough, the name is displayed the first time the modal i ...

What is the best way to continuously click a JavaScript link until it disappears?

Many websites utilize single-page pagination to display more content with each click. It can be beneficial to view all the content on one page, such as for web crawling purposes. Much like automatically clicking a button using Greasemonkey, how can JavaScr ...

Issues with jQuery functionality occurring when trying to display or hide div contents on dynamically loaded AJAX content

I have encountered an issue while working on a project that involves showing or hiding a div based on the selection of a drop down value. The show/hide functionality works perfectly when implemented on the same page, but it fails when I try to do the same ...

Does the functionality of JSON.parse include recursion?

After receiving a JSON string in response, I parse it as follows: ring = JSON.parse(response); However, although ring becomes an object, the property ring.stones is only a string when it should also be an object. To address this issue, if I execute: ri ...

Is it better to Vuex - manipulate store item twice, trigger new items, or perform transformations within components each time they are mounted?

I am considering performing two separate transformations on a single, large store item and then saving the results as two new store items. For instance: setEventsData: (state, data) => {...} // main huge master object // perform transformations on it an ...

Speed up - Handle alias resolution with module federation

Currently import federation from '@originjs/vite-plugin-federation'; import react from '@vitejs/plugin-react-swc'; import dns from 'dns'; import path from 'path'; import { visualizer } from 'rollup-plugin-visual ...

Unable to modify page property status through the Notion API

I've been attempting to use the Notion JS-sdk to update a page's status using their API. However, I've run into some issues that I can't seem to resolve. Updating the status requires modifying the properties object, but no matter what ...

Convert JavaScript objects to strings with JSON strings already included as values

Although this question may seem like a duplicate, I have not been able to find the answer. My issue is with stringifying a JavaScript object that contains JSON strings as values. Here is an example: var obj = {id:1, options:"{\"code\":3,\" ...

Finding a solution to the type issue of error handling in Route Handler with NextJS

I'm working on a route handler located at app/api/transactions/route.ts. Here's a snippet of the code: import { NextRequest, NextResponse } from "next/server"; import { AxiosError } from "axios"; import axios from "../axi ...

Revise the reply within ExpressJS

I need help with editing the response to a request in Express. When the request is made via XHR, I want to encapsulate the body inside a JavaScript object. This way, different parts of the page can be accessed individually within the JavaScript object, suc ...

The functionality of form.serialize() seems to be malfunctioning

I encountered an issue with my webpage called "View Employee." When we click on it, all the employees are displayed with an edit button for each one. Below is the corresponding page: echo "<form class="form-horizontal id="update_form" name="update_form ...

Is there a way to encase each v-if statement in my Vue code with a transition effect?

Writing the code for transitions can be quite laborious. <transition name="fade"> <div v-if="condition"> </div> </transition> Is there a simpler way to automatically wrap each v-if with a transition? ...

What is the best way to utilize node exec in synchronous mode?

I have a scenario where I need to run exec synchronously. Is there an alternative method to achieve this without using the deprecated execSync library? Can bluebird promises be used for this purpose? for (var i = 0; i < testCasesLength; i++) { va ...

Strategies for disabling middle mouse button default behavior in Vue

When I use @click.middle.stop.prevent="test", the default scroll wheel still shows up despite it detecting the middle mouse click and calling my function. Is there a way to prevent this from happening? ...

What is the most efficient way to halt the pipe if the value of an HTML input element remains unchanged using RxJS?

I'm currently incorporating RxJS into my Angular 9 project. My goal is to bind a keyup event to an input field and trigger an HTTP request whenever the user types a new value. Here's the code snippet I have: fromEvent(this.inputBox.nativeElemen ...