I'm in very beginning stage learning Vue.js and encountered problem I can't figure out right now. So I have 1 select field:
data: {
list: {
'Option 1': [ { size:'1',prize:'5' }, { size:'2',prize:'10' } ]
}
}
Then I populating first select field like this:
<select v-model="firstOptions" v-on:change="onChange">
<option v-for="(item, index) in list">{{ index }}</option>
</select>
At this point everything is fine, but how to populate another select field based on first select? I need to access size and price.
I'm think this should be done here:
methods: {
onChange: function() {
// get options for second select field
}
}
I'm assuming here in your data structure, list, that the value of each property defines the options you will use in the second select. The key here is the model for the first select drives the options for the second.
<option v-for="option in list[firstOption]" value="option.size">{{option.prize}}</option>
I'm not sure how exactly you want your text and values laid out in the first or second selects, but here is an example.
new Vue({
el:"#app",
data: {
firstOption: null,
secondOption: null,
list: {
'Option 1': [ { size:'1',prize:'5' }, { size:'2',prize:'10' } ],
'Option 2': [{size:'3', prize:'8'}]
}
}
})
and in your template
<div id="app">
<select v-model="firstOption">
<option v-for="(item, index) in list">{{ index }}</option>
</select>
<select v-model="secondOption" v-if="firstOption">
<option v-for="option in list[firstOption]" value="option.size">{{option.prize}}</option>
</select>
</div>
Example in codepen.
Related
I'm using html and laravel to build select box options in a foreach loop
This works and populates with ID as value and name as the option. What I want, and can't quite figure out here, is how to take my function when I call it and get the id and value as separate vue options for a post axios call I'm going to make.
So when I select the option and submit the form to call the function, how can I get the ID as one prop and the name as another?
<select>
#foreach($details as $detail)
<option value="{{$detail->id}}">{{$detail->name}}</option>
#endforeach
</select>
new Vue({
data: {
detailID: [''],
detailName: ['']
},
methods: {
let data = {
detailID: this.detailID,
detailName: this.detailName
};
}
})
Here is a code example just using vue.js
Template
<div id="app">
<select v-model="selectedDetailId">
<option v-for="detail in details" v-bind:value="detail.id">
{{ detail.name }}
</option>
</select>
</div>
Script
new Vue({
el: '#app',
data: {
selectedDetailId: null,
details: [
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },
{ id: 3, name: 'C' }
]
},
methods:{
post(){
//your selected Id is this.selectedDetailId
}
}
})
You can find more details and examples in the official Vue.js docs.
https://v2.vuejs.org/v2/guide/forms.html#Value-Bindings
Use v-model to bind the selection to your component data. SeeForm input bindings:
new Vue({
data: {
detailID: ""
},
// now you can access the id with this.detailID in your post request
})
<select v-model="detailID">
#foreach($details as $detail)
<option value="{{$detail->id}}">{{$detail->name}}</option>
#endforeach
</select>
And if you need both id and name, one work around could be:
new Vue({
data: {
detail: ""
},
// now you can access the id & name with this.detail.split(',') in your post request
})
<select v-model="detail">
#foreach($details as $detail)
<option value="{{$detail->id.','.$detail->name}}">{{$detail->name}}</option>
#endforeach
</select>
You would need to set a v-model to your select element and bind the entire object to each option element instead of just the id like you are doing in your example.
Here is a codepen with a simple example.
I'm having an array of objects. I wanted to change value to the object property by a select box.
HTML
<div id="app">
{{ message }}<br><br>
<select v-modal="items[0].val">
<option value="newjs">New js</option>
<option value="vannilajs">Vannila js</option>
</select>
<br>
ITEM 0 = {{items[0].val }}
<br>
ITEM 1 = {{items[1].val }}
</div>
JS
new Vue({
el:'#app',
data: {
message: 'testing',
items: [{val:'VUE'},{val: 'REACT'}] //intial two items
}
});
When select box changes I wanted to change the value of ITEM 0 to Selected Value.
Just for testing purpose right now I'm trying to access 0th object from the array. This select box will be in v-for loop for multiple select box.
FIDDLE
You can't assign variables to objects and arrays that easily in Vue.js
<div id="app">
<select #input="change">
<option value="newjs">New js</option>
<option value="vannilajs">Vannila js</option>
</select>
</div>
<script>
new Vue({
el:'#app',
data:{
items:[{val:'VUE'},{val:'REACT'}]
},
methods: {
change(element) {
this.$set(this.items[0], 'val', element);
}
}
});
</script>
Vue can only migically refresh values changed by reference (message = 'hey'), or by object reference (object.message = 'hey') but only if that reference was in data() in create time.
new Vue({
data: {
object: {first:0},
array: [1,2,3]
},
methods: {
change() {
object.first = 2; // works
object.second = 3; // doesn't work
array[0] = 2; // works for 0,1,2
array[4] = 3; // doesn't work
}
}
For the object.second and array[3] you need to use $set().
Ok i misread your question. You do have a spelling mistake but you also need an intermediary variable to hold the value of the drop down
<div id="app">
{{ message }}<br><br>
<select v-model="selectedValue">
<option value="newjs">New js</option>
<option value="vannilajs">Vannila js</option>
</select>
<br>
ITEM 0 = {{selectedValue }}
<br>
ITEM 1 = {{items[1].val }}
</div>
new Vue({
el:'#app',
data: {
message: 'testing',
selectedValue: 'VUE',
items: [{val:'VUE'},{val: 'REACT'}] //intial two items
}
});
Assume I have this :
<select class="form-control" name="gateway">
<option data-type="typeA" value="1">Test String</option>
<option data-type="typeB" value="2">Test String</option>
<option data-type="typeC" value="3">Test String</option>
</select>
<span>#{{ typeMapper(data-type) }}</span>
Here is my Vue.js script :
const app = new Vue({
el: '#vue-app',
data: {},
methods: {
typeMapper: function (type) {
var array = {
'typeA':'You selected Type A',
'typeB':'You selected Type B',
'typeC':'You selected Type C',
};
return array[type];
}
}
});
Now how can I get data-type values in Vue.js ?
I want to pass selected option data-type value to Vue.js typeMapper method and show the result in the span tag.
I don't know how to pass data-type value to Vue.js !
P.S:
I'm using Vue.js 2
Your data isn't a true javascript array so I used the Object.keys function to grab the key names. Here's a quick way of doing what you're looking for:
<div id="app">
<select class="form-control" name="gateway">
<option v-for="(key,index) in Object.keys(array)" :value="index+1" :data-type="key">
{{ array[key]}}
</option>
</select>
</div>
And the Vue code:
new Vue({
el: '#app',
data: {
array: {
'typeA': 'You selected Type A',
'typeB': 'You selected Type B',
'typeC': 'You selected Type C',
}
}
});
Here's a working fiddle.
I have no clue as to why you'd want to gather data sets from a given html5 structure. Since you'd usually want to get that trough ajax calls or set the select options trough some other component or whatever.
If you however want to get the elements given from a html structure you'd do something like this.
codepen
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>
<div id="vue-app">
<select class="form-control" v-on:change="typeMapper">
<option data-type="" value="">Select Type</option>
<option data-type="typeA" value="1">typeA</option>
<option data-type="typeB" value="2">typeB</option>
<option data-type="typeC" value="3">typeC</option>
</select>
<span>{{ gateway }}</span>
</div>
JS
const app = new Vue({
el: '#vue-app',
data: {
gateway: "",
},
methods: {
typeMapper: function (event) {
this.gateway = event.target[event.target.value].dataset.type;
}
}
});
This is by far from optimal or a cross browser variant. If I were you I'd scrap this approach.
I am trying to get the text of selected index in dropdown using knockout.js,
following is my HTML
<select name="" id="management" class="form-control" data-bind="value: ManagementCompanies,optionText:ManagementCompaniestxt">
<option value="0">---Select---</option>
<option value="1">abcd</option>
<option value="2">efgh</option>
</select>
following is my Model binding:
var FilterViewModel = {
ManagementCompanies: ko.observable(''),
ManagementCompaniestxt:ko.observable('')
}
FilterViewModel.ManagementCompanies.subscribe(function (newValue) {
alert(FilterViewModel.ManagementCompaniestxt());
});
ko.applyBindings(FilterViewModel, window.document.getElementById("SelectFilters"));
i have tried to bind using Text as well but didn't work.
how can i get selected text abcd in subscribe event?
thanks
It's a bit weird that you're trying to get data from your view to your viewmodel. Usually, your view is a representation of your viewmodel. It's better to have the data needed to render your <select> in your model, and use knockout's options data-bind to render it.
Here's how you can do this:
var FilterViewModel = {
ManagementCompanies: ko.observable(''),
ManagementCompaniestxt: ko.observable(''),
options: [
{ text: "---Select---", value: 0 },
{ text: "abcd", value: 1 },
{ text: "efgh", value: 2 }]
}
FilterViewModel.ManagementCompanies.subscribe(function(newValue) {
console.log(newValue.text);
});
ko.applyBindings(FilterViewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="value: ManagementCompanies,
options:options,
optionsText: 'text'">
</select>
I would like to populate the <select name="selectmodel"> <option> from a nested array of objects based on the selection of the <select name="selectmake"> <option> element.
Here is the multi-dimensional array:
muscleCars = [
{
id: 1, name: "Chevrolet", models: [
{ model: "Camaro" },
{ model: "Corvette" }
]
},
{
id: 2, name: "Dodge", models: [
{ model: "Charger" },
{ model: "Challenger" },
{ model: "Viper" }
]
},
{
id: 3, name: "Ford", models: [
{ model: "GT" },
{ model: "Mustang" }
]
}
];
This is the HTML
//select for Make:
<select name="selectmake" [(ngModel)]="makeListFilter">
<option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar.name">{{muscleCar.name}}</option>
</select>
//select for Model:
<select name="selectmodel" [(ngModel)]="modelListFilter">
<option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar.models">{{muscleCar.models}}</option>
</select>
So, basically when you select Chevrolet for example, I would like to have the second element populated with Camaro and Corvette.
Currently, the second select element is populated with an array [object Object] for each make, but can't figure out how to dig this deeper.
Here is a plunk:
https://embed.plnkr.co/0eEIJg5uzL6KsI70wWsC/
Any help would be appreciated.
This is how your HTML should look like:
<select name="selectmake" [(ngModel)]="makeListFilter">
<option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar">{{muscleCar.name}}</option>
</select>
<select name="selectmodel" [(ngModel)]="modelListFilter">
<option *ngFor="let carModel of makeListFilter?.models" [ngValue]="carModel.model">{{carModel.model}}</option>
</select>
So, what's happening here is that selected value of selectmake dropdown is binded to makeListFilter and second dropdown selectmodel is populated based on selected value of first dropdown. You will notice I binded the whole Object that is selected in first dropdown using ngValue directive so it can be used to populate second dropdown. Another interesting thing you'll notice is Elvis operator (?) I used in second dropdown - it is used to tell second dropdown to populate itself only after value is selected in first dropdown, this is necessary to avoid getting error for iterating through an undefined value. If you don't want to use Elvis operator, you can use *ngIf directive to prevent getting mentioned error, but that means that second dropdown will appear only after you select something in the first dropdown:
<select *ngIf="makeListFilter" name="selectmodel" [(ngModel)]="modelListFilter">
<option *ngFor="let carModel of makeListFilter.models" [ngValue]="carModel.model">{{carModel.model}}</option>
</select>