I need to set default value to ion-select item.
The ion-select above contain list of places, After choosing a place I save it into a localStorage.
What I need this localStorage be the default value of the ion-select,like this:
When the page is changed the ion-select return the placeHolder and no item is selected
CODE:
<ion-item>
<ion-select multiple="false" placeholder="Which Place?" [(ngModel)]="placeId" (ionChange)="showPlace()">
<ion-option class="text-input place-field" *ngFor="let place of places | async;let i = index" value="{{place.id}}">
{{place.name}}
</ion-option>
</ion-select>
</ion-item>
I found a solution to that..
add a AbstractControl to your [(ngModel)].
example:
in yout TS file..
placeForm: FormGroup;
placeId: AbstractControl;
constructor(){
this.placeForm.formBuilder.group({
placeId: ['', Validators.required],
});
this.placeForm.get('placeId').setValue(place.id);
}
in your html
just add this [(ngModel)]="placeForm.placeId"
If you have the index value you can just fetch that from localstorage and when the page loads you can save it in a variable and then use it to bind to the selected property on ion-option:
html:
<ion-item>
<ion-select multiple="false" placeholder="Which Place?" [(ngModel)]="placeId" (ionChange)="showPlace()">
<ion-option class="text-input place-field" *ngFor="let place of places | async;let i = index" [selected]="i == selectedIndex" value="{{place.id}}"> // the variable selected index will be a number and will select the item matching with the index from the array.
{{place.name}}
</ion-option>
</ion-select>
</ion-item>
in ts:
selectedIndex: number;
constructor(){
this.selectedIndex = localStorage.getItem('index') //
}
I solved
the ion-select have the two way data binding with placeId
<ion-select multiple="false" placeholder="Which Place?" [(ngModel)]="placeId" (ionChange)="showPlace()">
<ion-option class="text-input place-field" *ngFor="let place of places | async;" value="{{place.id}}">
{{place.name}}
</ion-option>
</ion-select>
After Selecting a place I saved the id of the selected place inside the local storage. Then on the ionViewDidLoad() I init the the variable this.placeId
this.placeId = localstorage.getItem('foo')
and it work properly.
My mistake: I was saving the name of the place in the localstorage and not the id of the place
Thanks
In ionic 4
ionViewDidEnter() {
let parametros = this.navParams.get('parametros');
this.cuentaBancaria.empresa._id = parametros.empresa;
this.cuentaBancaria.moneda._id = parametros.moneda;
}
Related
What I want to do is to show the first row in HTML (in below code) only on a condition using *ngIf. I've done the basic validation, but I have a hard time and can't find how I can do *ngIf accessing an object key directly (and not using an *ngFor before, on a different element). What I want to do is to show the row only when object.key === 'specific value'. I've tried options with "keys", "Object" but nothing seems to work. If you guys have any suggestion I would appreciate it.
my HTML
<ion-grid *ngIf="!isLoading && loadedBio.length > 0">
<ng-container *ngFor="let bio of loadedBio">
<ng-container *ngIf="bio.category === '1'">
<ion-row class="ion-padding">
<ion-col>
<ion-list class="no-last-border">
<ion-item
detail="false"
*ngIf="bio.category === '1'">
<ion-label
[ngStyle]="{
color:
bio.category === '1'
? '#A34F83'
: 'var(--ion-color-secondary)'
}"
class="bioLabel"
>{{bio.friendlyName}}</ion-label>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</ng-container>
</ng-container>
my JS object I want to access the key from has the following format
loadedBio = [{key: value, key2: value}]
If you need to display each of the properties on your found object, then, in your component, you'd want to convert that found object to an array first - where each element in the array represents a property on the object, and then iterate over it to display each element:
const obj1 = {
a: 'a title',
b: 42,
c: false
};
const arr1 = Object.values(obj1);
Put together that would look like this. First, in the component:
import { Component } from '#angular/core';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
loadedBio = [{'key': 1, 'key2': 2}, {'key': 3, 'key2': 4}];
obj1 = {
a: 'a title',
b: 42,
c: false
};
arr1 = Object.values(this.obj1);
constructor() {
}
}
And in the view:
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ng-container *ngIf="loadedBio">
<ion-list *ngFor="let bio of loadedBio">
<ng-container *ngIf="bio.key === 1">
<ion-item>{{bio.key}}</ion-item>
</ng-container>
</ion-list>
</ng-container>
<ng-container *ngFor="let e of arr1">
<ion-item>{{e}}</ion-item>
</ng-container>
</ion-content>
This should give you an idea of how to handle your situation. Sounds like you want to transform the data in your component, and then iterate over it need be in your view.
Here is a link to the working StackBlitz:
Best to filter your array in the controller, like this
this.filteredBio = this.loadedBio.filter(el=>el.category === 'overall')
and then simply use *ngFor on this filteredBio array. This will only have the objects whose category is 'overall'
Another solution is to implement a custom pipe Refer this
Context : I have a list of courses followed by the authenticated user. I want to be able to show all the courses's title in the ion-title header, and show the details relative to the selected course in the page content.
The problem : I want to show only the current course name in ion-title without adding a label, and show the label "Courses List" only on the select modal header.I tried to remove the ion-label,but the select modal displayed without title..
This is what I want :
The header :
The select modal :
This is what I get :
When keeping ion-label
When removing ion-label
My code :
<ion-item lines="none">
<ion-label>Courses List</ion-label>
<ion-select [(ngModel)]="course.id" (ionChange)="selectcourse($event)">
<ion-select-option *ngFor="let course of courses" [value]="course.id">
{{course.title}}
</ion-select-option>
</ion-select>
</ion-item>
A quick workaround is to set display:none to the ion-label and a placeholder with Course List to achieve what you are looking for.
<ion-item lines="none">
<ion-label style="display:none">Courses List</ion-label>
<ion-select placeholder="Courses List" (ionChange)="selectcourse($event)">
<ion-select-option *ngFor="let course of courses" [value]="course.id">
{{course.title}}
</ion-select-option>
</ion-select>
</ion-item>
First Time:
When one item selected:
You can use the default properties provided by ionic.
In your ts file, declare this:
import { Component } from '#angular/core';
#Component({
selector: 'app-CoursesComponent',
templateUrl: 'CoursesComponent.html',
styleUrls: ['./CoursesComponent.scss'],
})
export class CoursesComponent {
customActionSheetOptions: any = {
header: 'Courses List',
};
}
And then, in your html file:
<ion-item>
<ion-select placeholder="Select" [interfaceOptions]="customActionSheetOptions">
<ion-select-option value="Course1">Course 1</ion-select-option>
<ion-select-option value="Course2">Course 2</ion-select-option>
<ion-select-option value="Course3">Course 3</ion-select-option>
</ion-select>
</ion-item>
I have found out if you put the ion-label inside the selector tags and make the label 'fixed' using the position property with ionic labels it will do the job.
<ion-item lines="none">
<ion-select [(ngModel)]="course.id" (ionChange)="selectcourse($event)">
<ion-label position="fixed">Courses List</ion-label>
<ion-select-option *ngFor="let course of courses" [value]="course.id">
{{course.title}}
</ion-select-option>
</ion-select>
</ion-item>
I am facing a problem while using ionic 4, and ionic-select.
The problem comes when i have some data and i am binding the data.
In this example, i already have a pre selected data, when i do so the ion-select does not render properly.
<ion-item class="transparent">
<ion-label position='floating'>{{ 'GENDER_TXT' | translate}}</ion-label>
<ion-select interface="popover" color='medium' [(ngModel)]='Gender' formControlName='genderTxt'>
<ion-select-option *ngFor='let g of Genders' [value]='g'>{{g.GENDER}}</ion-select-option>
</ion-select>
</ion-item>
this.getGendersSub = this.proxy.Get_Genders(param).subscribe((data) => {
this.Genders = data;
this.Gender = this.Genders[0];
});
Demo Image
remove the ngmodel directive[(ngModel)]='Gender' when you use reactive form , you can set the selected value by update form control genderTxt value
this.getGendersSub = this.proxy.Get_Genders(param).subscribe((data) => {
this.Genders = data;
this.form.get(`genderTxt`).setValue(this.Genders[0]);
});
demo 🚀🚀
I am currently using ionic 4 and I am using the ion-select and ion-select-option tags in html side.
After looking at the documentations when I try to use the selected=true in the ion-select-option it doesn't default to that option chosen. Is there anything that i'm missing or doing wrong?
Here is how my code looks like in the HTML side. I only binded the ngModel on the ts side and nothing else
<ion-select class="ion-select" [(ngModel)]="dialCode">
<ion-select-option value="+1" selected=true>+1</ion-select-option>
<ion-select-option value="+852">+852</ion-select-option>
<ion-select-option value="+86">+86</ion-select-option>
</ion-select>
The issue is that you're binding the ion-select to the dialCode property
... [(ngModel)]="dialCode" ...
So instead of using selected=true, you need to initialize that property with the value that you want to be shown by default. So in your component you could do something like this for example:
// Angular
import { Component } from "#angular/core";
#Component({
selector: "app-home",
templateUrl: "home.page.html",
styleUrls: ["home.page.scss"]
})
export class HomePage implements OnInit {
public dialCode: string = '+1'; // <--- Initialize the property
constructor(...) {}
// ...
}
And then in the view:
<ion-select class="ion-select" [(ngModel)]="dialCode">
<ion-select-option value="+1">+1</ion-select-option>
<ion-select-option value="+852">+852</ion-select-option>
<ion-select-option value="+86">+86</ion-select-option>
</ion-select>
How to access selected choice in dynamically created ionic list ionic2 ?
This is part of my .ts file where routeArr is my array , in which i am retrieving data from firebase-
this.routeList.subscribe(snapshots => {
snapshots.forEach(snapshot => {
var valueToPush = new Array();
valueToPush[0] = snapshot.key;
valueToPush[1] = snapshot.val();
this.routeArr.push(valueToPush);
console.log(this.routeArr[i]);
console.log("Array Length = ",this.routeArr.length); // See the length of the array growing
i=i+1;
});
});
This is part of my HTML file where i am displaying radio buttons dynamically
<ion-item>
<ion-label floating>Trip</ion-label>
<ion-select [(ngModel)]="routeArr">
<ion-option *ngFor="let route of routeArr" value="{{route[0]}}">{{route[0]}}
</ion-option>
</ion-select>
</ion-item>