Ionic change text displayed on ion-select when an option is selected - ionic-framework

I don't know if you get the idea. What I want to do is having an ion-select with option, and when user selects, for example, "Albania (+355)" and press OK button, in the select will only be displayed "+355" instead of "Albania (+355)"
add-contact.html
<ion-select [(ngModel)]="selected_value" (ionChange)="getCountry(selected_value)" placeholder="PaĆ­s">
<ion-option [value]="countries" *ngFor="let countries of country">
{{countries.country_name}} ({{countries.dialling_code}})
</ion-option>
</ion-select>
add-contact.ts
country = [{ "country_code": "AL", "country_name": "Albania","dialling_code": "+355" },
{ "country_code": "DZ", "country_name": "Algeria", "dialling_code":"+213" }];
This is what I have:
name-code-displayed
This is what I try to display: only-code-displayed

According to the Documentation,
ion-select has an input property selectedText which:
The text to display instead of the selected option's value.
You can try using [selectedText]="selected_value.dialing_code".
<ion-select [(ngModel)]="selected_value" (ionChange)="getCountry(selected_value)" [selectedText]="selected_value.dialing_code">
<ion-option [value]="countries" *ngFor="let countries of country">
{{countries.country_name}} ({{countries.dialing_code}})
</ion-option>
</ion-select>

I could also use the statement of the ionChange event as follows:
(ionChange)="getCountry($event)"
In this way, the component captures the value that is selected in the select to then process the information you want to display.

Related

how to show the selected value in radio button as checked when opening the ion-lost again after selection in ionic

I have a field which opens a list having ion-radio.On selection of an option it shows the selected value as checked and when i open the list again, the checked value is not shown.
here is my code:
code to show the options in modal controller :
let modal = this.modalCtrl.create(ListComponent, { selectEmpType: type, selectValue: value, customiseColor: this.customiseColor , formMrType :formMrType, limitedRoleList : this.limitedRoleList, formType:this.formType,defaultOU1:this.defaultOus[0],defaultOU2:this.defaultOus[1],defaultOU3:this.defaultOus[2]});
modal.onDidDismiss(data => {
if (data.type == 'single') {
this.setEmpValue(data.data, name); //data.data is the value that is selected from the list
}
}
in listcomponent.html:
<div *ngIf= "formMrType =='employee'">
<ion-list radio-group [(ngModel)]="relationship">
<ion-item *ngFor="let option of inputDatas">
<ion-label>{{option.EMPFullName}}</ion-label>
<ion-radio [checked]="option.checked" value="{{option.EMPFullName}}"></ion-radio>
</ion-item>
</ion-list>
</div>
how to show the selected option as checked when opening the list for second time.
Preferably, you should be using ion-select for such functionality..
If you are using latest ionic versions ion-radio-group
But even in your case..you can try something like this...
<ion-radio [checked]="option.checked" value="{{option.EMPFullName}}" (ionBlur)="optionBlur(option)"></ion-radio>
optionBlur(option){
if(!option['checked']){
option['checked'] = true;
}
else{
option['checked'] = !option['checked']
}
}

Ionic 4: How to update or refresh a ion-select from script?

I created an app with a register form in ionic 4 and angular. I want to do the following:
I have two ion-selects, one is for a countries list and that another is for phone codes list. The behavior i am looking for is: when a country is selected in the ion-select for countries, the corresponding telephone code must be selected automatically in the phone code ion-select.
This is my code:
ion-select for countries
<ion-select id="paisselect" formControlName="pais" (ionChange)="changeselect($event)" interface="popover">
<ion-select-option *ngFor="let c of countries" value="{{c.cod}}">{{c.name}}</ion-select-option>
</ion-select>
ion-select for phone code
<ion-select formControlName="phonecode" placeholder="Cod." interface="popover">
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
</ion-select>
Here is the function i am using to update the phone code ion-select
changeselect($event){
console.log($event.target.value) ;
let item1 = this.countries.find(i => i.cod === $event.target.value);
console.log(item1) ;
this.codigoselected = item1.code;
console.log(this.codigoselected) ;
}
I am using the variable "codigoselected" to determine which country is selected. for this, in the ion-select-option i am using a condition to change the selected status of the option like this:
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
The current behavior is like this:
I selected a country:
But the ion-select for phone codes seems not to be updated
But when I click on the field, the specific code is selected
I need to do clic under option is selected in order to field be refreshed:
So, how can I make the field for phone codes update automatically in the interface without the user clicking?
Try this, pass the phone code select element to your changeselect(event, phoneCodeSelect) function.
<ion-select id="paisselect" formControlName="pais" (ionChange)="changeselect($event, phoneCodeSelect)" interface="popover">
<ion-select-option *ngFor="let c of countries" value="{{c.cod}}">{{c.name}}</ion-select-option>
</ion-select>
Add #phoneCodeSelect to your phone code select element.
<ion-select #phoneCodeSelect formControlName="phonecode" placeholder="Cod." interface="popover">
<ion-select-option *ngFor="let c of codetele" value="{{c.code}}" selected="{{codigoselected == c.code}}">{{c.code}}</ion-select-option>
</ion-select>
Then in your function change the value of the select element manually, while also updating the phoneCode form control value.
changeselect($event, phoneCodeSelect){
console.log($event.target.value) ;
let item1 = this.countries.find(i => i.cod === $event.target.value);
console.log(item1) ;
this.yourFormGroup.value.phonecode = item1.code; // change yourFormGroup to what your variable's name is
phoneCodeSelect.value = item1.code;
}

Ionic (ionChange) get selected index

So I know how to get the selected value using the ionChange event, but how do I get the selected index. For example if i was to select the 3rd value down in a dropdown how can I get the selected index (2) rather than the value selected?
You can do something like ths:-
<ion-select placeholder="Select One">
<ion-select-option value="id1">Value1</ion-select-option>
<ion-select-option value="id2">Value2</ion-select-option>
</ion-select>
By this when you will select any particular option, then on its change event you will get that object's id instead of the value in the controller.
I hope it helps.
This is what you can do to get the id. Add a local variable to your ion-select #selectedIndex. After add your value like this [value]=[prices.w, id]
<ion-select slot="end" (ionChange)="changePrice($event)" #selectIndex>
<ion-select-option text-uppercase [value]="[prices.w, id]" *ngFor="let prices of prodData?.prices; let id = index" >{{prices.w}}</ion-select-option>
</ion-select>
In your ts. Import ViewChild
import { Component, OnInit, ViewChild } from '#angular/core';
After reference your local variable
#ViewChild('selectIndex') selectedId: ElementRef;
then declare your ionChange() function changePrice(event) and add the following code
public changePrice(event) {
const childCount = this.selectedId['el']['childElementCount'];
this.selectedId['el']['childNodes'][`${childCount}`]['defaultValue'] = event.detail.value[0];
this.selectedId['el']['shadowRoot']['children'][1]['innerHTML'] = event.detail.value[0];
this.selectedId['el']['shadowRoot']['children'][1]['innerText'] = event.detail.value[0];
console.log(event.detail.value[1]);
}
logs your index
console.log(event.detail.value[1]);
Hope it works fine

formbuilder select value as object

I have a form in which I have a multiple select field. Options for this select field are populated from received data e.g. Tasks objects.
[
{"id":7,"title":"Seven","project":1},
{"id":8,"title":"Eight","project":2},
{"id":9,"title":"Nine","project":2}
]
and my select field:
<ion-list>
<ion-item>
<ion-label>Select tasks</ion-label>
<ion-select formControlName="tasks" multiple="true" (ionChange)="arrangeSelectedTasks()">
<ion-option *ngFor="let task of tasks" value={{task}}>{{task.title}}</ion-option>
</ion-select>
</ion-item>
</ion-list>
the function
arrangeSelectedTasks(){
for(let task of this.addScenarioForm.value.tasks){
this.selectedTasks.push(task)
}
console.log("selectedTasks: ", this.selectedTasks)
}
expects an array of task objects
Where value={{task.id}} passes ["7", "8"] and value={{task}} passes ["[object Object]", "[object Object]"]
This results in my backend throwing BAD REQUEST error. I want to pass something like [{..},{..}] this from value.
How to achieve this?
Use it .map
selectedTasks:any = [];
arrangeSelectedTasks(){
this.addScenarioForm.value.tasks.map(task => {
this.selectedTasks.push({task.id});
});
console.log("selectedTasks: ", this.selectedTasks)
}

Ionic 3 Navigation New page hidden

I am using ionic 3.10.3 my root page is loginPage then after user validation its redirect to HomeViewPage by as bellow
this.nav.setRoot(HomeViewPage);
no problem to now but in the HomeViewPage If the user click add button it must be redirect to NewItemPage I tried to use
this.nav.setRoot(NewItemPage, {itemID: _id, isNew: _isNew});
and
this.nav.push(NewItemPage, {itemID: _id, isNew: _isNew});
bellow is the new function
newItem(){
this.events.publish('apps:viewItem', this.handlerService.getNewID(), "1");
}
and this is the code in app.component.ts the receive event
this.events.subscribe('apps:viewItem', (_id, _isNew) => {
this.menu.close();
this.rootPage = NewItemPage;
this.nav.setRoot(NewItemPage, {itemID: _id, isNew: _isNew}).then(()=>{
this.nav.popToRoot();
}).catch(err=>{
alert("NewItemPage Error : " + err.toString());
});
});
but still in the HomeViewPage even i clicked the button many times
but when I click back button it return to NewItemPage but now data exists.
Any help
The problem was in NewItemPage the I use a null object property
<ion-item>
<ion-label>Activity Type</ion-label>
<ion-select [(ngModel)]="handlerService.dataService.item.activityType">
<ion-option *ngFor="let g of handlerService.dataService.activityTypes" value="{{g.id}}">{{ g.name }}</ion-option>
</ion-select>
</ion-item>
handlerService.dataService.item is an object that must be initialized