How to focus v-radio-group selected radio programmatically? - vue.js

How to focus v-radio-group selected radio programmatically ?
<v-radio-group v-model="radios" :mandatory="false" ref="RadioGroup">
<v-radio label="Radio 1" value="radio-1"></v-radio>
<v-radio label="Radio 2" value="radio-2"></v-radio>
</v-radio-group>
I tried with this.$refs.RadioGroup.focus() but nothing happens.
Thank you in advance

Two things
Vuetify adds divs and inputs to the DOM, so you need to dig in to the referenced element's children a little
you probably want to target one of the radio buttons since focusing the group seems to have no effect
This works, so might give you a idea to work with
<v-radio-group v-model="radios" :mandatory="false" ref="RadioGroup">
<v-radio label="Radio 1" value="radio-1" ref="Radio1"></v-radio>
<v-radio label="Radio 2" value="radio-2" ref="Radio2"></v-radio>
</v-radio-group>
this.$refs.Radio2[0].$el.children[0].children[0].focus()
Tested with this listener
created() {
document.addEventListener('focusin', event => {
console.log('focused', event.target)
})
},

Try using a custom directive. Add 'radio-focus' as a directive to the Vue Instance Add the v-radio-focus directive as an attribute to the v-radio element you wish to focus on. More about directives here.
'radio-focus': {
// directive definition
inserted: function (el) {
const $el: HTMLElement = el.children[0].children[0] as HTMLElement
$el.focus()
}
}

Related

VUE, VUETIFY |How to hide the selected radio option(selected in first group) from the second radio group if both radio groups have same radio options?

Click on this link to see the IMAGE, I want GSTN1 option hidden from the second radio group since it is getting selected in the first radio group
<template>
<v-radio-group v-model="firstIdRadio">
<v-radio
v-for="n in firstIdArr"
:key="n"
:label="`${n}`"
:value="n"
>
</v-radio>
</v-radio-group>
<v-radio-group v-model="secondIdRadio">
<v-radio
v-for="n in secondIdArr"
:key="n"
:label="`${n}`"
:value="n"
></v-radio>
</v-radio-group>
</template>
<script>
data() {
return {
firstIdRadio: "",
secondIdRadio: "",
firstIdArr: [GSTN1, GSTN2, GSTN3],
secondIdArr: [GSTN1, GSTN2, GSTN3],
}
}
</script>
I am using the Vuetify v-radio-group tag to populate radio buttons, I want the option hidden in the second radio group which got selected in the first radio group. If we take reference to the image provided above, GSTN1 should be hidden from the second radio group as it is selected in the first radio group.
Big thanks for the help.
Totally blank as to how to approach the challenge. The major issue is to know how to conditionally populate the radio options in the second radio group as per the selection done in the first radio group. Since I am using Vuetify, I am stuck on this part.
Array.filter
<v-radio-group v-model="secondIdRadio">
<v-radio v-for="n in secondIdArr.filter(i => i !== firstIdRadio)" :key="n" :label="`${n}`" :value="n"></v-radio>
</v-radio-group>

Vuetify radio button checked with value in edit form

I have a table with the option to edit each row. When I click on edit, a form pops up and shows the fields filled out with the data to edit. However, the selected radio button is not showing with the data selected before as a value, I only got the two radio buttons unchecked.
How can I make the correct radio button show the correct checked value?
This is my code
<v-card-subtitle class="py-1"> CONTRACT TYPE </v-card-subtitle>
<v-radio-group v-model="dataItem.type_contract" row class="ml-3">
<v-radio name="dataItem.type_contract" label="DIRECTO" value="direct" ></v-radio>
<v-radio name="dataItem.type_contract" label="CONTINGENCIA" value="contingency" ></v-radio>
</v-radio-group>
On the data I have this
dataItem: new ContractModel(),
Thanks for the help!
I found a solution that works for my case
I had to change the v-model on the v-radio-group and set it to a prop like this
<v-radio-group v-model="radioGroup" row class="ml-3">
<v-radio label="DIRECTO" value="direct"></v-radio>
<v-radio label="CONTINGENCIA" value="contingency"></v-radio>
</v-radio-group>
on Data
radioGroup: 'direct',
And because the edit modal is activated by a button I set the values like this
{
icon: "mdi-pencil",
description: "Editar",
action: (params) => {
this.modeEdit = true;
this.dialogAddEdit = true;
this.dataItem = JSON.parse(JSON.stringify(params[0]));
if (this.dataItem.type_contract === "DIRECTO") {
this.radioGroup = 'direct';
} else{
this.radioGroup = 'contingency';
}
},

Vuetify autocomplete - Clear auto complete cache-items list / suggestions

How do I clear the cache item list for auto complete ? I am using the cache-items flag. Once The user has the results for the current query and want to try a new list, I need the current items to be cleared.
I have "cache-items" on so the user can see all his selections before submitting.
<v-autocomplete v-if="showautocomplete" v-model="autocomplete_model"
:items="items" :loading="isLoading" autofocus
:search-input.sync="autocomplete_search" chips clearable hide-selected cache-items>
I don't know if you still need this but I think I've found an answer here: https://github.com/vuetifyjs/vuetify/issues/11365 from bka9.
You can add a ref on your v-autocomplete and change the cachedItems through the $refs. I'm not sure when you would have to trigger the method but that's all that's missing.
<template>
<v-autocomplete ref="autocomplete" cache-items ... />
</template>
<script>
methods: {
clearCachedItems() {
this.$refs.autocomplete.cachedItems = [];
},
},
</script>

displaying v-progress-circular in a conditional is making the below elements "jumpy"

The progress circle will be there until the codition sending is false. what happens is that after the dive goes away the below element will jump up to where the div dissapeared making it seem "jumpy". What would be the best way to display it in a stable manner.
<v-progress-circular
color="#64B5F6"
indeterminate
></v-progress-circular>
</div>
//rest of stuff
I suspect that you are using v-if="sending" on the parent <div>. When sending==false the v-if directive will completely remove it from the DOM. You can't even use v-show because although this will keep it in the DOM, it will have "display: none" css so it it still wont take up any space.
Instead, you need to set the visibility: hidden css to hide the spinner without collapsing the space it takes up.
You can do this in two ways:
<div :style="{visibility: sending ? 'visible' : 'hidden'}">
<v-progress-circular ...>
<div>
Or, create a re-usable custom directive:
Vue.directive('visible', function(el, binding) {
el.style.visibility = !!binding.value ? 'visible' : 'hidden';
});
Then use it like so:
<div v-visible="sending">
<v-progress-circular ...>
<div>

How to expand v-expansion-panel by default with vue/vuetify?

I'd like to have an expansion panel <v-expansion-panel> open by default on page load, but cannot figure it out. I know for Angular, you put [expanded]="true", but this doesn't work with Vue. I haven't had luck anywhere finding this answer. I'm not sure if javascript is needed or not, or if it can be done right within the <v-expansion-panel> tag.
I tried <v-expansion-panel [expanded]="true" and it did show the buttons that are in that section, but that's it.
<section>
<v-app>
<v-expansion-panel
v-model="search"
expand
>
.
.
.
</v-app>
</section>
Watch the PROPS section on the documentation:
https://vuetifyjs.com/en/components/expansion-panels#expansion-panel
The expand prop says: Leaves the expansion-panel open while selecting another.
This is not what you want.
You need to use value prop: Controls the opened/closed state of content in the expansion-panel. Corresponds to a zero-based index of the currently opened content. If expand prop is used then it is an array of Booleans where the index corresponds to the index of the content.
So, in your case:
<section>
<v-app>
<v-expansion-panel
v-model="search"
:value="0"
>
.
.
.
</v-app>
</section>
Where "0" is the index of the expansion panel expanded by default.
I made an example here:
https://codepen.io/anon/pen/pmqyOP?&editable=true&editors=101#anon-login
It works for me use:
<v-expansion-panels v-model="panel" multiple>
panel: [], // default
panel: [0, 1, 2, 3], // open items 1 to 4
If you want to return all the items by default or use a function to expand all:
this.panel = Array.from(Array(this.dataReturned.length).keys());
https://vuetifyjs.com/en/components/expansion-panels/#model