Here is the code I write to get value form API, Is it possible to send the two parameter like i send in the following example.
async getMainAssignmentsList(schoolId,lessonId) {
var getLearningStrategiesUrl =
this.apiBasedUrl + 'Assignment/' + schoolId +'/'+ lessonId + '/GetMainAssignment'
await this.axios
.get(getLearningStrategiesUrl)
.then(response => {
this.mainAssignmentList = response.data
})
.catch(function (error) {
console.log(error)
})
}
My requirement is that I need to select First 'form.schoolId' and after that 'form.lessonId' and on the basis of combined want to show data but, here is am unable to send parameter in the API.
<div class="col-sm-12">
<b-form-select v-model="form.schoolId" class="mt-2" placeholder="Please select an option" #input="getMainAssignmentsList(form.schoolId)">
<option :value="null">Select school</option>
<option v-for="School in allSchoolsList" :value="School.id" :key="School.id">{{School.schoolName}}</option>
</b-form-select>
</div>
<div class="col-sm-12" >
<b-form-select v-model="form.lessonId" class="mt-2" #input="getMainAssignmentsList(form.lessonId)">
<option :value="null">Please select Lesson</option>
<option v-for="Lesson in allLessons" v-bind:value="Lesson.id" :key="Lesson.id">{{Lesson.name}}</option>
</b-form-select>
</div>
<!--- Here want to show value in drop down --->
<div class="col-sm-12" >
<b-form-select v-model="parentAssignmentId" class="mt-2" >
<option :value="null">Please select Main Assignment</option>
<option v-for="MainAssignment in mainAssignmentList" v-bind:value="MainAssignment.id" :key="MainAssignment.id">{{AssignmentTypes.name}}</option>
</b-form-select>
</div>
Currently my URL is showing following value:
Request URL:http://localhost:51711/api/Assignment/undefined/undefined/GetMainAssignment
You can always try something like this and let bootstrap-vue take care of most of the heavy lifting.
<template>
<b-container>
<b-row>
<b-col sm="12">
<b-form-select
v-model="selectedSchoolId"
class="mt-2"
placeholder="Please select an option"
:options="allSchoolsList"
></b-form-select>
</b-col>
<b-col sm="12">
<b-form-select v-model="selectedLessonId" class="mt-2" :options="allLessons"></b-form-select>
</b-col>
<b-col sm="12">
<b-form-select
v-model="parentAssignmentId"
class="mt-2"
placeholder="Please select Main Assignment"
:options="mainAssignmentList"
></b-form-select>
</b-col>
<b-col>
<b-btn #click="getMainAssignmentsList">Submit</b-btn>
</b-col>
</b-row>
</b-container>
</template>
<script>
export default {
data() {
return {
selectedLessonId: null,
selectedSchoolId: null,
parentAssignmentId: null,
allSchoolsList: [
{ value: null, text: "Please select an option" },
{ value: "schoola", text: "School A" },
{ value: "schoolb", text: "School B" },
{ value: "schoolc", text: "School C" },
{ value: "schoold", text: "School D" }
],
allLessons: [
{ value: null, text: "Please select an option" },
{ value: "lessona", text: "Lesson A" },
{ value: "lessonb", text: "Lesson B" },
{ value: "lessonc", text: "Lesson C" },
{ value: "lessond", text: "Lesson D" }
],
mainAssignmentList: [
{ value: null, text: "Please select an option" },
{ value: "assignmenta", text: "Assignment A" },
{ value: "assignmentb", text: "Assignment B" },
{ value: "assignmentc", text: "Assignment C" },
{ value: "assignmentd", text: "Assignment D" }
]
};
},
methods: {
getMainAssignmentsList() {
let apiArgs = {
lesson: this.selectedLessonId,
school: this.selectedSchoolId
};
console.log(apiArgs);
//Call API using apiArgs
}
}
};
</script>
I removed the options tags and changes some divs to b-col tags. If you want to manual make your own options tags you will need to use the correct tag like this example taken straight from the docs:
<b-form-select-option value="C">Option C</b-form-select-option>
Related
There is a component in which two select fields (country, city) are created on click.The fields are dependent, when the country is selected, the values in the second select (city) are changed.The problem is that when we change one select (country) all other select-s (cities) change.
<template>
<b-container class="bv-example-row">
<b-row v-for="(station, counter) in stations" v-bind:key="counter">
<b-col cols="6">
<label>Country</label>
<select class='form-control' name="country_dest_id" v-model='station.country' #change='getStates($event)'>
<option value='0' >Select Country</option>
<option v-for='data in countries' :value='data.id'>{{ data.name }}</option>
</select>
</b-col>
<b-col cols="6">
<label >City</label>
<select class='form-control' v-model='station.state'>
<option value='0' >Select State</option>
<option v-for='data in states' :value='data.id'>{{ data.name }}</option>
</select>
</b-col>
<b-col cols="1">
<button class="btn btn-danger remove" #click="deleteStation(counter)"><i class="fa fa-times" aria-hidden="true"></i> Remove</button>
</b-col>
</b-row>
<b-row class="justify-content-md-center">
<b-col cols="3" md="3">
<button class="btn btn-success" type="button" #click="addStation">Add</button>
</b-col>
</b-row>
</b-container>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
},
data(){
return {
stations:[
{
country: '',
state:'',
}
],
countries: [],
states:[]
}
},
methods:{
addStation(){
this.stations.push({
country:'',
state: ''
})
},
deleteStation(counter){
this.stations.splice(counter,1);
},
getCountries: function(){
axios.get('/getCountries')
.then(function (response) {
this.countries = response.data;
}.bind(this));
},
getStates: function(event) {
console.log(event.target.value);
axios.get('/getStates',{
params: {
country_id: event.target.value
}
}).then(function(response){
this.states = response.data;
}.bind(this));
}
},
created: function(){
this.getCountries()
}
}
</script>
How to make field groups unique values?
The problem is that the getStates function works always on the same array in data(). #change overwrites that value & every select that is bound to it receives the new set of values.
You'd be better off if you built up an object of states, something like (the sample probably is not working, just giving an idea):
data() {
return {
states: {
countryId1: ["cid1state1", "cid1state2"],
countryId2: ["cid2state1", "cid2state2"],
},
}
},
methods: {
getStates: function(event) {
if (!(event.target.value in this.states)) {
axios.get('/getStates',{
params: {
country_id: event.target.value
}
}).then(function(response){
this.states[event.target.value] = response.data;
}.bind(this));
}
}
},
Try to select from datalist while a data property is updated on a interval.
What is wrong in my code?
http://jsfiddle.net/startflorin/gr6b1h7j/18
Mounted intervals:
setInterval((ctx) => { ctx.notification = "111"; }, 500, this);
setInterval((ctx) => { ctx.notification = "222"; }, 500, this);
Data:
data: {
notification: null,
demoList:[
{
id: 1,
name: "option 1",
},
{
id: 2,
name: "option 2",
},
{
id: 3,
name: "option 3",
},
],
},
My HTML code:
<div>
{{ notification }}
</div>
<input list='demoList' v-on:change="selectSymbolList(target.value)">
<datalist id="demoList">
<option v-for="item in this.demoList" v-bind:value="item.name" v-bind:key="item.id">{{ item.name }}</option>
</datalist>
To cache the rendering of <input> and <datalist> (to isolate them from unrelated changes in the component's template), put them into a component:
Vue.component('demo-list', {
props: ['items'],
template: `<div>
<input list='demoList'>
<datalist id="demoList">
<option v-for="item in items" v-bind:value="item.name" v-bind:key="item.id">{{ item.name }}</option>
</datalist>
</div>`
})
Note this example requires the runtime compiler to compile the template string. Otherwise, render functions would be required instead of template.
Then use the component in your app's template:
<div id="app">
<div>{{ notification }}</div>
<demo-list :items="demoList"></demo-list>
</div>
demo
Can anyone help with this problem? How can I emit two properties from child component to parent on select input change? I can submit the value, see below, but would like to emit the value and the name property of segmentLocations object. This is the child component:
<template>
<div class="container">
<div>
<select v-model="selectedSegmentValue" v-on:change="$emit('selectLocation', $event.target.value)">
<option selected value="">Choose your location...</option>
<option v-for="segmentLocation in segmentLocations"
:value="segmentLocation.value"
:key="segmentLocation.value">
{{ segmentLocation.name }}>
</option>
</select>
</div>
</div>
</template>
<script>
export default {
data() {
return {
segmentLocations: [
{ value: "Residential", name: 'Residential building' },
{ value: "Workplace", name: 'Workplace' },
{ value: "Hospitality", name: 'Hospitality or Retail' },
{ value: "Real Estate", name: 'Real Estate' },
{ value: "Commercial Parking", name: 'Commercial Parking' },
{ value: "Fleets", name: 'Fleets' },
{ value: "Cities & Governments", name: 'Cities & Governments' },
{ value: "Corridor", name: 'Highway, Corridor or Petrol Station' }
],
}
}
};
</script>
And this is the parent:
<template>
<Segments
v-on:selectLocation="quote.selectedSegmentValue = $event"
:selectedValue="quote.selectedSegmentValue">
</Segments>
</template>
<script>
export default {
data() {
return {
quote: {
selectedSegmentValue: "",
selectedSegmentName: ""
},
};
},
</script>
I think the existing answers and mine share a similar technique, but I created a couple of simplified sample components based on your components.
Child component:
<template>
<div class="emit-two-properties">
<div class="form-group">
<label for="segment-location">Segment Location</label>
<select class="form-control" id="segment-location"
v-model="segmentLocation" #change="selectSegmentLocation">
<option v-for="(segLoc, index) in segmentLocations" :key="index"
:value="segLoc">{{ segLoc.name }}</option>
</select>
</div>
</div>
</template>
<script>
export default {
data() {
return {
segmentLocation: {},
segmentLocations: [
{ value: "Residential", name: 'Residential building' },
{ value: "Workplace", name: 'Workplace' },
{ value: "Hospitality", name: 'Hospitality or Retail' },
{ value: "Real Estate", name: 'Real Estate' },
{ value: "Commercial Parking", name: 'Commercial Parking' },
{ value: "Fleets", name: 'Fleets' },
{ value: "Cities & Governments", name: 'Cities & Governments' },
{ value: "Corridor", name: 'Highway, Corridor or Petrol Station' }
],
}
},
methods: {
selectSegmentLocation() {
this.$emit('select-segment-location-event', this.segmentLocation);
}
}
}
</script>
Parent component:
<template>
<div class="parent">
<h4>Parent.vue</h4>
<div class="row">
<div class="col-md-6">
<form #submit.prevent="submitForm">
<emit-two-properties #select-segment-location-event="updateSegmentLocation" />
<button class="btn btn-secondary">Submit</button>
</form>
<p><span>Selected Segment Location Value:</span>{{ segmentLocation.value }}</p>
<p><span>Selected Segment Location Name:</span>{{ segmentLocation.name }}</p>
</div>
</div>
</div>
</template>
<script>
import EmitTwoProperties from './EmitTwoProperties'
export default {
components: {
EmitTwoProperties
},
data() {
return {
segmentLocation: {}
}
},
methods: {
updateSegmentLocation(segLoc) {
this.segmentLocation = segLoc;
}
}
}
</script>
you can create a method to get name and value from event.target (remove value from the end of child emit):
changeSelectedSegment(selected){
this.selectedSegmentName = selected.name
this.selectedSegmentValue = selected.value
}
in the parent change v-on:selectLocation to v-on:selectLocation="changeSelectedSegment($event)"
you can define a method like this (this method emit an object with name and value properties to parent
)
methods: {
selectLocation(event){
if(event.target.value !== ''){
const item = this.segmentLocations.find( item => item.value === event.target.value)
this.$emit('selectLocation', {
name: item.name,
value: event.target.value
})
}
}
},
and change this line :
<select v-model="selectedSegmentValue" v-on:change="$emit('selectLocation', $event.target.value)">
to this:
<select v-model="selectedSegmentValue" v-on:change="selectLocation">
I've been following VueJS official documentation on passing data to child components with props; though I'm not working with a string template. I'm aware about what happens when your prop is camel case; you should write it as kebab case.
Nevertheless, this is not the case since it's all lowercase and won't work.
I'm using nuxt and I've separated my work into files, which are:
<template>
<div class="row">
<input type="text" name="" id="" placeholder="Write your question" v-model="text">
<select v-model="selectedField">
<option v-for="option in options" :key="option.id" :value="option.value">
{{ option.text }}
</option>
</select>
<button class="btn btn-sm btn-primary" #click="$emit('add-field')"
v-bind:class="{ disabled: ($parent.count <= 1 && $parent.count == identifier) }">
>{{identifier}}</button>
<button class="btn btn-sm btn-danger" #click="$emit('delete-field')">-</button>
</div>
Now for its JS file:
export default {
data () {
return {
options: [
{
id: 1,
value: 1,
text: "Radio"
},
{
id: 2,
value: 2,
text: "Rate"
},
{
id: 3,
value: 3,
text: "Text"
}
],
props: ['identifier'],
selectedField: 1,
text: "",
}
},
}
Now, for my parent component:
<template>
<div class="offset-md-3" id="qz">
<form-maker
v-for="item in questions" :key="item._id"
v-on:add-field="addField()"
v-on:delete-field="deleteField(item._id)"
v-bind:identifier="item._id" <<--What I want to set
ref="child"
></form-maker>
<button #click="saveForm" class="btn btn-large btn-success">SAVE</button>
</div>
</template>
Finally:
var vm = null;
export default {
layout: 'admin',
components: {
formMaker
},
data() {
return {
count: 1,
questions: [{
_id: 1//static
}]
}
},
}
What I'm trying to do is, to use the prop for some validations, nevertheless it throws the next error:
Property or method "identifier" is not defined on the instance but
referenced during render. Make sure that this property is reactive,
either in the data option, or for class-based components, by
initializing the property.
Thank you.
Here is where you go wrong. Props should not be in data(). See the code snippet below
<script>
export default {
props: ['identifier'],
data() {
return {
options: [
{
id: 1,
value: 1,
text: "Radio"
},
{
id: 2,
value: 2,
text: "Rate"
},
{
id: 3,
value: 3,
text: "Text"
}
],
selectedField: 1,
text: "",
}
}
}
</script>
I'm trying to create a simple vue that binds the selected item from a select/dropdown to a property in the vm.
I haven't been able to find a clear and simple example of how this is down when using an options collection that is also in the view model.
<template>
<div>
<h1>Select box</h1>
<b-dropdown id="ddCommodity"
name="ddCommodity"
v-model="ddTestVm.ddTestSelectedOption"
text="Select Item"
variant="primary"
class="m-md-2" v-on:change="changeItem">
<b-dropdown-item disabled value="0">Select an Item</b-dropdown-item>
<b-dropdown-item v-for="option in ddTestVm.options":selected="option.value == 'LME/ST_TNI_ALL'":value="option.value">{{option.text}}</b-dropdown-item>
</b-dropdown> <span>Selected: {{ ddTestVm.ddTestSelectedOption }}</span>
</div>
</template>
<script>
export default {
components: {
},
data() {
return {
someOtherProperty: null,
ddTestVm: {
originalValue: [],
ddTestSelectedOption: "Value1",
disabled: false,
readonly: false,
visible: true,
color: "",
options: [
{
"value": "Value1",
"text": "Value1Text"
},
{
"value": "Value2",
"text": "Value2Text"
},
{
"value": "Value3",
"text": "Value3Text"
}
]
}
}
},
methods: {
changeItem: async function () {
//grab some remote data
try {
let response = await this.$http.get('https://www.example.com/api/' + this.ddTestVm.ddTestSelectedOption + '.json');
console.log(response.data);
this.someOtherProperty = response.data;
} catch (error) {
console.log(error)
}
}
},
watch: {
},
async created() {
}
}
</script>
<style>
</style>
Regardless of what i've tried i cannot get the selected value in the dropdown to change the ddTestSelectedOption property of the vm.
Could anyone assist on this issue?
Thanks.
b-dropdown in bootstrap-vue does not support v-model. As the documentation states:
Dropdowns are toggleable, contextual overlays for displaying lists of
links and actions in a dropdown menu format.
In other words, b-dropdown is essentially a UI component for displaying a menu or similar set of options.
I expect what you want is b-form-select.
That said, you could add a click handler to the options that sets the value.
<b-dropdown-item v-for="option in ddTestVm.options"
:key="option.value"
:value="option.value"
#click="ddTestVm.ddTestSelectedOption = option.value">
Here is a working example.
I thing you need b-form-select
<template>
<div>
<b-form-select v-model="selected" :options="options"></b-form-select>
<b-form-select v-model="selected" :options="options" size="sm" class="mt-3"></b-form-select>
<div class="mt-3">Selected: <strong>{{ selected }}</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: null,
options: [
{ value: null, text: 'Please select an option' },
{ value: 'a', text: 'This is First option' },
{ value: 'b', text: 'Selected Option' },
{ value: { C: '3PO' }, text: 'This is an option with object value' },
{ value: 'd', text: 'This one is disabled', disabled: true }
]
}
}
}
</script>
Only b-form-select can achieve the selected value behaviour.
Non-Selected Value Preview:
Selected Value Preview:
Sample Code:
<template>
<div>
<b-form-select v-model="selected" :options="options"></b-form-select>
</div>
</template>
<script>
export default {
data() {
return {
selected: null,
options: [
{ value: 1, text: 'Please select an option' },
{ value: 2, text: 'This is First option' },
{ value: 3, text: 'Selected Option' }
]
}
}
}
</script>
Wanted to leave a comment, but code example looks pale there :)
Yes, b-dropdown does not properly support Vue model, but it doesn't have to.
For those still interested in exactly dropdown (f.e. because it looks fancier), consider:
<b-dropdown :text="$i18n.locale" >
<b-dropdown-item v-for="(lang, i) in $i18n.availableLocales" :key="`Lang${i}`" :value="lang" v-on:click="$i18n.locale = lang;" >{{lang}}</b-dropdown-item>
</b-dropdown>
Slecifically v-on:click, which can handle the model value change for you.