Reputation: 35
i am loading a page in ionic with values fetching from php, Eg. edit profile page.
Here i am using an <ion-select multiple="true">
item. Here when user selects multiple values then in php side i am ready the array object and storing the values of selected items as "Delhi,Koltata,Mumbai".
Now the challenge i am facing is how to show those values as selected when the ionic page is loaded. I am using like --
<ion-select multiple="true" value="{{vcity}}" placeholder="Select city" name="city" [(ngModel)]="postData.city">
But when the <ion-select>
is loading it's showing blank, that i understood for sure as values of vcity here is "Delhi,Koltata,Mumbai" which is not readable to <ion-select value="{{vcity}}">
So how i can make this thing happen. Any idea for startup.
THank you
This is my current code, here the problem is that the values are not getting selected when the page loads --
<ion-select multiple="true" placeholder="Select Caste" name="caste" [(ngModel)]="postData.caste">
<ion-select-option *ngFor="let caste of casteData" value="{{caste.name}}" selected='caste.selected ? "selected" : "" '>{{caste.name}}</ion-select-option>
</ion-select>
Upvotes: 0
Views: 816
Reputation: 35
So, finally i got my solution. I want to thank @Mostafa Harb, i am up-voting your answer for the help, for guiding and helping me, using your codes and with some on tweaking i finally got what i wanted. Here is what i have done .
this.casteData = [];
const splitValues = this.vcaste.split(',');
for (const data of splitValues) {
this.casteData.push(data);
}
<ion-select multiple="true" [value]="casteData" placeholder="Select Caste" name="caste" [(ngModel)]="postData.caste">......</ion-select>
Here in "casteData" array the selected values are present which is supplied to the ion-select
as [value]="casteData"
Upvotes: 0
Reputation: 1675
Inside you ts file declare new varaible example :
cities = [];
then inside ngOnInit() or the place your getting the vcity value from... use:
const splitValues = vcity.split(',');
for(let i =0;i<splitValues.length;i++) {
cities.push({name : splitValues[i]});
}
Also Edited In ion-select just edit ngmodel="splitedValues" And Inside ion-select add :
<ion-select-option *ngFor="let city of cities">{{city.name}}</ion-select-option>
Edited Part : According to you edits in variable names:
Upvotes: 1