ember js get form select after submit - ember.js

Tried to search some example with form select list but seems they always take value by using onchange function
http://balinterdi.com/2015/08/29/how-to-do-a-select-dropdown-in-ember-20.html
However in my project I want something like this:
<form {{action "createBook" on="submit"}}>
<p>{{input type='text' id='title' value=title}}</p>
<p> <select name="lang" id="lang">
<option value="">--Choose--</option>
{{#each model.language as |lang|}}
<option value="{{lang.url}}">{{lang.title}}</option>
{{/each}}
</select>
</p>
</form>
In my controller I have:
actions:{
createBook() {
var title = this.get('title'); //This line works fine
var lang = this.get('lang'); //This value from select isnt working at all
//do stuffs ..
},
}
What can i do so that i can get the value from select in createBook() actions?

You can pass the option value in select onchange and set value in from action handler,
<select onchange={{action "selectOption" value="target.value"}}>
<option value="">--Choose--</option>
{{#each model.language as |lang|}}
<option value="{{lang.url}}">{{lang.title}}</option>
{{/each}}
{{/each}}
Actions,
actions: {
selectOption: function(option) {
this.set("lang", option);
}
}

you can use like this
<select {{action 'change' on='change'}}>
{{#each content as |item|}}
<option value="{{item.id}}">
{{item.name}}
</option>
{{/each}}
in onchange method and set the property of lang of selected item
actions: {
createBook() {
var title = this.get('title'); //This line works fine
var lang = this.get('lang');
console.log(lang, title);
},
change() {
const selectedEl = this.$('select')[0];
const selectedIndex = selectedEl.selectedIndex;
const selectedValue = this.get('content')[selectedIndex];
this.set('lang', selectedValue.id)
}
}
Please check hereTwiddle

Related

How to get the value from the drop down box django? without submit button

How to get the value from the dropDown Box in Django Without Submit Button
<div class="single-shorter">
<form action="." method="GET">
<label>Sort By :</label>
<select name="val" id="val">
<option selected="selected" value="name">Name</option>
<option value="price">Price</option>
</select>
</form>
</div>
You can simple obtain it using Ajax but as mentioned in comments in dont want to use that u can use value attribute in option value and using event listner and window.loction.href we can obtain this... This is one example
<select id="foo">
<option value="">Your Dropdown</option>
<option value="http://www.google.com">x</option>
<option value="http://www.facebook.com">y</option>
</select>
<script>
document.getElementById("foo").onchange = function() {
if (this.selectedIndex!==0) {
window.location.href = this.value;
}
};
</script>
as u are using django use this and also dont want to change the value attribute
<select id="my_selection">
<option value="x" href="/link/to/somewhere">value 1</option>
<option value="y" href="/link/to/somewhere/else">value 2</option>
</select>
<script>
document.getElementById('my_selection').onchange = function() {
window.location.href = this.children[this.selectedIndex].getAttribute('href');
}
</script>

Select statement using handlebars in Ember

/ exams / template.hbs
<form>
<select name="selectvalue" id="selectvalue">
<option value="one"> One </option>
<option value="two"> Two </option>
<option value="three"> Three </option>
</select>
<button type="submit" {{action "printans"}}> Submit </button>
</form>
/ exams / controller.hbs
import Controller from '#ember/controller';
export default Controller.extend({
actions: {
printans: function(){
let val = this.get('selectvalue');
console.log(val);
}
}
});
All I need is I want to replace the html code in the template.hbs with handlebars, and when the form is submitted, I need to pass the value to the controller.
Do you mean something like:
<form>
{{#power-select
selected=selectvalue
options=cities
onchange=(action "printans")
as |name|
}}
{{name}}
{{/power-select}}
</form>
I'm totally guessing here.
Additional Details would be helpful:
- what are you trying to do (specifically)
- where do you get your list of items?
There is this addon, which covers a lot more common behavior: https://github.com/cibernox/ember-power-select

Cant trigger action from option select

I have the following in my template:
<select>
{{#each sortedManufacturers key="id" as |manufacturer|}}
<optgroup label="{{manufacturer.name}}">
{{#each manufacturer.cars key="id" as |car|}}
<option {{action "carSelected" car}} value="{{car.model}}">{{car.model}}</option>
{{/each}}
</optgroup>
{{/each}}
</select>
In the controller, I have:
actions: {
carSelected(car) {
console.log(car);
}
}
When the option is selected from the select. It doesn't seem to trigger the carSelected action.
Any ideas why?
In Ember > 1.13 you can do the following:
<select onchange={{action "carSelected" value="target.value"}}>
<!-- ... -->
</select>
With component JS:
export default Component.extend({
actions: {
carSelected(car) {
// ..
}
}
});
Or you can use the unquoted “closure action” form:
<select onchange={{action carSelected value="target.value"}}>
<!-- ... -->
</select>
With component JS:
export default Component.extend({
carSelected(car) {
// ..
}
});
See the improved actions RFC for more on all this.
I don't exactly know why click event is not binded to option this way. I beleive it's better to use change event on select instead.
<select {{action 'carSelected' on='change'}}> ... </select>
Here is implementation details: http://emberjs.com/deprecations/v1.x/#toc_ember-select

On submit can't get data from checkbox selection

Hi in my apps I have a form with checkbox group, and I can't retrived selected at submit.
Here is some code
The form content from declare.handlebars:
<form class="declare">
<div class="hidden-fields" style="display:none">
{{view Ember.TextField valueBinding="declaration_type" class="form-control half" type="text"}}
</div>
<fieldset>
...
</fieldset>
<fieldset>
<div class="form-group">
<label>Type de support</label>
<p>
{{render 'publication/declaration_support_types' Sampick.supportTypes}}
</p>
</div>
...
</fieldset>
<div class="actions-bottom">
<button {{action "sendDeclaration" content}} class="button button-select"><i class="icon-download"></i> Confirm</button>
</div>
</form>
The handlebars code for the render of publication/declaration_support_types:
{#each }}
<label class="checkbox-inline">
{{input type="checkbox" name="publication_declaration_support_type" checked=isChecked}} {{ description }}
</label>
{{/each}}
Then I have the following controller for the render 'publication/declaration_support_types':
Sampick.PublicationDeclarationSupportTypesController = Ember.ArrayController.extend({
sortProperties: ['description'],
sortAscending: false,
itemController: 'publicationDeclarationSupportType',
selected: Ember.computed.filterBy('[]', 'isChecked', true),
selectedItems: Ember.computed.mapBy('selected', 'description')
});
Sampick.PublicationDeclarationSupportTypeController = Ember.ObjectController.extend({
isChecked: false,
toggle: function() {
this.toggleProperty('isChecked');
}
});
and finaly the route for the previous html
Sampick.PublicationDeclareRoute = Ember.Route.extend({
actions: {
sendDeclaration: function(content) {
var self = this;
if (content.get("prints") == 1) {
self.validateRecipient(content);
} else {
self.submitDeclaration(content);
}
}
}
});
My issue is that in my sendDeclaration action I can't get the selected checkbox from declarationSupportTypes using the selectedItems propertie define in the controller.
Thanks for your helps
Working fiddle: http://emberjs.jsbin.com/legozucega/1/
There was a typo in IndexRoute, action=>actions.

Ember Checkbox Select All, or How to set an observable boolean property not tied to a model

I'm trying to set an observable property for a boolean value isCompleted in a controller that is not tied to any DataStore model as a workaround in an attempt to have a toggleable checkbox and all select checkbox, and I've tried the following with varying degrees of success:
http://emberjs.com/guides/getting-started/display-a-button-to-remove-completed-todos/ and the related JSBin: http://jsbin.com/ULovoJI/1/edit
http://poeticsystems.com/blog/ember-checkboxes-and-you and the related JSBin: http://jsbin.com/heqeni/4/edit
http://alexdiliberto.com/posts/ember-toggle-all-checkbox/ and the related JSBin: http://emberjs.jsbin.com/coliwiwa/2/edit
http://codeflip.przepiora.ca/blog/2014/05/22/ember-js-recipes-checkboxable-index-pages-using-itemcontroller/ and the related JSBin: http://jsbin.com/heqeni/4/edit
Example 3 is ultimately what I want, and has gotten me the closest. The toggle all checkbox works perfectly, but the individual check boxes do nothing. There is in fact something happening behind the scenes, because the bound value is being "updated", the handlebars template updates toggles is being reflected in the total items count, but the handlebars template doesn't update the checkboxes. Likewise, if I check an individual checkbox, all checkboxes are toggled (not just the one I clicked), and the toggleItemCount doesn't add anything. I've tried a number of variations on this, including specifying my own action to handle the toggle on a button, but the template then doesn't update.
App.UserAttendeeController = Ember.ObjectController.extend(Ember.Validations.Mixin, {
isChecked: false,
registerOnParent: function() {
this.send('registerToggle', this);
}.on('init'),
willDestroy: function() {
this.send('deregisterToggle', this);
}
});
App.UserAttendeesController = Ember.ObjectController.extend({
/**
Simply a placeholder array for each child `itemController`
*/
toggles: function(){ return Ember.A([]) }.property(),
/**
This computed property acts as both a setter and a getter. Check
out the docs for more information on this type of computed property:
http://emberjs.com/guides/object-model/computed-properties/#toc_setting-computed-properties
http://emberjs.com/guides/getting-started/toggle-all-todos/
*/
allChecked: function(key, value){
if (arguments.length === 1) /* get */ {
/* Executes `if` block on get when the user toggles any of the individual `itemController` checkboxes */
var toggles = this.get('toggles');
return toggles && toggles.isEvery('isChecked');
} else /* set */ {
/* Executes `else` block on set when the user toggles the actual "Toggle Select All" checkbox */
this.get('toggles').setEach('isChecked', value);
return value;
}
}.property('toggles.#each.isChecked'),
/* Get the total number of selected checkboxes */
allSelectedItems: Ember.computed.filterBy('toggles', 'isChecked', true),
totalSelectedCount: Ember.computed.alias('allSelectedItems.length'),
actions: {
/* Called when each child `itemController` is initialized (initial state/dynamically added) */
registerToggle: function(toggle) {
this.get('toggles').addObject(toggle);
},
/* Called when each child `itemController` is deleted and destroyed from the view render tree */
deregisterToggle: function(toggle) {
this.get('toggles').removeObject(toggle);
},
addAttendee: function() {
user = this.store.createRecord("user");
},
removeAttendees: function() {
var allSelectedItems = this.get('allSelectedItems').mapBy('content');
if(this.get("isIndividual")){
this.get('otherFamilyMembers').removeObjects(allSelectedItems);
} else {
this.get("group").get("users").removeObjects(allSelectedItems);
}
}
}
});
<div class="row">
<div class="col-xs-12 padding-l-0">
<button class="btn btn-primary" {{action "addAttendee"}}>Add Attendee</button>
<button {{bind-attr class=":btn :btn-default :padding-l-10 attendeesRows::disabled"}} {{action "removeAttendees"}}>Delete Attendee</button>
</div>
</div>
<div class="row padding-b-20">
<div id="attendee-users-manual-header" class="flowrow center font-small border-group white font-xs-7">
<div class="col-xs-1 border light-default-border default-background padding-5">
<div class="check-all-named-user-container">
{{input type="checkbox" checked=allChecked id="allchecked" name="allchecked"}}
</div>
</div>
<div class="col-xs-4 col-sm-2">
<strong>First Name</strong>
</div>
</div>
{{#each attendee in attendeesRows itemController="userAttendee" }}
<div class="flowrow center font-small font-xs-7">
<div class="col-xs-1 light-default-border" style="min-height:34px;">
{{input type="checkbox" checked=isChecked}}
{{#if checkedValue}}
y
{{else}}
n
{{/if}}
</div>
<div class="col-xs-4 col-sm-2 padding-0">
{{input type="text" value=attendee.firstName placeholder="First Name" class="form-control full-width required"}}
</div>
</div>
{{/each}}
<strong>{{totalSelectedCount}}</strong> currently selected
</div>
At a loss, have put 42 man hours into this already.