I want to hide and show the textbox when the ion-select(combobox) is change
For example I have : 1 and 2 in ion-select
If i choose 1 the textbox will hide and if i choose 2 the textbox will appear
Here is my current code:
.ts
onChange(membership){
if (this.membership = '1'){
this.value = 1; }
else if (this.membership = '2'){
this.value = ""; }
}
HTML
<ion-list>
<ion-item>
<ion-label>Register as</ion-label>
<ion-select [(ngModel)]="membership" (ionChange)="onChange(membership)">
<ion-option value="1">member</ion-option>
<ion-option value="2">nonmember</ion-option>
</ion-select>
</ion-item>
</ion-list>
this is the textbox that i want to hide
<ion-item [hidden]="!value">
<ion-label floating>Email Address</ion-label>
<ion-input type="text" name="email" ></ion-input>
</ion-item>
When I'm opening the application the textbox is hiding and when i select from the combobox the textbox is showing but when I select the other item in the combobox its not hiding anymore.
It is just a simple error in onChange method. You are assigning the value with =, so it always returns true and set this.value to 1. You have to use double equals to verify equality :
onChange(membership){
if (this.membership == '1') {
this.value = 1;
} else if (this.membership == '2'){
this.value = 0;
}
}
Related
I am trying to add a country code where when you press on the selector under the "country" label, in the second picture, you get a list like the first picture with the country names and the flags next to it then you would be redirected to the second picture with the selector displaying the selected country flag and the country code. I tried to use
Html
<ion-row>
<ion-col>
<ion-item (click)="openSelect()">
<div class="ion-text-left">
<ion-label position="stacked">البلد</ion-label>
<ion-input [(ngModel)]="selectedCode"></ion-input>
</div>
</ion-item>
<ion-item style="display: none">
<div class="ion-text-left">
<ion-label >البلد</ion-label>
<ion-select #select1
[(ngModel)]="selectedCode">
<ion-select-option *ngFor="let c of countries" value="{{c.flag}}{{c.code}}">{{c.flag}}{{c.name}}</ion-select-option>
</ion-select>
</div>
</ion-item>
</ion-col>
.TS
#ViewChildren('select1') select1: Select;
countries: any = [
{
code: '+91',
flag: [./in.png],
name: 'India'
},
{
code: '+1',
flag: [./US.png]
name: 'America'
}
];
openSelect() {
setTimeout(() => {
this.select1.open();
}, 150);
}
for selecting the country code value you can use [value] property of ion select option
and for displaying the selected value you can use [selectedText] property of ion select in ionic 4/5
you can find official documentation here
<ion-select formControlName="country" [selectedText]="signupform.controls.country.value" name="country" #countryselect id="countryselect">
<ion-select-option *ngFor="let country of countries" [value]="country.id">
{{country.name}}
</ion-select-option>
</ion-select>
I have a form in my ionic project that has 2 dropdown menus of selection ion-select.
When the user selects an option in the first dropdown menu, it generates the options in the second dropdown list. This works well.
When I add new fields, I can reestablish the values of the first list displayed with [(ngModel)] = null in .ts, but I have not been able to reestablish those in the 2 drop-down list.
Image of error
,
image
This is the markup I have
<div formArrayName="sucursales" margin-bottom>
<!--generate new registration fields-->
<section [formGroupName]="i" *ngFor="let tech of form.controls.sucursales.controls; let i = index">
<ion-item>
<ion-icon name="list" item-start></ion-icon>
<ion-label>Departamento</ion-label>
<ion-select [(ngModel)]="locate" name="country" #country formControlName="country" (ionChange)="CountryChange($event)">
<ion-option *ngFor="let department of ubicacion; let i = index" value="{{department.departamento}}">{{department.departamento}}</ion-option>
</ion-select>
</ion-item>
<ion-item *ngFor="let x of selectedMunicipality">
<ion-icon name="list" item-start></ion-icon>
<ion-label>Municipio</ion-label>
<ion-select [(ngModel)]="locates" name="state" #state formControlName="state">
<ion-option *ngFor="let municipio of x.ciudades" value="{{municipio}}">{{municipio}}</ion-option>
</ion-select>
</ion-item>
And this id my code .ts:
this.http.get('http://localhost/ionic-php-mysql/colombia.json').map(res => res.json()).subscribe(
data => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.ubicacion = data.colombia;
},
error => {
console.log("Oops!", error);
}
);
CountryChange(value: string) {
this.locate = null; //It works well
this.locates = null; //it does not work
this.selectedMunicipality = this.ubicacion.filter(item => item.departamento === value)
}
I want the second ion-select to select according to the first ion-select, and if I add new fields dynamically that the new values are independent.
I finally resolved.
buildGroup(): FormGroup {
return this.fb.group({
selectedSubcategory: [""],
});
CountryChange(value, index:number) {
this.productos.controls[index]selectedMunicipality = this.ubicacion.filter(item => item.departamento === value)
}
and in HTML
(ionChange)="CountryChange($event,i)"
<ion-item *ngFor="let x of tech.selectedMunicipality">
i stuck , please help me
I am using ion-searchbar , i want that when there is no text on the searchbar the list must be hide but the list is still showing
Can anyone help me?
in .html file
<ion-searchbar (ionInput)="getItems($event)" class="" style="background-color: #073262 !important" [showCancelButton]="shouldShowCancel" style="background:none!important;"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let item of items" (click)="openNavDetailsPage(item)">
{{ item }}
</ion-item>
</ion-list>
in .ts file
getItems(ev) {
// Reset items back to all of the items
this.initializeItems();
// set val to the value of the ev target
var val = ev.target.value;
// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.items = this.items.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
You can use ngModel and ngIf :
<ion-searchbar [(ngModel)]="searchInput" (ionInput)="getItems($event)" class="" style="background-color: #073262 !important" [showCancelButton]="shouldShowCancel" style="background:none!important;"></ion-searchbar>
<ion-list *ngIf="searchInput !== ''">
<ion-item *ngFor="let item of items" (click)="openNavDetailsPage(item)">
{{ item }}
</ion-item>
</ion-list>
in .ts:
public searchInput='';
OR
If your variable items is empty at initalization :
<ion-searchbar (ionInput)="getItems($event)" class="" style="background-color: #073262 !important" [showCancelButton]="shouldShowCancel" style="background:none!important;"></ion-searchbar>
<ion-list *ngIf="items && items.length">
<ion-item *ngFor="let item of items" (click)="openNavDetailsPage(item)">
{{ item }}
</ion-item>
</ion-list>
in .ts:
public items = [];
I am using ionic 3, and looping ion-card with like using ngFor. I want to know how can I react with the user when user click the like/unlike button in each ion-card without reload the list.
<ion-card *ngFor="let u of users">
<p>{{u.name}}</p>
<button ion-button [hidden]="u.isliked=='1'" (click)="like(u.id)">like</button>
<button ion-button [hidden]="u.isliked!='1'" (click)="unlike(u.id)">unlike</button>
</ion-card>
You can make use of the *ngIf operator. This won't hide the element like the hidden property, but actually removes the element from the DOM.
(made u.isLiked into a boolean because I think it's cleaner that way, personal preference. Also changed (click) to (tap), see the answer on ionic2 tap vs click for more details.)
<ion-card *ngFor="let u of users">
<p>{{u.name}}</p>
<button ion-button *ngIf="u.isLiked" (tap)="like(u.id)">like</button>
<button ion-button *ngIf="!u.isliked" (tap)="unlike(u.id)">unlike</button>
</ion-card>
And in your ts:
like(userId) {
for(let user of this.users) {
if(user.id == userId) {
user.isLiked = true;
}
}
}
unlike(userId) {
for(let user of this.users) {
if(user.id == userId) {
user.isLiked = false;
}
}
}
I have 2 select in ionic 2 and i am able to dynamically populate the second based on the value selected in first. but i cannot set the selected default option after the population. i tried:
<ion-item>
<ion-label>City</ion-label>
<ion-select (ionChange)="setCity($event)">
<ion-option selected >City1</ion-option>
<ion-option >City2</ion-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Zona</ion-label>
<ion-select >
<ion-option *ngFor ="let location of locations[city]"
[selected] = location.selected >
{{location.zone}}
</ion-option>
</ion-select>
</ion-item>
in .ts
this.locations = {'City1':[{ zone: 'Zone1', selected: true},{zone:'Zone2', selected: false}],
'City2': [{zone: 'Zone3', selected: false} {zone:'Zone4', selected: true}]}
setCity(event){
this.city = event;
}
there is no selected value after the second ion-select is populated
thank you.
Try setting the ngModel attribute in ion-select and value attribute in ion-option like below
<ion-label>Zona</ion-label>
<ion-select [(ngModel)]="mLocation" >
<ion-option [value] = "location" *ngFor ="let location of locations[city]"
[selected] = location.selected >
{{location.zone}}
</ion-option>
</ion-select>
Create a change event handler on the first select element. Then have that call a method, passing the selected value with it. Then do some checks on the selected values to determine if you want to show the next select and then the list.
Check out this StackOverflow question How to show second dropdown data based on previous dropdown selection
Working version: https://embed.plnkr.co/Qu1lL6PDIX2DTDGv01ZI/
UPDATE Display Default selected item
You wanna display Default selected item then you need to add these few line in your html page
like that:
<ion-label>District</ion-label>
<ion-select (ionChange). ....
<ion-option [value]="0" >select District</ion-option>
<ion-option [value]="sDistrict" *ngFor = ...
</ion-select>
and add these line in your type script(.ts) file
export class HomePage {
....
sState: number = "0";
sDistrict: number = "0";
.....
}
if you face any problem then let me know.