Hierarchical Select Using Aurelia - html

I'm writing a mobile application using Aurelia (Cordova, Typescript, HTML5, & Bootstrap) and I need to do a hierarchical select where the selection from one SELECT list filters the options in the next SELECT list. Does anyone know how to do this in Aurelia? My code with the bindings are below. The list in selRatedItems needs to be filtered by what is selected in selCategories. Thanks for any help.
<div class="row">
<div class="form-group form-group-sm">
<label for="selCategory" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
<select class="form-control" id="selCategory" value.bind="selectedCategory" required>
<option value="">Select a category...</option>
<option repeat.for="option of categories" model.bind="option">${option.name}</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group form-group-sm">
<label for="selRatedItem" class="col-sm-2 control-label">Rated Item</label>
<div class="col-sm-10">
<select class="form-control" id="selRatedItem" value.bind="selectedItem" required>
<option value="">Select an item...</option>
<option repeat.for="option of selectedCategory.rateditems" model.bind="option.rateditems.id">${option.rateditems.name}</option>
</select>
</div>
</div>
</div>

Here's how I resolved this...
First, I had to add a computed property in the view model that was based off of a property bound to the selCategory SELECT list from above.
Added import statement:
import { computedFrom } from 'aurelia-framework';
Added property:
#computedFrom('selectedCategory')
get rateditems() {
if (this.selectedCategory && this.selectedCategory.rateditems) {
return Object.keys(this.selectedCategory.rateditems).map(key => this.selectedCategory.rateditems[<any>key]);
}
else {
var array: any[] = [];
return array;
}
}
Then, I bound the SELECT list that is to be filtered, in this case, selRatedItem, to the computed property.
Newly bound SELECT list:
<div class="row">
<div class="form-group form-group-sm">
<label for="selRatedItem" class="col-sm-2 control-label">Rated Item</label>
<div class="col-sm-10">
<select class="form-control" id="selRatedItem" value.bind="selectedItem" required>
<option value="">Select an item...</option>
<option repeat.for="item of rateditems" model.bind="item.id">${item.name}</option>
</select>
</div>
</div>
</div>

Related

How can I update values of a dropdown list with angular?

I want to update values into dropdown list but when I click in the form the value change to null.
knowing that the add works correctly . Anyone can help me please ?
this is my html code:
<div class="form-group">
<label for="service">Service</label>
<select id="service" class="form-control" ngModel="{{editPersonne?.service}}" name="serviceId">
<option value="">Service</option>
<option value="service.id" *ngFor="let service of services" >{{service.designation}}</option>
</select>
</div>
you should use varibale
<div class="form-group">
<label for="service">Service</label>
<select id="service" class="form-control" [(ngModel)]="{{editPersonne?.service}}" name="serviceId">
<option value="">Service</option>
<option [value]="service.id" *ngFor="let service of services" >{{service.designation}}</option>
</select>
</div>
or
<div class="form-group">
<label for="service">Service</label>
<select id="service" class="form-control" ngModel="{{editPersonne?.service}}" name="serviceId">
<option value="">Service</option>
<option value="{{service.id}}" *ngFor="let service of services" >{{service.designation}}</option>
</select>
</div>

Problems with removeClass()

On the front-end I'm asking the user 2 questions, the first one visible, the second will be visible depending on the firs answer. But somehow my removeClass is not working as expected (not working at all)
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
And the functions I'm using are:
$('#savingfinanceiro').on('change',function () {
var component = $("#divfinanceiro");
makevisible($('#savingfinanceiro').val(), component);
});
function makevisible(value,component){
if (value === "0"){
component.removeClass("invisible");
}
}
Any help would be greatly appreciated.
as pointed out in the comments #savingfinanceiro and #divfinanceiro do not exist in your html. You must have overlooked that. In the following snippet I have fixed that. There, #savingfinanceiro, from the <select>, was changed to #savingcontratos and var component = $("#divfinanceiro");, the 'to-hide' <div> was changed to var component = $("#divcontratos");. After applying these changes your code should work as expected.
$('#savingcontratos').on('change', function() {
var component = $("#divcontratos");
makevisible($('#savingcontratos').val(), component);
});
function makevisible(value, component) {
if (value === "0") {
component.removeClass("invisible");
} else {
component.addClass("invisible");
}
}
.invisible {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
Hope this helps!

angular2 ngif selected show required form

I'm new to angular2
I want to create a function that when user choose an option the required form wil be show.
This is my html code
<label class="col-md-3 form-control-label" for="select">Zone</label>
<div class="col-md-9">
<select class="form-control" id="select" name="select" size="1">
<option *ngFor="let obj of zone" value={{obj.id}} ngDefaultControl>
{{obj?.name}}</option>
</select>
</div>
Let's say a user chooses Zone 1, then a form for Zone 1 appears. If a user selects Zone 2, then a form for Zone 2 appears. And so on. How can I create those functions in Angular2? I want the form to appeared in the same html page.
You can simply do this with switch cases like below.
My HTML file:
<label class="col-md-3 form-control-label" for="select">Zone</label>
<div class="col-md-9">
<select class="form-control" id="select" name="select" size="1" [(ngModel)]="selected">
<option *ngFor="let obj of zone" value={{obj.id}} ngDefaultControl>
{{obj?.name}}</option>
</select>
</div>
<div [ngSwitch]="selected">
<div *ngSwitchCase="1">
<p>1st Form</p>
</div>
<div *ngSwitchCase="2">
<p>2nd Form</p>
</div>
<div *ngSwitchCase="3">
<p>3rd form</p>
</div>
</div>
My TypeScript file
zone:Array<Object>=[
{id:1,name:"Name One"},
{id:2,name:"Name Two"},
{id:3,name:"Name Three"}
];
selected:number=0;
You can do this:
- Delare a property in component class to hold selected zone
- Then use ngIf base on above property
<label class="col-md-3 form-control-label" for="select">
<div *ngIf="selectedid==1">
Zone1
</div>
<div *ngIf="selectedid==2">
Zone2
</div>
</label>
<div class="col-md-9">
<select class="form-control" id="select" [(ngModel)]="selectedid" name="select" size="1">
<option *ngFor="let obj of zone" value={{obj.id}} ngDefaultControl>
{{obj?.name}}</option>
</select>
</div>

Display div or input box, when drop down value selected as Others

Plnkr code for review : https://plnkr.co/edit/vWR7kK9MQw4nyciRt1Bj?p=preview
How do I display a div or input box, once a drop down value is selected as Others?
Html
<div class="form-group fields col-sm-2 col-xs-4">
<label>EDUCATION QUALIFICATION *</label>
<select name="education" class="form-control1 drop" required ng-model="model.education" placeholder="select">
<option value='' disabled selected>Select</option>
<option value="male">10th</option>
<option value="female">12th</option>
<option value="male">Graduate</option>
<option value="male">Post Graduate</option>
<option value="male">Ph.d</option>
<option value="other">Others</option>
</select>
</div>
<div class="form-group fields col-sm-2 col-xs-4 floatField" ng-if =" model.education=Others;displayDiv = true">
<label>OTHERS</label>
<input type="text" name="othereducation" class="form-control1" autocomplete="off" ng-model="model.othereducation"/>
</div>
You can use Angular in-built directives ng-show/ng-hide to show or hide the content.
So you can add the below snippet to show the required content when you select the value 'other' in selectbox.
<div ng-show="model.education==='other'">
DIV/input you want to show
</div>
Demo:
JSBin
Here is the working link
Try the code below
Code
<div ng-if="model.education==='other'">
<label >OTHERS</label>
<input type="text" name="othereducation" class="form-control1" autocomplete="off" ng-model="model.othereducation"/>
</div>

How to disable a input field according to another select field value in bootstrap?

I am using bootstrap, I wanted to know is there something like a feature in bootstrap to do this?
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other">
</div>
</div>
I want to activate this bellow input field, if the above select field value is "other"
Since I could not able to find a short cut for this using bootstrap, I thought to write this in native javascript.
function disable(select_val,input_id) {
var e = document.getElementById(select_val);
var strUser = e.options[e.selectedIndex].value;
if(strUser === "100"){
document.getElementById(input_id).disabled = false;
}
else{
document.getElementById(input_id).value = document.getElementById(input_id).defaultValue;
document.getElementById(input_id).disabled = true;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Principle mode of water supply</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="water_supply" onchange="disable('water_supply', 'w_s_other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="4">Private pipe line</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="w_s_other" disabled value="">
</div>
</div>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">x2</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="x2" onchange="disable('x2', 'x2other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="x2other" disabled value="">
</div>
</div>
Try using change event at select element to set input disabled to true if value of select element is "5" with .prop()
$("select").change(function() {
$("+ input", this).prop("disabled", !(this.value === "5"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other" disabled="true">
</div>
</div>