Carolina de Moraes
Carolina de Moraes

Reputation: 57

Enable a Tab with Javascript on Vuejs

So I'm using Vue and Bootstrap-Vue. The context: This is a register page which I have 2 tabs, where when the first tab is completed then you can hit the button and pass to the second tab. The second one have the 'disabled' attribute, and when I click on my button it should enable the tab. I tried to do a javascript function, but it doesn't seem to be right.

My code is on Fiddle, and also here it is the relevant portion of the code.

<b-tabs class = "the-tabs-group" v-model="tabIndex">
    <b-tab title="Primeira Fase" active>
        <div class="form-group row">  
            <label class="col-sm-1 col-form-label">Email:</label>
            <div class="col-sm-9">
                <input type="email" class="form-control " id="email"  name="email">
            </div>
            <hr>
            <label class="col-sm-1 col-form-label">Senha:</label>
            <div class="col-sm-9">
                <input type="password" class="form-control" id="pwd" name="pwd">
            </div>
            <hr>
            <label class= "confirmsenha col-sm-1 col-form-label">Confirmar senha:</label>
            <div class="col-sm-9">
                <input type="password" class="form-control" id="cpwd" name="cpwd">
             </div>
        </div>
    </b-tab>
    <b-tab id="segundaFase" title="Segunda Fase" disabled >
        <div id = "bb">
            <h1>Quase lá!</h1>
            <p>Loren ipsum sit amet,consecteur adisplinig elit.Etiam eget commodo nignbi phometehues</p>
        </div>      
    </b-tab>
</b-tabs>

<div class="next">
    <a>
        <b-btn id="nois" class="btn-link" @click="tabIndex++; enableTab">Próxima Fase</b-btn>
    </a>
</div>

<script>
new Vue({
    el: '#app',
    data: {
        tabIndex: 0
    },
    methods:{
        enableTab: function(){
        console.log("chamou");
        var tab = document.getElementById("segundaFase");
        tab.removeAttribute('disabled'); }
            }
})
</script>

Upvotes: 3

Views: 4365

Answers (3)

Pierre_T
Pierre_T

Reputation: 1302

You could set a activeTab in your component data :

data: {
   activeTab: 'tab1',
   name:'',
   email:''
  },

then 2 simple methods to set the activetab and return your boolean you will use to render your html :

 methods: {
    isActiveTab(tabId){
        return this.activeTab === tabId
    },
    setActiveTab(tabId){
        this.activeTab = tabId
    }

..and a computed for your :disabled property of your button element

computed: {
    formIsComplete(){
    //here you might want to add some more adequate validation rules
    return this.email.length > 0 && this.name.length > 0
    }
  }

Html side :

<div id="app">
  <div class="tab" v-if="isActiveTab('tab1')">
    <h2>Tab 1:</h2>
    <span> Whatever content you want in tab1</span>
    <input v-model="email" type="email"/><label>Email</label>
    <input v-model="name" type="text"/><label>Name</label

      <button @click="setActiveTab('tab2')" :disabled="!formIsComplete">
          Change tab
      </button>

  </div>

  <div class="tab" v-if="isActiveTab('tab2')">
    <h2>Tab 2:</h2>
    <span> Congrats you made it to Tab2</span>
  </div>
</div>

See a simple fiddle here

Upvotes: 4

Steven Spungin
Steven Spungin

Reputation: 29071

Assign your attribute to a reactive data member

<b-tab id="segundaFase" title="Segunda Fase" :disabled=‘disabled’ >


data: {disabled:true}

In your click handler, mutate the data

onClick(){ this.disabled=!this.disabled}

Upvotes: 2

gijoe
gijoe

Reputation: 1189

You can achieve this option with another way by using the disabled property of a tab I will introduce some code below and explain

<div id="app">

 <b-tabs>

   <b-tab title="first" active>
     <br>I'm the first tab
     <button v-on:click="enableThirdTab">Enable third tab</button>
   </b-tab>

   <b-tab title="second">
      <br>I'm the second tab
   </b-tab>

   <b-tab title="third" v-bind:disabled="tabDisabled">
     <br>Im the third tab
   </b-tab>

 </b-tabs>

</div>




new Vue({
  el:'#app',
  data:function(){
   return {
     tabDisabled:true
   }
  },
 methods:{
    enableThirdTab:function(){
      this.tabDisabled = false;
   }
 }

});

You can use the property disabled of the b-tab to enable or disable it programmitcally

So in your data function create a property tabDisabled with true as first state and bind it to the tab as v-bind:disabled="tabDisabled"

Then you can have the method in methods:{} enableTab where all you need to do is to set this.tabDisabled = false;

Upvotes: 1

Related Questions