I am having trouble getting my getColor() method to correctly change colors based on the data provided

When using a datatable, there are two columns: status and priority. The STATUS LIST includes OPEN or CLOSED, while the PRIORITY LIST includes HIGH, MODERATE, and LOW. So, if the status is open, it should be displayed in red color; if closed, then in green. In the priority column, high priority should display in dark blue, low priority as warning color, and moderate priority as info color.

[![enter image description here][1]][1]

Currently, the colors for the Status column are displaying correctly, but the Priority column is not working as expected.

Here is the HTML template:

<v-data-table
  :headers="headers"
  :items="searchFilterArray"
  disable-pagination
  :hide-default-footer="true"
>
           
    <template v-slot:item.status="{ item }">
      <span :class="getColor(item.status)">
        {{ item.status }} // status can be open or closed
      </span>
    </template>
    <template v-slot:item.priority="{ item }">
      <span :class="getColor(item.priority)">
        {{ item.priority }} // priority can be High,Moderate or Low
      </span>
    </template>
</v-data-table>
 methods: {
    getColor (status) {
      console.log('status is : ', status) // Only open and closed are printed in the console. Not sure why Low and High are not displaying as shown in the screenshot.
      if (status === 'closed') return 'v-green font-weight-bold'
      else if (status === 'open') return 'v-error font-weight-bold'
      else if (status === 'High') return 'v-darkblue font-weight-bold'
      else if (status === 'Low') return 'v-warning font-weight-bold'
      else if (status === 'Moderate') return 'v-info font-weight-bold'
      else return 'v-secondary '
    },
 }

Answer №1

If you pass an object containing your conditions, you can simplify the process:

getColor (status) {
    return {
      'v-green font-weight-bold': status === 'closed',
      'v-error font-weight-bold': status === 'open',
      'v-darkblue font-weight-bold': status === 'High',
      'v-warning font-weight-bold': status === 'Low',
      'v-info font-weight-bold': status === 'Moderate',
      // Consider being more specific here with additional conditions 
      // such as using 
      // !['closed', 'open', 'High', 'Low', 'Moderate'].includes(status)
      'v-secondary': !status
    }
}

You don't have to use if/else statements to return the correct classes manually, vue will handle it based on boolean values.


If you're not seeing the expected output in the console.log, it may be due to a slot issue. Make sure your slot is actually being utilized.

Also, ensure that your column is correctly named priority and matches the field in the object. The header name may differ from the actual field name, causing discrepancies.


If your field is priority.name, adjust your template accordingly:

<template v-slot:item.priority.name="{ item }">
  <span :class="getColor(item.priority.name)">
    {{ item.priority.name }} // priority can be High,Moderate or Low
  </span>
</template>

The issue might be that your slot doesn't correspond with the field name, preventing vuetify from calling it properly.

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

Whenever attempting to choose a rating, the MUI dialog continuously refreshes and resets the selected rating

I'm facing an issue with my MUI dialog where it keeps refreshing and resetting the selected rating every time I try to rate a movie. Any assistance on this matter would be highly appreciated. The RateWatchMovieDialog component is designed to display ...

What might be causing my Vue unit test to overlook a function?

I am in the process of creating unit tests for my component dateFormat.js using Jest. The focus is on testing the function formatDateGlobal. Here is an excerpt from the test: import DateFormat from '../dateFormat'; describe('dateFormat.js& ...

How to access global variables in node.js modules?

I'm looking to move some functionality to a new file called helpers.js. Below is the code that I have put in this file. How can I access the app variable within my method so that I can retrieve the config element called Path? Helpers = { fs: requ ...

Utilizing Kendo UI Jsonp in conjunction with a WordPress json plugin: A comprehensive

Issue at Hand: My goal is to modify a kendo UI data-binding example to work with my own jsonp request. Description of the Problem: Starting from a data-binding example here, I have created this jsfiddle that demonstrates the desired functionality. To a ...

Having trouble with Nativescript Vue Developer Tools?

Currently, I am following the tutorial exactly as described here: The development tools are installed and the application is running smoothly on my android emulator. However, I encounter a message in the dev tools that says "Waiting for connection..." des ...

Guide on integrating the Nuxt Auth library for simulating components in Nuxt using Jest

Currently diving into the world of JavaScript. Recently, I created a Nuxt application and successfully integrated the @nuxt/auth middleware globally in my nuxt.config.js. Everything is working smoothly within my application. Now, I am eager to test some ...

JavaScript doesn't seem to be functioning properly within PHP

I have implemented the supersized jQuery script to incorporate sliding backgrounds on my WordPress page. Now, I aim to create unique slides for different sites and require a PHP if request. This is my code: <?php if ( is_page(array('Restaurant&a ...

Unable to detect the click event on Bootstrap accordion

I am currently utilizing Bootstrap to populate a table with data and then attempting to capture an accordion show event. My intention is to extract the div id in order to make an ajax call for more information on the respective item. Unfortunately, I have ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

What steps should I take to ensure that clicking this button triggers the API request and returns the data in JSON format?

I'm attempting to have the button with id 'testp' return an api request in json format, however it seems to not be functioning properly. You can find the HTML code link here: https://github.com/atullu1234/REST-API-Developer-1/blob/main/js-bu ...

Require assistance with understanding the aes-256-cbc encryption using oaepHash

Procedure for Secure Data Encryption: Generate a random 256-bit encryption key (K_s). For each Personally Identifiable Information (PII) value in the payload: 1. Pad the plaintext with PKCS#7 padding. 2. Create a random 128-bit Initialization Vector (IV). ...

Encounter issue when using GAS withSuccessHandler function

I've developed a Google Sheets add-on that utilizes a modal dialog for the user interface. I encountered an issue with the success handler not running as expected, so I created a basic test interface to troubleshoot the problem. After the server-side ...

Adding new elements to an array does not activate the reactivity of Vue

After discovering that editing objects within an array doesn't function properly in vue.js due to its limitations, I tried using vue.set to resolve the issue, but it's proving to be quite challenging for me. Let's take a look at the sample ...

Pressing the ENTER key does not submit the text box data in Rails/Bootstrap, but clicking the SUBMIT button does

I need assistance with my Rails 4 and Bootstrap 3 project. I have implemented an email address field on my page (localhost:3000) where users can enter their email and click the SUBMIT button to send it to the MongoDB database. However, pressing the ENTER k ...

The ng-message function appears to be malfunctioning

I am facing an issue with the angularjs ng-message not working in my code snippet. You can view the code on JSfiddle <div ng-app="app" ng-controller="myctrl"> <form name="myform" novalidate> error: {{myform.definition.$error ...

The proper way to define an event delegator's syntax

Typically, when you want to handle a button click event, you would do so like this: $(document).ready(function() { $("button").click(function() { doSomething(); }); }); However, in the scenario of an event delegator, you may need to respon ...

Guidelines for incorporating role-based permissions in a React application

I am developing a MERN application that has various features catering to different roles. I need the ability to display specific data, navigation options, and divs based on the user's role while hiding these elements from others. It is important to no ...

Concentrating on a Div Element in React

I'm trying to set up an onKeyPress event that will be triggered when a key is pressed while a 'Level' element is displayed. I know that the div needs to be focused for events to register, but I'm not sure how to do this with functional ...

How can I prevent event propagation in Vuetify's v-switch component?

Currently, I am working with Vuetify and have incorporated the use of v-data-table. Whenever I click on a row within this data table, a corresponding dialog box represented by v-dialog should open. Additionally, each row contains a v-switch element. Howeve ...

Obtain a report using a variety of different conditions

My database has a table with the following fields: TPI CLICKS IMPRESSION CLASSIFY I am looking to retrieve 3 specific results: 1) Calculate SUM(CLICKS)/SUM(IMPRESSION) * 100 GROUPED BY TPI 2) Calculate SUM(IMPRESSION) WHERE CLASSIFY = "XYZ" GROUPED BY ...