Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I toggle a class to show which tab is active in VueJS?

I have created two tabs, which I would like the class of active set on the first <li> by default.

Then, when the second tab is selected, the .active class should pass to the second <li> and be removed from the first.

I can use CSS to create some style rules in order to give a visual indicator as to which tab is currently active.

I have also created a JS Fiddle to see the current output.

Any help welcome as I am rather stuck.

<ul class="overlay-panel-actions-primary">
    <li v-for="(tab, index) in tabs" @click="currentTab = index">{{tab}}</li>
</ul>
<div class="content-bd">
    <div class="tab-content">
        <div v-show="currentTab === 0">
            List of filters options
        </div>
        <div v-show="currentTab === 1">
            List of sort options
        </div>
    </div>
</div>

new Vue({
  el: "#app",
    data() {
        return {
            currentTab: 0,
            tabs: ['Filter', 'Sort']
        };
    },
})
like image 946
Al-76 Avatar asked Apr 22 '19 11:04

Al-76


People also ask

How do you toggle classes at Vue?

We can toggle a class dynamically in vue by passing an object to the v-bind:class attribute. In this example, we are toggling a class by clicking the Toggle button.

What is active class Vue?

In this lesson, we learn about Vue router active class. The active class is automatically handled by the router-link component and allows us to mark which route we are currently viewing with CSS.

How do I use Vue toggle in Javascript?

Toggle The Active CSS Class Based on the isActive property, let's toggle active class in the button element by adding an object to the :class. The active class, which is the key of the object, will get added to the button when isActive is set to true which is the value of the object. Nice.

What is $V in Vue?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue. js 2.0), which is intended to check every input, which is made in a non-html form.

How to toggle a class dynamically in Vue?

We can toggle a class dynamically in vue by passing an object to the v-bind:class attribute. In this example, we are toggling a class by clicking the Toggle button. In the above code, we have passed {box: isActive} to v-bind:class attribute, where box class will be added to div element if the isActive data property is true; otherwise it is removed.

How to toggle classes in VB NET?

If you click on a Toggle button the box class is removed, and toggle it again box class is added. You can also pass multiple classes like this. After clicking the toggle button: Similarly, we can also toggle classes by passing an array to the v-bind:class attribute.

How to toggle an element class in JavaScript?

How to Toggle an Element Class in JavaScript ? Toggling the class means if there is no class name assigned to the element, then a class name can be assigned to it dynamically or if a certain class is already present, then it can be removed dynamically by just using the toggle () or by using contains (), add (), ...

How do you get the Active class of a class?

A v-for looping over items with i set for the index. The class is then bound to active, only when the index is equal to the activeItem, will this equal true and produce the active class. First, we set the activeItem data = null.


Video Answer


2 Answers

Use this -

:class="{active: currentTab === index}"

<li v-for="(tab, index) in tabs" @click="currentTab = index" :class="{active: currentTab === index}">{{tab}}</li>

Let me know if it works.

Fiddle - https://jsfiddle.net/so3mf8h9/


Update

You can also use ternary operator, It should also work.

:class="currentTab === index ? 'active' : ''"
like image 60
arora Avatar answered Oct 17 '22 07:10

arora


Edit: Ah, sorry I thought you were using vue-router. When your site gets bigger, it may be an idea to start using router, and when you do, this method will work for you 🤓


Vue has this functionality built in 🙌

All you need to do is add this into your stylesheet

.router-link-exact-active {
    // Your styles go here
    border: 5px dashed red;
}

Reference: Vue documentation

Here's an example of how I implemented it in a Vue site I made a couple weeks back: Markup, and Styles. Hope that helps, let me know if you've got any more questions on implementing this 😊

like image 22
Alicia Avatar answered Oct 17 '22 07:10

Alicia