Utilizing dynamic class and color binding features in VueJs?

I need help with implementing a Custom Sort method on my divs to arrange them in ascending or descending order. My query is how can I pre-set the icon color to grey by default, and only change it to black when clicked, while keeping the others greyed out as demonstrated in Vuetify data tables here.

For reference, here is a link to my pen.

new Vue({
  el: '#app',
  data() {
    return {
      headers: [{
          text: "Name",
          value: "name"
        },
        {
          text: "Grades",
          value: "grades"
        }
      ],
      labels: ["Andy", "Max", "John", "Travis", "Rick"],
      Grades: [99, 72, 66, 84, 91],
      sortKey: "",
      direction: 1
    }
  },
  computed: {
    tableItems() {
      let retVal = this.labels.map((label, i) => {
        return {
          name: label,
          grades: this.Grades[i]
        };
      });
      
      if (this.sortKey) {
        retVal.sort((a, b) =>
          this.direction *
          (a[this.sortKey] < b[this.sortKey] ? -1 : 1)
        );
      }
      return retVal;
    }
  },
  methods: {
    sortBy(prop) {
      if (this.sortKey === prop) {
        this.direction *= -1
      }
      this.sortKey = prop;
      console.log(prop);
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44323121302d223d04756a716a7570">[email protected]</a>/dist/vuetify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9fe9eafaebf6f9e6dfaeb1aab1aeab">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
  <v-app id="inspire">
    <v-container>
      <v-layout>
        <v-flex v-for="header in headers" :key="header.text" xs4 py-1>
          <span>
                {{ header.text }}
                <v-icon small @click="sortBy(header.value)">arrow_upward</v-icon>
            </span>
      </v-layout>
      <v-layout v-for="item in tableItems" :key="item.name">
        <v-flex xs4 py-1>
          <span>{{ item.name }}</span>
        </v-flex>
        <v-flex xs4 py-1>
          <span>{{item.grades}}</span>
        </v-flex>
      </v-layout>
    </v-container>
  </v-app>
</div>

I am trying to replicate the functionality of Vuetify data tables but struggling with binding the color of the icon based on the header value when clicked.

Answer №1

A simple solution would be to use a dynamic class name that changes based on a method to check your sortKey value, either directly on the icon or on a parent element.

Customized Pen

:class="{ current: sortKey == header.value }"

Answer №2

If you want to enhance your code...

customizeIconStyle(value) {
  return (this.direction===1 && value===this.sortKey)?'black--text':'grey--text'
}

You can apply it in this way...

<v-icon small @click="updateSortBy(header.value)" :class="customizeIconStyle(header.value)">arrow_upward</v-icon>

Check out the custom demo here

Another way to handle the icon styling is...

<v-icon small @click="updateSortBy(header.value)" :class="customizeIconStyle(header.value)">{{setCustomIcon(header.value)}}</v-icon>

setCustomIcon(value) {
    return (this.direction===1 && value===this.sortKey)?'arrow_upward':'arrow_downward'
}

View the updated customized Codeply

Answer №3

Incorporate the icon as its own stateful component by creating a single file component called Icon.vue. Then, simply import it into your Vue instance where necessary.

<template>
  <i @click="setActive" v-bind:class="{ active: isActive }"></i>
</template>

<script>
  data() {
    return {
      active: false
    }
  },
  methods: {
    setActive() {
      active = true
    }
  },
  computed: {
    isActive() {
    return active
  }
}
</script>

<style scoped>
  .active {
    /* Define styles for 'active' state here */
  }
</style>

Import the Icon component:

import Icon from 'wherever/icon/is.vue'
/* ... */
components: {
  'icon': Icon,
}

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

A Node.js feature that enables atomic file replacement, triggering watchers only once

I have a unique scenario where I need to handle two types of processes. One process is responsible for writing a file, while the other processes are required to read it whenever there is a change. In my research, I came across fs.watch as a solution to ke ...

What are the solutions for repairing direct links in vue router?

I have a Vue.js single-page application hosted on Azure that functions properly in production when starting from the homepage. However, I encounter a problem when trying to directly access links or refresh pages as it leads me to a 404 error page. I suspe ...

Endless loops: How React JS components can render indefinitely

Every time I try to render a screen with the Bar component, it seems to get stuck in an infinite loop without even passing any data. I tested importing react-chartjs-2 and that worked fine, loading just once. However, the other bar chart keeps rendering co ...

What is the best way to selectively print the contents of a child window upon page load?

I've created a function that opens a child window and fills it with content using AJAX. function OpenWindow(){ jQuery.ajax({ type: 'POST', data: { //some fields }, url: 'getPageForPrint.php', su ...

Exploring new classes with jQuery's .not() and :not()?

I am working on a memory game where I need to flip cards and check if two are equal. My issue is that I do not want the function to run when clicking on a card that is already flipped, or on another flipped card. I tried using jQuery's .not() and :no ...

Could you explain the distinction between Node.bind() and Template Binding?

Currently, I am exploring the documentation for Google Polymer and have come across two types of data binding - Node.bind() and Template Binding. Can someone please explain the distinction between Node.bind() and Template Binding to me? ...

What is the most effective method for displaying a child modal within a map?

Project link: Check out my project here! I have two key files in my project - App.js and PageFive.js. In App.js, there is an array defined as follows: state = { boxes: [ { cbIndex: "cb1", name: "Bob" }, { cbI ...

Storing Boolean values in MongoDB with Express JS: A Step-by-Step Guide

I am encountering an issue where a Boolean value that I am trying to store in Mongodb always returns false. Below is the schema of my database: const UserSchema = new Schema({ name: String, password: { type: String, required: true }, isAdmi ...

The promise is unexpectedly fulfilled ahead of schedule without returning the expected value from an AXIOS call

My current challenge involves making a request to a service that rapidly generates multiple strings. The problem lies in returning a promise from this service, as I lack control over the string-generation process. It is crucial for me to return a promise ...

Changing the CSS property from "display: none" to "display: block" using JavaScript causes all the div elements to overlap

My issue involves several radio inputs where clicking one should reveal a hidden div containing information. However, when this div appears, it overlaps with the footer instead of staying positioned between the footer and radio input as intended. I am str ...

Are there any better methods for transforming a class component into a hook component?

After some attempts, I'm working on getting this code to function properly using useState: https://codesandbox.io/s/rdg-cell-editing-forked-nc7wy?file=/src/index.js:0-1146 Despite my efforts, I encountered an error message that reads as follows: × ...

Node.js and Express - Dealing with Callbacks that Return Undefined Prematurely

I've hit a roadblock with this issue that's been haunting me for weeks. The first function in my code queries a MySQL database and returns a response, which is then processed by the second function. The problem lies in the fact that JavaScript ...

After reloading the page, Nuxt dynamic routes are displaying a 404 error

Hey there! I'm currently diving into a project that involves using nuxt js, and it's all new to me. I've set it up in spa mode without any modifications in the nuxt config file, just sticking with the default settings. Here's how I&apos ...

Attempting to extract a parameter from a URL and pass it as an argument to a function for the purpose of locating objects based on

I am trying to retrieve data from a URL http://localhost:3000/share/user=sampleuser to display objects with an author value that matches the one in the URL. However, I encountered an error when attempting to call a function that extracts the value from t ...

Customize v-text-field label styling using Vuefity translate

Looking to localize the text displayed on v-text-field and vuetify-google-autocomplete components in Vuetify. Here's a snippet of the code: <template>(...) <v-text-field label="$t('common.nameLabel')" v-model="registerName" r ...

Obtaining input value when button is clicked

I am currently working on a feature where, upon clicking the Submit button, I aim to retrieve the value entered into the input field in order to update the h1 element. import React from "react"; function App() { const [headingText, setHeadingT ...

What exactly does the question mark represent in the code structure as indicated in VSCode?

When looking at the image, you can see that in the description of done(), VSCode indicates the type of parameters using a colon error: any or sometimes with a question mark and colon user?: any. So, what exactly is the distinction between these two ways o ...

Exploring the integration of data from two Firestore collections using JavaScript

I manage two different types of collections, one being called CURRENCY-PAIR and the other Alerts. The collection CURRENCY-PAIR includes the following information: Currency-Pair Name Currency-AskPrice Currency-BidPrice On the other hand, the Alerts colle ...

I'm encountering an issue where the same id is being passed to every function when I transfer it to the function parameter in a v-for cycle. What steps can I

When I pass the id parameter to the submitOrder function within a v-for loop, each function call with that parameter (id) ends up being the same every time. I'm puzzled as to why this is happening. <div v-for="order in orders"> <button ...

What is the best way to display a component in Blade?

Why isn't my app code functioning properly within Laravel? When I set the address /admin, everything works correctly and displays the admin.blade.php template. However, when I set admin/one, it still shows admin.blade.php but the content of the app is ...