I have an array of colors. I want to use them to choose colors for the text. I use the radio button because I only want to select one. I custom the default radio by using a label. The problem I'm having is that the radio only selects one but my label can select multiple. How can I fix it? It's hard to understand so this is a picture about my problem
<div class="d-flex align-items-center flex-wrap mb-7">
<span class="w--26 flex-fixed pe-3 mb-5">{{
$t('setting.setting_theme.title_background')
}}</span>
<div v-for="color in list_colors" :key="color.id" class="me-5 mb-5">
<input
:v-model="color.checked"
type="radio"
class=""
:id="color.id"
name="selectTitleBackground"
#click="color.checked = !color.checked"
/>
<label
class="w--8 h--8 flex-center cursor-pointer border"
:for="color.id"
:style="{ backgroundColor: color.value }"
><i
v-if="color.checked"
:class="{ 'text-dark': color.value == '#fff' }"
class="fal fa-check text-light"
></i
></label>
</div>
</div>
enter image description here
The model that is used on the inputs is color.checked. This color object is coming from v-for="color in list_colors". So checking the input is changing each individual color object. You need to use a data property to store the checked color:
data() {
return {
selectedColorId: 1
}
}
<input
:v-model="selectedColorId"
type="radio"
:id="color.id"
:value="color.id"
name="selectTitleBackground"
/>
You also dont need the click event on the input as this should be handled by the input binding. https://v2.vuejs.org/v2/guide/forms.html#Radio
And the adding of the tick icon:
<i v-if="color.id === selectedColorId"
:class="{ 'text-dark': color.value == '#fff' }"
class="fal fa-check text-light"
></i>
Related
I have a custom component in Vue that is a multiple selection. Due to the way this was created, I am having difficulties validating that at least one option has been selected. As currently, the component uses a button, that when toggled opens a popup/dropdown for the user to select some options. But I cannot figure out how to validate this. I have tried replacing this button with other components such as <input> and <select> but nothing seems to work.
This is where I am currently at:
<div
:id="'multiple-selection' + customFieldId"
class="dropdown multiple-selection"
>
<input
type="button"
class="form-control dropdown-toggle"
data-type="Multiple Selection"
:id="customFieldId"
:value="localValues"
:required="required"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
/>
<div
:id="'multiple-selection-options' + customFieldId"
:name="'multiple-selection-options' + customFieldId"
class="dropdown-menu multiple-selection-options-wrapper"
style="/*width: calc(100% - 40px)*/"
>
<a
v-for="fOption in options.split(',').map(option => option.trim())"
class="dropdown-item cursor-pointer three-dots-text"
#click="selectOption(fOption)"
#click.stop
>
<input
class="cursor-pointer"
type="checkbox"
:id="'checkbox-' + fOption + customFieldId"
value="fOption"
:checked="localValues.includes(fOption)"
/>
<span :for="'checkbox-' + fOption + customFieldId">
{{ fOption }}
</span>
</a>
</div>
</div>
My current issues with this implementation are that I am unsure how to display the options in the input when selected, or a None selected placeholder when none are, and then of course, when they click save, the browser popup should tell them that it is a required field.
I am trying to output the value of individual checkboxes and also add a class to the label when the checkbox is checked. I can do one of the other but not both together. If I add :value="attendance" the output works as individual instances but the adding of the class doesn't work and if I add value="attendance" then it treats the 2 checkboxes as one value.
Can someone help please?
<div class="container">
<div class="row">
<div class="col-sm">
<label
class="btn btn-outline-primary label-in-filter"
:class="{
showattendances:
showattendancesisChecked('attendance'),
}"
v-for="attendance in uniqueattendances"
:key="attendance"
>
<!-- <input
value="attendance"
id="attendance"
name="showattendances"
class="ck-in-filter"
type="checkbox"
v-model="attendances"
/> -->
<input
id="attendance"
name="showattendances"
class="ck-in-filter"
type="checkbox"
:value="attendance"
v-model="attendances"
/>
{{ attendance }}
</label>
</div>
<p v-for="attendance in attendances" :key="attendance">
{{ attendance }}
</p>
</div>
</div>
methods: {
showattendancesisChecked(value) {
return this.attendances.includes(value);
},}
To check which input is enable, in our DOM structure(Knockout JS) there is no specific property, which says the button is enable..
Its handled by one of the CSS property, of ::before class :
CSS: {content: "\f013";}
HTML:
<div data-bind="foreach: XXXXXX ">
<div class=".col-xs-3 col-sm-3 col-md-3 -setting-radio" data-bind="attr: " >
<label class="radio-position radio-inline " data-view="widgets/input/radio/view" data-active-view="true" style="">
<input class="-widgets-input-radio-check" id="radio" type="radio" data-bind="Enable: enable, value: value, checked: checked" value="1" name="FS">
<span class="-radio-label"></span>
<span class="-widgets-input-radio-text" data-bind="html: data">Global</span>
</label>
</div>
<div class=".col-xs-3 col-sm-3 col-md-3 -setting-radio" data-bind="attr: " >
<label class="radio-position radio-inline" data-view="widgets/input/radio/view" data-active-view="true" style="">
<input class="-widgets-input-radio-check" id="radio" type="radio" data-bind="Enable: enable, value: value, checked: checked" value="2" name="FS">
<span class="-radio-label"> </span>
<span class="-widgets-input-radio-text" data-bind="html: data">Capital</span>
</label>
</div>
</div>
How can we achieve it ?
There is a special method for this in selenium, named value_of_css_property The name differs on the language you use, but the idea is the same.
As I understand from your question you can use it like as in the below example:
assert driver.find_element_by_css_selector('css_locator').value_of_css_property("content") == "\f013"
HTML is not enough, you need to check which CSS properties are changed as well.
You can use it to for waits and asserts.
I use computed tab for this:
this is my code :
<b-form #submit="onSubmit" #reset="onReset" >
<b-card v-for="(item, index) in items" :key="item.name">
<input type="file"
style="visibility:hidden;"
id="file"
ref="file"
#click="printIndex(index)" #change.once="handleFileUpload($event.target,$event.target.files)"
/>
{{index}}
</b-card>
</b-form>
my model :
items: [{
name: "soldat",
margin: 3,
labour: 2,
finition: 1,
demandMax: 40,
demandMin: 0
},
{
name: "train",
margin: 2,
labour: 1,
finition: 1,
demandMax: 800,
demandMin: 0
}
]
printIndex() function only prints 0, while it should print 1 when I click on the second displayed input object .
The {{index}} displays correctly 1 on the second object (train) .
It is like the INPUT can't access the INDEX variable, because it is a v-for .
Do you experiment the same behavior ?
With a modified vue code like this :
<b-card v-for="(item, index) in items" :key="item.index" #click="printIndex(index)">
<input type="file"
style="visibility:hidden;"
id="file"
ref="file"
#change.once="handleFileUpload($event.target,$event.target.files)"
/>
</b-card>
printIndex() is triggered 2 times when clicking on INPUT , and also gives 0 the second time, while it should only display the index 1 when clicking on my second object, just like in angularJs.
I have no solution, I am bloqued. If anyone has a solution, i would really appreciate it .
This is the printIndex method :
printIndex(index){
console.log(index)
}
EDIT : Excuse me, Some code was missing, added the #change . I suspect that there is a conflict between both #change and #click.
EDIT 2 :
<input v-model="index" #click="printIndex(index)"></input>
is working perfectly inside the v-for loop, the error seems to be linked to type="file" : this is the error :
**File inputs are read only. Use a v-on:change listener instead.**
EDIT 3 resolved, thank you
I have placed the printIndex() function above the hidden button , in the label, like this :
<label for="file" class="btn btn-secondary btn-block" #click="printIndex(index)">
<i class="fas fa-user-astronaut"></i>
<span class="d-none d-sm-block">Change picture</span>
</label>
<input
type="file"
style="visibility:hidden;"
id="file"
ref="file"
#change="handleFileUpload($event.target,$event.target.files)"
accept="image/*"
/>
The main problem was that an input type ="file" is readonly, so you can't access his index .
Your model objects doesn't have any index property so :key="item.index" is just wrong. Use :key="index" instead...
EDIT 3 resolved, thank you
I have placed the printIndex() function above the hidden button , in the label, like this :
<label for="file" class="btn btn-secondary btn-block" #click="printIndex(index)">
<i class="fas fa-user-astronaut"></i>
<span class="d-none d-sm-block">Change picture</span>
</label>
<input
type="file"
style="visibility:hidden;"
id="file"
ref="file"
#change="handleFileUpload($event.target,$event.target.files)"
accept="image/*"
/>
The main problem was that an input type ="file" is readonly, so you can't access his index .
How to show different component on radio button select.
<input type="radio" name="book" value="One" checked="checked">
<input type="radio" name="book" value="Round">
<div> // this should show show default, One is selected
<p>Value One</p>
</div>
<div> // this should show show on radio change to Round selected
<p>Value Round</p>
</div>
How about something like this?
new Vue({
el: '#app',
data: {
x: 'one',
},
});
<script src="https://rawgit.com/vuejs/vue/dev/dist/vue.js"></script>
<div id="app">
<input type="radio" v-model="x" value="one">
<input type="radio" v-model="x" value="two">
<div v-show="x === 'one'">One</div>
<div v-show="x === 'two'">Two</div>
</div>
i have done it with javascript .onclick it calls each function which hides & show element vice versa
function rdone(){
document.getElementById('one').style.display ='block';
document.getElementById('round').style.display ='none';
}
function rdround(){
document.getElementById('round').style.display = 'block';
document.getElementById('one').style.display ='none';
}
#round{
display:none;
}
<input type="radio" name="book" value="One" checked="checked" onclick="rdone();">
<input type="radio" onclick="rdround();" name="book" value="Round">
<div id=one> // this should show show default, One is selected
<p>Value One</p>
</div>
<div id=round> // this should show show on radio change to Round selected
<p>Value Round</p>
</div>