Angular 2 Reactive Forms - Binding select drop down value in form array of a reactive form - angular2-forms

I have created a reactive form which has a form array. A Form Group is pushed into the form array on a button click. So every button click generates a section of html on screen.
This form group contains 2 drop down lists having parent-child relation i.e. selection of item in parent drop down decides values to be populated in child drop down.
When I select a value in parent drop down change event fetches results for child but this has to be limited to the scope of the specific form array element. Currently change event fetches data and sets values for child drop downs in each form array element. This is a basic structure of my mark up
<form [formGroup] = "mainForm" (ngSubmit)="savedata()">
<div>some form control</div>
<div formArrayName = "myArray" *ngFor="let arrayitem of myArray.controls";let i = index">
<div [formGroupName] = "i">
<div>
<select class="form-control" (change)="onSelect($event.target.value)" id="{{'header' + i}}" formControlName="parentdrpdown">
<option value="" disabled selected hidden>Select a Parent...</option>
<option *ngFor="let pitem of parentdrpdown"
value={{pitem.id}}>
{{pitem.name}}
</option>
</select>
</div>
<div>
<select class="form-control" (change)="onSelect($event.target.value)" id="{{'header' + i}}" formControlName="childdrpdown">
<option value="" disabled selected hidden>Select a Child...</option>
<option *ngFor="let citem of childdrpdown"
value={{citem.id}}>
{{citem.name}}
</option>
</select>
</div>
</div>
</div>
constructor(private fb:
FormBuilder, private _segService: SegmentService) {
this.parentdrpdown= this._segService.getparentValues();
onSelect(pid) {
this.childdrpdown= this._segService.getChildSegment()
.filter((item) => item.parentid == pid);
}
parentdrpdown and childdrpdown are properties declared in component and they are loaded with values using http get request to a web server

Use below snippet for the code in reactive form in case of select tag.
<h1>My Application</h1>
<select formControlName="selectedValue">
<option *ngFor="let c of countries" [ngValue]="c">{{c.name}}</option>
</select>

Related

How to create select element with selected option in shopping cart view

In ASP.NET Core 5 MVC shopping cart Razor view in used to create cart layout selector.
In ASP.NET MVC 4.7 it works OK but in .NET Core lines which set selected option
<option value="List" #(Model.LocatorViewModel.CartLayout == CartLayout.List ? "selected" : "" )>
show syntax error in razor editor in VS 2019:
Which is property method to create select element selected attribute in Razor?
Bootstrap 3 is used.
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<label>
#await I.I("Display mode")
<select name="cartlayout" class="form-control" onchange="this.form.submit()">
<option value="List" #(Model.LocatorViewModel.CartLayout == CartLayout.List ? "selected" : "" )>
#await I.I("List")
</option>
<option value="Thumb" #(Model.LocatorViewModel.CartLayout == CartLayout.Thumb ? "selected" : "" )>
#await I.I("Thumbnails")
</option>
</select>
</label>
</div>
</div>
As error message indicated, the 'OptionTagHelper' can not have C# in the element's attribute declaration area.
In the code you shared, it seems that the option works as just plain HTML <option> in your scenario. If so, you can try to apply #removeTagHelper to a specific view that would help remove the specified Tag Helper from the view.
#removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.OptionTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers
Test Result

Odoo 11 - assign value to variable on <select> tag in Web Template

I want to assign value to a variable when a user picks a choice in a dropdown in Odoo 11 Web Template. So when a user has done clicking the dropdown then a value will be assigned with a value accordance to user pick
<div class="col-md-3 col-sm-3">
<select class="form-control" id="university_id" name="university_id">
<t t-foreach="universities" t-as="university">
<option t-att-value="university.id"><t t-esc="university.name"/><t set="var_univ" t-value="university.name"/></option>
</t>
</select>
</div>
I want to set a value in var_univ variable with the name of the university. How can I achieve this?
please try with following :
<div class="col-md-3 col-sm-3">
<select class="form-control" id="university_id" name="var_univ">
<t t-foreach="universities" t-as="university">
<option t-att-value="university.name">
<t t-esc="university.name"/>
</option>
</t>
</select>
</div>
Make sure above div tag is in Form View with action.
Thanks !

Ionic drop down IOS

I have the following json
[{"name":"Male","code":"1"},{"name":"Female","code":"2"}]
<label id="labelPersonalInformationExtra" class="item item-input item-stacked-label">
<span id="inputLabel" class="input-label">Gender</span>
<select id="dropDownPersonalDetails" ng-options="gender.name for gender in genders" ng-model="gender" ng-change="updateGender(gender)" >
<option style="display:none;" value="" disabled>{{genderPlaceholder}}</option>
</select>
</label>
What I wanted to do is have the placeholder to show the last option they chose but what happens is that the value becomes duplicated. If their last selected choice was female. The drop down would show as:
Female
Female
Male
How do I point their selected value to a value in my json so the drop down shows directly that.
Added option for placeholder will remain as is, you can't remove it dynamically. I'd rather prefer to give a generic text to placeholder of select like Select a gender. And then for preselected Female option you could do $scope.gender = $scope.genders[1] inside your controller.
Markup
<label id="labelPersonalInformationExtra"
class="item item-input item-stacked-label">
<span id="inputLabel" class="input-label">Gender</span>
<select id="dropDownPersonalDetails" ng-model="gender"
ng-change="updateGender(gender)"
ng-options="gender.name for gender in genders">
<option value="" disabled>Select a gender</option>
</select>
</label>
Demo Plunker

Angular select option send value id only

I try to use ngOptions Angular. I want to send parameter to back-end to send value only not object.
Here is my code:
<div class="form-group">
<label>Parent Menu</label>
<select class="form-control" ng-model="admin_asset.parent_id" ng-options="parent.title for parent in parent_asset_options">
<option value="">-- choose parent --</option>
</select>
</div>
When I select the option, the output shows:
"parent_id": {
"id": 1,
"title": "Index Assets"
}
I just want to send value 1 only. So the result should be:
"parent_id": 1
I have tried to another tutorial but I did not have solution. Thanks
You can use select as
<div class="form-group">
<label>Parent Menu</label>
<select class="form-control" ng-model="admin_asset.parent_id" ng-options="parent.id as parent.title for parent in parent_asset_options">
<option value="">-- choose parent --</option>
</select>
</div>
It's necessary to extend you ng-options expression with some extra fields.
ng-options="parent.id as parent.title for parent in parent_asset_options"
more examples

based on Session(UserType), display particular div on ASP.NET MVC Page

I have a view page that has two DIV tags.
If the session[UserType] = ‘Guest’ , then I need to diplay Guest DIV Section.
Else
If session[userType] = ‘Logged’
, then I don’t need to diplay Guest DIV Section.
<div id="Logged" >
<div class="txtContent">
<label for="product" class="Form_Label">product:<em>*</em></label>
<%= Html.DropDownList("product", " -- Select one -- ")%>
</div>
</div>
<div id="Guest">
<label for="DeliveryMethod">Delivery Method: </label>
<select name="DeliveryMethod" id="Select1" style="width: 100px">
<option value="email">Email</option>
<option value="fax">Fax</option>
<option value="mail">Mail</option>
</select>
</div>
Appreciate your responses.
Thanks
Sounds like you could do with either jQuery or a partialView.
My flavour would be to display a partial view based on the decision point.
Remember that your page shouldn't have any logic in it so you may want to write a helper to make the decision for you.
As an alternative you could render the page and then call a jQuery method that will return a partial view based on the parameter you send it.
If you require code please add a comment and I'll post some.
Edit
#Rita, if you're happy to have the HTML present on the page and you only want to hide/show the divs then here is the code for that;
<input type="hidden" id="hdnSessionValue" value="<%=session[UserType]">
<div id="Guest">
<label for="DeliveryMethod">Delivery Method: </label>
<select name="DeliveryMethod" id="Select1" style="width: 100px">
<option value="email">Email</option>
<option value="fax">Fax</option>
<option value="mail">Mail</option>
</select>
</div>
Then you have the following jQuery;
<script language="javascript">
$(document).ready( function () {
if ( $('#hdnSessionValue').val() == "Guest")
$('#Guest').show();
else
$('#Guest').hide();
} );
</script>
Edit2
Based on the amount of HTML you have to hide/show I don't think a jQuery postback is required.

Resources