It is possible to mix external code with instances of Vue.js. See the example I did, I created a variable called condicao
out of Vue and associated in Vue instance. When you perform this way, the external variable is transformed into two-way databind.
After creating the two-way databind, you can use external functions to manipulate this data, even when it is changed Vue recognizes and changes the component without any problem.
Also create a function to validate the button, on it you disable or enable according to the conditions of the external variable.
Follows code:
var condicao = {
opcao1: false,
opcao2: false,
opcao3: false,
};
function habilitarBotaoComFuncaoExterna() {
condicao.opcao1 = true;
condicao.opcao2 = true;
condicao.opcao3 = true;
}
function desabilitarBotaoComFuncaoExterna() {
condicao.opcao1 = false;
condicao.opcao2 = false;
condicao.opcao3 = false;
}
new Vue({
el: "#app",
data: {
aba: 1,
condicao: condicao,
},
methods: {
verificaCondicao: function() {
return !!this.condicao.opcao1 && !!this.condicao.opcao2 && !!this.condicao.opcao3;
},
fazAlgumaCoisa: function() {
console.log('Botão clicado, faz alguma coisa...');
},
trocarDeAba: function($event, aba) {
if (!this.verificaCondicao()) {
console.log('Habilite opções para prosseguir com a troca de guia.');
return; // Cancela a ação pois não foi atendida
}
// Troca de aba
this.aba = aba;
}
}
});
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<div id="app">
<div>
<div>
<a href="#" v-on:click.prevent="trocarDeAba($event, 1)">Aba 1</a>
<a href="#" v-on:click.prevent="trocarDeAba($event, 2)">Aba 2</a>
<a href="#" v-on:click.prevent="trocarDeAba($event, 3)">Aba 3</a>
</div>
<div>
<div v-if="aba == 1">Conteúdo Aba 1 ******************</div>
<div v-if="aba == 2">Conteúdo Aba 2 ------------------</div>
<div v-if="aba == 3">Conteúdo Aba 3 ##################</div>
</div>
<div v-if="verificaCondicao()">
<h4>Agora você pode clicar nas abas</h4>
</div>
<div v-else>
<h4>Marque as opções para poder alterar de guia</h4>
</div>
</div>
<hr />
<div>
<button v-on:click="fazAlgumaCoisa()" class="actAplicarTodosFiltros" :disabled="!verificaCondicao()">
Opção com Condições
</button>
</div>
<ul>
<li><input v-model="condicao.opcao1" type="checkbox"> Condição 01</li>
<li><input v-model="condicao.opcao2" type="checkbox"> Condição 02</li>
<li><input v-model="condicao.opcao3" type="checkbox"> Condição 03</li>
</ul>
</div>
<div>
<button onClick="habilitarBotaoComFuncaoExterna()">Fora do Vuejs - Habilitar</button>
<button onClick="desabilitarBotaoComFuncaoExterna()">Fora do Vuejs - Habilitar</button>
</div>
What is the variable that determines whether
.actAplicarTodosFiltros
is disabled or not?– Sergio
actually are variables determined with jQuery, it getting enabled doesn’t make much difference to me at the moment, I’m looking for something like a v-on:click preventdefault of tabs, and only getting enabled when it gets disabled, I don’t know if I would need a kind of watch
– haykou
"variables determined with jQuery" - leave jQuery! : ) I’m kidding, but if you have logic Vue.js this is much simpler. So you could control both. I’ll give you an example.
– Sergio
I’m thinking here and it’s hard to mix jQuery... are different generations and logics. You can show the code that changes the
disabled
of that button?– Sergio
is simple thing, if on another button that when it is clicked it gives a $('. actAplicarTodosFiltros'). attr('disabled', false);
– haykou
And that part is 100% detached from the Vue component?
– Sergio
Aham, disabled is not getting into anything from Vue, only the tabs click
– haykou
Let’s go continue this discussion in chat.
– haykou
Did any of the answers/solved the question?
– Sergio
@Sergio they helped yes, but I ended up solving otherwise
– haykou
Ok! It was interesting to put here as an answer to complete the question and solutions
– Sergio
@Sergio, I will rephrase the question and put my solution, I think I explained wrong because the solution was simpler than it seemed
– haykou