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

Utilizing jQuery to place an element beside another and maintain its position

I need ElementA to be positioned next to ElementB as shown in this example. The key difference is that I want ElementA to move along with ElementB if the latter is relocated. Is there a way to keep a specific element fixed to another one? Re-calculating ...

Troubleshooting not locating view files in ExpressJS and implementing client-side JavaScript and CSS deployment in ExpressJS

My JavaScript file is located in the same directory as my HTML file, and I am trying to render it. I'm unsure of what I may be missing. How difficult could it possibly be? var express = require('express'); var app = express(); app.engine( ...

Unable to proceed, WISTIA upload frozen at 100% complete

I recently integrated the Wistia API for video uploads. Everything seemed to be working fine as per the Wistia documentation until I encountered an issue. After uploading a video file, the progress bar would reach 100%, but then an error was logged in the ...

Displaying subtotal in a list using Vue.js and conditional rendering with v-if statement

Seeking guidance on calculating a total for a vue.js list that contains invoice items. To illustrate, let's consider a scenario where a table of invoice items is being rendered. Here is the code snippet: <table> <template v-for="(invoice_ite ...

When working with create-react-app and TypeScript, you may encounter an error stating: "JSX expressions in 'file_name.tsx' must

After setting up a React project with TypeScript using the CLI command create-react-app client --typescript, I encountered a compilation error when running npm start: ./src/App.js Line 26:13: 'React' must be in scope when using JSX react/r ...

Troubleshooting React child problems in TypeScript

I am facing a coding issue and have provided all the necessary code for reference. Despite trying numerous solutions, I am still unable to resolve it. export class JobBuilderOptimise extends React.Component<JobBuilderOptimiseProps & JobBuilderOptim ...

Combining Multiple Results into One Unique Output using a Lodash Function

<span v-for="r in results" >{{ r.owner.first_name }} {{ r.owner.last_name}} </span> I want to ensure that only one occurrence is displayed for the result. So, with the provided code snippet, I am currently seeing: John Doe John Doe John ...

Adding an object to an array using the Array.push() method deposits an array

Currently, I am extracting the days of absence of my colleague from excel files and storing them in a MongoDB database using Mongoose and Express.js. The process of reading the data is smooth; however, when trying to update the data in the database, an un ...

Retrieving server information using AJAX in React

I've been attempting to utilize an AJAX call in order to fetch server data for my React Components. However, I'm encountering difficulties when it comes to displaying it with React as I keep receiving this error: Uncaught TypeError: Cannot read ...

The Ion-icon fails to appear when it is passed as a prop to a different component

On my Dashboard Page, I have a component called <DashHome /> that I'm rendering. I passed in an array of objects containing icons as props, but for some reason, the icons are not getting rendered on the page. However, when I used console.log() t ...

Enabling automatic scaling during the rendering of an .stl file using three.js

I'm in the process of developing a server/webpage with the following functionality: Users can upload an .stl file to be 3D-printed The server executes a mesh repair script on the uploaded .stl file The corrected .stl file is displayed in the user&ap ...

How can you tap into local storage in CSS while utilizing a chrome extension?

Can I access local storage from a Chrome extension's options file using CSS? Is it possible to use JavaScript to define a variable that can be utilized in CSS, or is local storage only accessible through JavaScript? If local storage is restricted to J ...

Converting JSON data into an array of JavaScript objects

Here is some of the JSON data I have: [ { "items": [ { "retailername": "Zop Now", "value": 475 }, { "retailername": "Snap Deal", "value": 265 ...

The Ajax.BeginForm() function is not functioning as expected and is instead directly calling a JavaScript method within the OnSuccess

While working with ASP MVC 5, I have encountered an issue with Ajax.BeginForm() in one of my views. Whenever I submit a form using Ajax.BeginForm, the defined method is not being called. There are no errors thrown or caught, and it directly jumps to the ca ...

Check if the provided arguments in JavaScript are arrays and, if they are, combine them into a single large array

My array variables are as follows: const gumBrands = ['orbit', 'trident', 'chiclet', 'strident']; const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac']; Presen ...

`Can a creation of this nature be accomplished?`

In my text input field, users can type messages to send to me. I'd like to add buttons on the page with symbols like "!", "XD", and "#". When someone clicks on a button, such as the "!" button, that corresponding symbol should be inserted into the tex ...

Ways to obtain data in JavaScript function from HTML

Struggling to pass the key from a database query in HTML using PHP to a JavaScript function? You're not alone. The challenge lies in passing the field_id to the updateRecords() JS function through the onClick() event of an HTML button. Previously, se ...

When using Laravel with Vue and Axios, a peculiar issue arises where a DELETE request sent using

I'm encountering a specific error and I'm struggling to identify the root cause. Instead of providing more explanations, let me show you with code: These are my defined routes : Route::get('/', 'HomeController@index')->n ...

JavaScript: Automatically retrieving the if else function

I need help automating the calculation of the number of days a person worked based on their "in-time" and "out-time". I've tried to implement this in my JS code, but it's not working as expected. HTML <fieldset> In-Time: <input clas ...

How to retrieve specific items from an array contained within an array of objects using Express.js and MongoDB

Within the users array, there is an array of friends. I am looking to retrieve all friends of a specific user based on their email where the approved field is set to true. In my Node.js application, I have defined a user schema in MongoDB: const UserSchem ...