I'm trying to get the selected value from a select tag, I have tried these cases but I'm not able to get the selected value, Does anyone have an idea on what is wrong with the way I'm declaring the action on the select tag?
UPDATE 1
I tried to recreate the problem on ember-twiddle but for some reason it won't run as it does on my project, it seems that ember-twiddle does not read the actions declared on the route. However here is the example:
Example on ember-twiddle
Case 1
In this case I got an error:
Uncaught TypeError: Cannot read property 'apply' of undefined
Template:
{{#each model.items as |item|}}
<select class="form-control" onchange={{action 'ChangeCombo' item "Price" value='target.value'}}>
<option value="">Select an option</option>
<option value="1000">$1000</option>
<option value="100">$100</option>
<option value="10">$10</option>
</select>
{{/each}}
Route Actions:
actions: {
ChangeCombo(paramOne, paramTwo, paramThree) {
console.info(paramOne, paramTwo, paramThree);
},
}
Case 2
In this case the first parameter is the current Item from de model array, the second one is the string "Price" but the third parameter is undefined and the last parameter which is supposed to be the event object is also undefined.
Console output:
{object}, "Price", undefined, undefined
Template:
{{#each model.items as |item|}}
<select class="form-control" {{action 'ChangeCombo' item "Price" value='target.value' on='change'}}>
<option value="">Select an option</option>
<option value="1000">$1000</option>
<option value="100">$100</option>
<option value="10">$10</option>
</select>
{{/each}}
Route Actions:
actions: {
ChangeCombo(paramOne, paramTwo, paramThree, paramFour) {
console.info(paramOne, paramTwo, paramThree, paramFour);
},
}
Ember version
ember-cli: 3.7.1
node: 10.1.0
os: win32 x64
If you wan't to use the action value option, you can't have any other params directly in the action helper.
You need to use closure actions, i.e.
<select class="form-control" onchange={{action (action 'ChangeCombo' item "Price") value='target.value'}}>
<option value="">Select an option</option>
<option value="1000">$1000</option>
<option value="100">$100</option>
<option value="10">$10</option>
</select>
Related
Could you please tell me why ng-change give "undefined" in angularjs ?
$scope.onChangedCityDropDown= function(i){
console.log(i)
}
<select class="form-control" ng-model="a"
ng-options="item.city as item.city for item in vals"
ng-change="onChangedCityDropDown(item)">
<option value="" disabled>choose an option</option>
</select>
here is my code
https://plnkr.co/edit/HLLUcVnIl8ySg8baSMHv?p=preview
You can directly pass the ng-model value to the function,
<select class="form-control" ng-model="a" ng-options="item.city as item.city for item in vals" ng-change="onChangedCityDropDown(a)">
<option value="" disabled>choose an option</option>
</select>
just change onChangedCityDropDown(item) to onChangedCityDropDown(a)
see
ng-options doesn't work the same way as that of ng-repeat works. You won't get item value apart from ng-options attribute value. To make it work you should use ng-model(a) value inside ng-change callback function.
ng-change="onChangedCityDropDown(a)"
Also you want to set state based on city selection then change ng-options to below
ng-options="item.state as item.city for item in vals"
Demo Plunker
Just change your ng-options as below this will give you the object you are expecting.
ng-options="item as item.city for item in vals"
ng-change="onChangedCityDropDown(a)"
in ng-change you have to pass your ng-model
here is the working example
You can try like the below code for the city and the state reference selection and also please check this plunker link of your given working example.
Controller:
$scope.vals = [{
"city":"gurgaon",
"state":"Haryana"
}, {
"city":"Manesar",
"state":"Haryana"
}, {
"city":"Bangalore",
"state":"Karnataka"
}];
$scope.onChangedCityDropDown= function(i){
console.log(i)
}
$scope.onChangedStateDropDown= function(i){
console.log(i)
}
Template:
City
<select class="form-control" ng-model="city" ng-options="item.city as item.city for item in vals" ng-change="onChangedCityDropDown(city)">
<option value="" disabled>Select City</option>
</select>
State
<select class="form-control" ng-model="state" ng-options="item.state as item.state for item in vals | filter:{city:city}" ng-change="onChangedStateDropDown(state)">
<option value="" disabled>Select State</option>
</select>
I am new to vue.js. I'm stuck trying to dynamically set the options of one select based on the selected value in another.
For example, I have two dropdowns named division and district.
var A = [{1: 'a'},{2:'b'}];
If value of A is 1, then the district select should load the related codes. I can do it with jQuery but not with Vue.
Here is my template.
<div class="form-group" :class="class_obj">
<select name="div_code" v-model="div_code" class="form-control" required>
<option value="">Choose One</option>
<option v-for="option in div_list" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
</div>
<div class="form-group" :class="class_label">
<label for="">District</label>
</div>
<div class="form-group" :class="class_obj">
<select name="dist_code" v-model="dist_code" class="form-control" required>
<option value="">Choose One</option>
</select>
</div>
Here is my javascript.
export default {
data():{
div_list: [
{'1': "ABC"} , {'2': "DEF"}
];
}
}
How can I load the dist_code select with related data from ajax when div_code value is 1?
Handle a change event on the div_code change
<select name="div_code"
v-model="div_code"
#change="onDivCodeChange"
class="form-control"
required>
<option value="">Choose One</option>
<option v-for="option in div_list" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
And add the onDivCodeChange method to your Vue.
methods:{
onCodeChange(){
// make an ajax call and set the options for your
// dist_code here. For example:
$.ajax({
url: "...",
data: {div_code: this.div_code},
success: codes => this.code_list = codes
error: err => console.log(err)
})
}
}
And of course add code_list as a property of your data and update your template.
<select name="dist_code" v-model="dist_code" class="form-control" required>
<option value="">Choose One</option>
<option v-for="code in code_list" :value="code.value">{{code.text}}</option>
</select>
Create computed property. Basically, when data property changes(should be used in computed property) the computed property will update itself.
As in below example, when message property changes the reverseMessage will update itself.
<div id="example">
<p>Original message: "{{ message }}"</p>
<p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
// a computed getter
reversedMessage: function () {
// `this` points to the vm instance
return this.message.split('').reverse().join('')
}
}
})
1- In your example, bind your first dropdown value to a data property.
2 -Create a computed property for the second dropdown and use the data
property in it.
3 -Write the template of it.
Done.
When the first dropdown changes the second dropdown will update itself.
Note: Vue documentation advise to use Watcher for Ajax requests instead of computed properties. Both are very similar.
https://v2.vuejs.org/v2/guide/computed.html#Watchers
I have a basic select element with options that dropdown hooked up to a small set of data which is being filtered using the dropdown. Initially on page load the select element has a value of undefined (according to the console), however after selecting any option it takes on the value of that option.
How can I go back to undefined? Basically I want to be able to select an option in the list that will go back to displaying all of the data. Below is my app on JSBin:
App
add a custom filter function
$scope.filterByGenre = function(item){
if (!$scope.selectedGenre || $scope.selectedGenre == 'All'){
return true;
}
return item.genre && item.genre.indexOf($scope.selectedGenre) != -1;
}
change your <select> to this:
<select ng-model="selectedGenre"
ng-options="choice as choice for (idx, choice) in genres"
name="genre"
class="genre-dropdown">
</select>
change <tr ng-repeat="... filters to this:
<tr ng-repeat="movie in movies | filter:searchBar | filter:filterByGenre | orderBy:sortType:sortReverse">
Online Demo - http://jsbin.com/riyafupexu/1/edit?html,js,output
I'm confused, you are using ng-options but you also provided the static options.
For a quick fix in this case you can remove that ng-options and uncomment that All and remove it's value.
Like:
<select ng-model="selectedGenre"
ng-change="handleSelect()"
name="genre"
class="genre-dropdown">
<option selected="selected" value="">All</option>
<option value="Action">Action</option>
<option value="Adventure">Adventure</option>
<option value="Animation">Animation</option>
<option value="Biography">Biography</option>
<option value="Comedy">Comedy</option>
<option value="Crime">Crime</option>
<option value="Drama">Drama</option>
<option value="Fantasy">Fantasy</option>
<option value="History">History</option>
<option value="Horror">Horror</option>
<option value="Romance">Romance</option>
<option value="Sci-Fi">Sci-Fi</option>
<option value="Western">Western</option>
</select>
You can do it this way:
$scope.selectedGenre = "";//set the model to blank.
$scope.genres = 'All,Action,Adventure,Animation,Biography,Comedy,Crime,Drama,Fantasy,History,Horror,Romance,Sci-Fi,Western';
//create an array after splitting the commas
$scope.genresAry = $scope.genres.split(',');
$scope.genresAry.push("");//push blank into the array.
In HTML use genresAry.
<select ng-model="selectedGenre"
ng-options="choice as choice for (idx, choice) in genresAry"
ng-change="handleSelect()"
name="genre"
class="genre-dropdown">
working code here
I have a form that builds out multiple widgets based on some JSON data. In part of that form is a select dropdown, and some items have different options selected by default.
ie:
object 1 {
tag: "products"
}
The select dropdown in the ng-repeat widget
<select class="btn-success form-control">
<option value="companies">companies</option>
<option value="news">news</option>
<option value="people">people</option>
<option value="products">products</option>
</select>
^ Here if this was object 1, I'd need the products option to gain the selected attribute.
What I've tried so far, that hasn't worked, but so you can see my thinking:
HTML
ng-repeat="stuff in stuffs"...
<select class="btn-success form-control">
<option value="companies">companies</option>
<option ng-if="widget.selectedTag(stuff.tag)" value="news">news</option>
<option value="people">people</option>
<option value="products">products</option>
</select>
Controller
this.selectedTag= function(s) {
console.log(s);
if (s = 'news'){
return 'selected';
}
}
How would you go about this?
Found answer here: Initializing select with AngularJS and ng-repeat
<option ng-selected="{{operator.value == filterCondition.operator}}"
ng-repeat="operator in operators"
value="{{operator.value}}">
So in my case:
<option value="products"
ng-selected="{{stuff.tag == 'products'}}">products</option>
I have some problem with my selects. Here is code:
<select ng-model="car" ng-options="car.name for car in cars track by car.id" class="form-control">
<option value="">Choose model name</option>
</select>
<br />
<select ng-options="model.name for model in car.models track by model.id" ng-model="model" class="form-control">
<option value="">Choose car</option>
</select>
I want that when i visit this page that the two selects have default values. For example in first select - "BMW" and in second - "528I" at the same time. I'm trying to do this solution:
<select ng-model="car" ng-init="car.id = 1" ng-options="car.name for car in cars track by car.id" class="form-control">
<option value="">Choose model name</option>
</select>
<br />
<select ng-init="model.id = 5" ng-options="model.name for model in car.models track by model.id" ng-model="model" class="form-control">
<option value="">Choose car</option>
</select>
But it work only for the fisrt select. (And when i choose the name in first select with name with id = 1, the second select to switch to the model with id = 5;)
I don't know how to reailze default values in two selects at the same time.
Have you any ideas?
Here is plunker
You could just use ng-init, like this:
<select ng-init="car=cars[0]" ng-model="car" ng-options="car.name for car in cars" class="form-control">
<option value="">Choose model name</option>
</select>
<br />
<select ng-init="model=car.models[0]" ng-options="model.name for model in car.modelsd" ng-model="model" class="form-control">
<option value="">Choose car</option>
</select>
UPDATE
The syntax of your select was a bit off, you actually don't need to use track by unless you are grouping the values of your select, which you are not. I think that what you actually wanted was to have the model bound to the id of the 'car' rather than having the model bound to the car itself. If that's what you want you could do it like this:
<select ng-init="car=1" ng-model="car" ng-options="car.id as car.name for car in cars" class="form-control">
<option value="">Choose model name</option>
</select>
But then you would have to do something very awkward in order to render the second select with the models of the car, because in that case your variable car would be an int (the id of the car), not the 'car' object, so you would have to do something like this:
<select ng-init="model=5" ng-model="model" ng-options="model.id as model.name for model in ((cars | filter:{id:car})[0].models)" class="form-control">
<option value="">Choose car</option>
</select>
Which is just as awkward as it looks, but if that's what you want... well, here you have an example:
Working example
A better idea in my opinion would be to have the models pointing to the objects, and if you want to initialize the selects through the id, you could do this in the ng-init :
<select ng-init="car=(cars|filter:{id:1})[0]" ng-model="car" ng-options="car.name for car in cars" class="form-control">
<option value="">Choose model name</option>
</select>
<br />
<select ng-init="model=(car.models|filter:{id:5})[0]" ng-options="model.name for model in car.models" ng-model="model" class="form-control">
<option value="">Choose car</option>
</select>
Working Example
You may use ng-model attribute in your controller and assign a default variable like
<div ng-controller="MyController" >
<form>
<select ng-model="myForm.car">
<option value="nissan">Nissan</option>
<option value="toyota">Toyota</option>
<option value="fiat">Fiat</option>
</select>
</form>
<div>
{{myForm.car}}
</div>
</div>
<script>
angular.module("myapp", [])
.controller("MyController", function($scope) {
$scope.myForm = {};
$scope.myForm.car = "nissan";
} );
</script>
jsbin
and go through this link so that you will come to know form handling in angularjs:
http://tutorials.jenkov.com/angularjs/forms.html