I am creating a navbar in VueJS where I toggle on or off drop-down menus. I have no problem toggling an individual drop-down menu, but I cannot figure out how to handle multiple-drop down menus so that if I open up one menu the other ones will automatically close.
For instance, let's take a look at the following code:
// template
<button #click="isMenuOneOpen = !isMenuOneOpen">Menu One<button>
<button #click="isMenuTwoOpen = !isMenuTwoOpen">Menu Two<button>
<button #click="isMenuThreeOpen = !isMenuThreeOpen">Menu Three<button>
<button #click="isMenuFourOpen = !isMenuFourOpen">Menu Four<button>
...
// script --> data
isMenuOneOpen: false,
isMenuTwoOpen: false,
isMenuThreeOpen: false,
isMenuFourOpen: false,
Let's say I click on menu one and then click on menu two (and then three and four). With my current code, all four menus will be open. What I want, though, is that if I click on a particular menu, not only will it open up but the other drop-down menus will close.
How can I do this using VueJS?
Thanks.
Here's a sample code. But the problem here is that, once you disable the button, you won't be able to click it. Maybe add a checkbox to enable all buttons?
Here's the html:
<div id="app">
<v-app id="inspire">
<div v-for="button in buttons">
<button #click="updateMenu(button.name)" :disabled='button.isDisabled'>{{ button.name }}</button>
</div>
</v-app>
</div>
Here's the javascript:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: () => ({
buttons: [{name: "menu one", isDisabled: false}, {name: "menu two", isDisabled: false}, {name: "menu three", isDisabled: false}, {name: "menu four", isDisabled: false}]
}),
methods: {
updateMenu: function (name) {
const button = this.buttons.find(b => b.name === name);
button.isDisabled = false;
const toDisableButtons = this.buttons.filter(b => b.name != name);
toDisableButtons.forEach(function(item) {
item.isDisabled = true;
});
}
}
});
Related
Vuetify disabled prop directly disabled input itself.I am trying to disable individual select option as per content on page.If we pass disabled="string" to items array (static arrangement).
How to make it dynamic.I have made Codepen for the same https://codepen.io/spider007/pen/eYmLBOG
<div id="app">
<v-app id="inspire">
<v-container>
<v-overflow-btn
class="my-2"
:items="items"
label="Overflow Btn - Dense"
dense
v-model="recordToAdd"
></v-overflow-btn>
<p v-if =" recordToAdd === '1' ">A's content is here -- i.e, Option A must be disabled in selection option</p>
</v-container>
</v-app>
</div>
JS
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: () => ({
recordToAdd : "",
items: [
{text:"A",value:"1"},
{text:"B",value:"2"},
{text:"C",value:"3"},
],
}),
})
You just need to add disabled: true in the data.
<template>
...
<v-overflow-btn
class="my-2"
:items="dropdown"
label="Test"
segmented
target="#dropdown-example"
/>
...
</template>
...
data() {
return {
disabled: false,
dropdown: [
// Always disabled
{ text: 'disabled option', callback: () => console.log('disabled'), disabled: true },
// disable depending on another variable
{ text: 'depending', callback: () => console.log('Hello'), disabled: this.disabled },
{ text: 'other Option', callback: () => console.log('Option') },
]
}
},
I have some text:
Hover me
on positioning the cursor over the text, I would like it to change to:
I'm being hovered
on moving the cursor off, the text should change back to:
Hover me
I can do this with CSS, but I can't figure out how to do it with Vue?
Something like this should work.. easiest if you use a computed property.
CodePen mirror: https://codepen.io/oze4/pen/XQapNP
new Vue({
el: "#app",
data: {
hover: false
},
computed: {
message() {
return this.hover === true ? "I'm being hovered" : "Hover me";
}
},
methods: {
handleHover(s){
this.hover = s;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<div id="app">
<p #mouseover="handleHover(true)" #mouseleave="handleHover(false)">
{{ message }}
</p>
</div>
You need to define the output you want and a boolean for the hover state, I've called it "hoover"
data: () => ({
hoover: false
}),
computed: {
tyext() {
if (this.hoover === false) {
return "Hover Me"
}
return "I'm being hovered"
}
}
Then in the template you can have event listeners to change the boolean.
<p #mouseenter="hoover = true" #mouseleave="hoover = false">{{ tyext }}</p>
You typically wouldn't have logic like this in your template and would instead call a function like this #mouseenter="changeHoover" and change the state but I showed this for brevity, which was kind of pointless as I keep banging on like this.
I have two button groups which are created from arrays by using v-for directive in Vue Js. When I clicked a button in first button group I need it to be in focused until the other button click in that button group, the focus state does not affected by any other click events, except the other button was clicked in that button group. The buttons are 1.All 2. True, 3. False, 4. None In the initial stage 1.All button has to be focused until the next button clicked in that button group.
Keep track of the active button by adding an active property to each array element:
new Vue({
el: '#app',
data: () => ({
buttons: [
{
text: 'All',
active: true
},
{
text: 'True',
active: false
},
{
text: 'False',
active: false
},
{
text: 'None',
active: false
}
]
}),
methods: {
activate (b) {
this.buttons.forEach(button => {
button.active = button.text === b.text
})
}
}
})
button {
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button v-for="(b,i) in buttons" :key="i" type="button" :style="{ 'background-color': b.active ? 'green' : 'grey' }" #click="activate(b)">{{ b.text }}</button>
</div>
I'm faced with an issue where my semantic drop down in my vue project won't activate when clicking on the arrow icon but works when I click on the rest of the element. The drop down also works when I set the dropdown to activate on hover, but just not on click. Solutions I've tried:
tested if the dynamic id are at fault
tested if the back ticks are confusing things
placed the values directly into the semantic drop down
Aside from the dropdown not activating, the code below works as intended and brings back the selected value to the parent component and can be displayed.
Dropdown.vue:
<template>
<div class="ui selection dropdown" :id="`drop_${dropDownId}`">
<input type="hidden" name="gender" v-model="selected">
<i class="dropdown icon"></i>
<div class="default text">Gender</div>
<div class="menu">
<div class="item" v-for="option in options" v-bind:data-value="option.value">
{{ option.text }}
</div>
</div>
</div>
</template>
<script>
export default {
data: function () {
return {
selected: {}
}
},
watch: {
selected: function (){
this.$emit("dropDownChanged", this.selected)
}
},
props: {
options: Array, //[{text, value}]
dropDownId: String
},
mounted () {
let vm = this;
$(`#drop_${vm.dropDownId}`).dropdown({
onChange: function (value, text, $selectedItem) {
vm.selected = value;
},
forceSelection: false,
selectOnKeydown: false,
showOnFocus: false,
on: "click"
});
}
}
</script>
The component usage:
<vue-drop-down :options="dropDownOptions" dropDownId="drop1" #dropDownChanged="dropDownSelectedValue = $event"></vue-drop-down>
The data in the parent:
dropDownOptions: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
],
dropDownSelectedValue: ""
Here is a fiddle of the above but simplified to use a flatter project. However the problem doesn't reproduce :(
https://jsfiddle.net/eywraw8t/210520/
I'm not sure what is causing your issue (as the examples on the Semantic Ui website look similar), but there is a workaround. For you arrow icon:
<i #click="toggleDropDownVisibility" class="dropdown icon"></i>
And then in the methods section of your Vue component:
methods: {
toggleDropDownVisibility () {
$(`#drop_${this.dropDownId}`)
.dropdown('toggle');
}
},
How I can capture key press and click on a tr element ?
I need to implement a table that can handle a single row selection, or multiple-row selection.
Right now, I tried to bind the key ctrl:
Vue.directive('on').keyCodes.ctrl = 17;
But, if i use #keyup.ctrl sure this dont works, because I need to check what key is pressed when the user click on a row.
The click event includes properties that indicate whether Control, Shift, Alt, or Meta keys were pressed during the click.
new Vue({
el: 'body',
data: {
controlled: false,
shifted: false,
meta: false,
alted: false
},
methods: {
clicked: function(event) {
console.debug(event);
this.controlled = event.ctrlKey;
this.shifted = event.shiftKey;
this.meta = event.metaKey;
this.alted = event.altKey;
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<button #click="clicked">Click me!</button>
<div v-if="controlled">Control was pressed</div>
<div v-if="shifted">Shift was pressed</div>
<div v-if="alted">Alt was pressed</div>
<div v-if="meta">Meta was pressed</div>