Learn how to make a specific row blink twice with the power of Vue.js and Vuetify

Once a record is successfully created, I have implemented a feature where an alert message appears in green if the API returns 200 OK, and red if not. This functionality is currently working flawlessly thanks to my use of Vuex + Vuetify Snackbar.

this.notificationData = {
    color: 'green',
    text: campaign.name + ' - deleted successfully !'
}



<v-snackbar timeout="1000" v-model="notification" absolute top :color="notificationData.color" outlined right>
    <strong>
        {{ notificationData.text }}
    </strong>
</v-snackbar>

https://i.sstatic.net/lGnoN.png

In order to enhance the user experience even further, I am looking to add a slight animation effect by making that specific row blink for 1 second (2 times).

I do have access to the campaign.name property.

How can I achieve this in Vue.js?

Answer №1

If you want to add a custom row class to your v-data-table, you can utilize the item-class prop.

As stated in the documentation, this prop allows you to specify a property on each item that contains the class for that row.

You can provide a function to this prop that determines the class based on certain conditions, such as adding a class like blink.

Here's a demonstration showcasing this feature: (be sure to click on Full page after running the code snippet below to appreciate the effect)

Vue.config.productionTip = false;
Vue.config.devtools = false;

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      rowToBlink: null,
      items: [{
          id: 1,
          name: 'Frozen Yogurt',
          calories: 159,
        },
        {
          id: 2,
          name: 'Ice cream sandwich',
          calories: 237,
        },
        {
          id: 3,
          name: 'Eclair',
          calories: 262,
        },
        {
          id: 4,
          name: 'Cupcake',
          calories: 305,
        },
      ],
      headers: [{
          text: 'Dessert',
          value: 'name',
        },
        {
          text: 'Calories',
          value: 'calories'
        },
      ],
    };
  },
  methods: {
    blink(item) {
      if (item.id === this.rowToBlink) return 'blink';
      return '';
    }
  },
});
.blink {
  animation: blinking ease-out 1s 2;
}

@keyframes blinking {
  0% {
    background-color: #06c3d1;
  }
  100% {
    background-color: #fff;
  }
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f0f9f8e2d6a2b8ee">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394f4c5c4d505f40790b1741">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6315160623514d1b">[email protected]</a>/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097f7c6c7d606f70493b2771">[email protected]</a>/dist/vuetify.js"></script>

<div id="app">
  <v-app>
    <v-container>
      <v-row class="pa-5">
        <v-autocomplete v-model="rowToBlink" outlined label="select row to blink" :items="items" item-text="name" item-value="id"></v-text-field>
      </v-row>
      <v-row class="px-5">
        <v-data-table hide-default-footer :headers="headers" :items="items" :item-class="blink"></v-data-table>
      </v-row>
    </v-container>
  </v-app>
</div>

In the example provided, I connected the data table items array to a select box for easy selection and applied a method called blink to the item-class prop of the data table component.

The purpose of the blink function is to assign the 'blink' class to the specified row in the table if the selected item's ID matches the item's ID, or return no class otherwise.

To achieve the blinking effect, use the defined .blink CSS class where you can customize the animation duration, delay, easing function, etc.

For real-world implementation, consider storing important information (like campaign names) in variables retrieved from API calls:

async apiCall(someArg, campaignName) {
 const res = await apiFn(someArg);
 // Store the campaign name if it meets certain criteria
 if (res) this.campaignName = campaignName;
}

Your item-class function could then look something like this:

blink(item) {
 if (item.name === this.campaignName) return 'blink';
 return '';
}

Answer №2

To achieve this effect, transitions can be utilized.

Vue.js offers a variety of transitions to choose from, such as CSS transition or animate.

Explore Vue.js Transitions

Through Vue.js’ transition system, automatic transition effects can be applied when elements are added or removed from the DOM. Vue.js will handle the addition/removal of CSS classes at appropriate times to trigger CSS transitions or animations for you. You also have the option to provide JavaScript hook functions for custom DOM manipulations during the transition process.

DEMONSTRATION:

Add the desired transition style or create a new one in your CSS file and apply it to your element.

transition="blink"

Next, define three CSS rules: .blink-transition, .blink-enter, and .blink-leave.

.blink-transition {
  transition: all 1s ease;
  background-color: green;
}

.blink-enter, .blink-leave {
  background-color: white;
}

By following these steps, the element will blink for one second as specified in the example provided.

If blinking is not your intended effect, CSS keyframes may be preferable. Refer to the documentation for more information.

UPDATE:

A transition component is now available in Vue.js.

Discover Vue.js 2 Transitions

Instead of specifying a transition parameter, wrap the element you wish to animate with a transition element.

<transition name="blink"> </transition>

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

Trigger the click event on a specific class selector to extract the corresponding ID

I have an HTML Table with each row: <table> <tr><td><a href='#' id='1' class='delete'>delete</a></td></tr> <tr><td><a href='#' id='2' class='de ...

AngularJS redirection to a different state by utilizing a URL

When I need to direct a user to a specific state, typically I would use the following code: $state.go('state_name'); $state.transitionTo('state_name'); This method usually takes users to /state_name. However, there is one particular s ...

No children were located within the div element

Presently, I am attempting to disable an image for the initial element in each faqs div class in HTML. To achieve this, I am aiming to target the parent element of faqs and then navigate downwards to locate the initial list element using the following Java ...

What are some strategies for stopping a form from redirecting me while utilizing ReactJS and ExpressJS?

Recently, I created a form that redirects to the route /repair upon submission with an action of /repair. However, I would prefer to keep it as a Single Page Application (SPA) where after submitting the form, the fields are cleared, and a simple message l ...

The beforePopState event in next/router is not triggering as expected

Noticing an issue where the beforePopState event is not triggering when I use the back button. This code snippet is part of a hook defined in _app.js according to the documentation. The current version being used is 12.1.5 If anyone has insights on what ...

Adjust the color of the background when hovering with the cursor

I am currently working on a project with a menu of buttons that dynamically changes based on the elements in a list. For instance, if my list contains ["A", "B", "C"], then I would have a menu with 3 buttons. Here is how I display the buttons using a java ...

Is it possible to hide the <dd> elements within a <dl> using knockout's custom data binding upon initialization?

I have implemented a <dl> where the <dd> can be expanded/collapsed by clicking on the corresponding <dt> using knockout's data binding. The inspiration for my solution came from a tutorial on creating custom bindings. Currently, I h ...

Discover the step-by-step guide to integrating the Oxford Dictionary API with ReactJS!

I am a beginner in the world of React and I'm currently working on creating a dictionary web application. My goal is to utilize the API provided by Oxford dictionary. I have already signed up for the prototype version and obtained my API_ID and API_KE ...

Locate the selected radio button's label

There are 25 radio button groups on my page. Each group has a specific action that needs to be performed when a radio button is selected. In order to execute the correct action for each group, I require the NAME attribute of that particular radio group. ...

Exploring the Subfolder of Laravel and Vue

Currently, I am working on a project that involves Laravel and VueJS. The Laravel application is nested in a subdirectory (www.dominio.com/subdirectory). I have made adjustments to my Apache vHost settings so that when accessing that specific URL, it redir ...

Create a row in React JS that includes both a selection option and a button without using any CSS

My dilemma involves a basic form consisting of a select element and a button. What I want to accomplish is shifting the position of the form to the right directly after the select element https://i.sstatic.net/3gfkO.png Below is the code snippet that I ha ...

What could be the reason for a code running successfully in node but not in the REPL environment?

This is the current script I'm working with: const lib = require('./lib.js'); const fs = require('fs'); const graph = fs.readFileSync('../js-working-dir/add_graph.pb', 'utf8'); const sess = new lib.Session(gr ...

Tips for retrieving an input value following a DOM insertion

Developing a function to inject HTML into the DOM following an AJAX call like this: The function for injecting HTML $.ajax({ type: "POST", url: base_url + "main/GetTheLastSeq" }) .done(function( msg1 ) { ...

Tips on transitioning from Modal1 to Modal2 in Ionic after a successful submit button press?

My survey app requires users to fill out a form and click the Submit Feedback button. Upon successful submission, a message saying "Feedback submitted successfully" should be displayed in Modal2, which is inside another modal named (Modal1). However, being ...

Renaming errors within a project with a complex nested structure using npm

I am encountering an issue in my NodeJS project which consists of nested subprojects with their own package.json files. Whenever I make changes to dependencies in the subprojects, I encounter errors similar to the one below: npm ERR! code ENOENT npm ERR! ...

Using JQuery to enable the movement of divs on a screen through clicking and dragging, without the use of

Recently, I've been experimenting with a small project involving draggable divs. However, the code I've written doesn't seem to be functioning properly, causing JQuery to become unresponsive. Is there an alternative method that is simple an ...

Trouble toggling Reactstrap navbar in my TypeScript project using NextJS

I recently integrated Reactstrap into my NextJS TypeScript project and encountered an issue with the Navbar component. Despite following the example from the Reactstrap documentation, the mobile toggle menu does not open when clicked. Additionally, none of ...

Issue with clicking a button in Selenium using JavaScript for automation

I'm encountering an issue where Selenium is detecting an element as disabled, despite it being enabled. To work around this, I am attempting to click on the element using JavaScript with the following code snippet: IWebElement button = driver.FindEl ...

Is there a method to avoid redeclaring variables in JavaScript with jQuery?

In the structure of my code, I have the following setup. <!-- first.tpl --> <script> $(document).ready(function() { objIns.loadNames = '{$names|json_encode}'; } ) </script> {include file="second.tpl"} <! ...

Submitting the form with an extending controller results in an undefined state

I have a primary controller and another controller where I extend the primary one to display a different layout. Primary controller: function CustomerInformationController(...) { var vm = this; ... vm.save = function() { if (angular. ...