I have 3 dropdowns for country,state,city in angular. I have used ng-select module for those dowpdowns from reference here. On country change states populates, and on state change city populate.
template HTML
<ng-select formControlName="country" (change)="onChangeCountry($event)" >
<ng-option value="dbCountryId ? dbCountryId : ''">{{dbCountryName ? dbCountryName : 'Select Country' }}</ng-option>
<ng-option *ngFor="let country of countryInfo" [value]="country.id">{{country.name}}</ng-option>
</ng-select>
<ng-select formControlName="state" (change)="onChangeState($event)">
<ng-option value="dbStateId ? dbStateId : ''">{{dbStateName ? dbStateName : 'Select State' }}</ng-option>
<ng-option *ngFor="let state of stateInfo" [value]="state.id">{{state.name}}</ng-option>
</ng-select>
<ng-select formControlName="city" >
<ng-option value="dbCityId ? dbCityId : ''">{{dbCityName ? dbCityName : 'Select City' }}</ng-option>
<ng-option *ngFor="let city of cityInfo" [value]="city.id">{{city.name}}</ng-option>
</ng-select>
ts code
this.userService.getUserDetails(userDetails.id).subscribe((results) => {
if (results['status'] === true) {
this.dbCountryName = results.data.country ? results.data.country : null;
this.dbCountryId = results.data.country_id
? results.data.country_id
: null;
this.dbStateName = results.data.state ? results.data.state : null;
this.dbStateId = results.data.state_id
? results.data.state_id
: null;
this.dbCityName = results.data.city ? results.data.city : null;
this.dbCityId = results.data.city_id ? results.data.city_id : null;
this.form.patchValue({
country:
results.data.country_id === null ? '' : results.data.country_id,
state:
results.data.state_id === null ? '' : results.data.state_id,
city: results.data.city_id === null ? '' : results.data.city_id,
});
}
});
I am using same form for add and edit data. I am storing id of country,state, city. In api response I get stored id, name of fields. I have patched id with respective form control.
I have 2 problems.
- 'Select country/state/city' like default text , it shows in dropdown not in inputbox
- I am not able to show fetched data properly. its showing like below
How I can solve these problems with ng-select in angular? please help and guide. Thanks.
Edit
Template code
<div class="col-sm-6">
<div class="form-group">
<label for="country">Country <b style="color: red">*</b></label><ng-select formControlName="country" (change)="onChangeCountry($event)" [ngClass]="{ 'error_border': submitted && f.country.errors }">
<ng-option *ngFor="let country of countryInfo" [value]="country.id">{{country.name}}</ng-option>
</ng-select>
<div *ngIf="submitted && f.country.errors" class="text-danger">
<div *ngIf="f.country.errors.required">Country is required</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="state">State <b style="color: red">*</b></label>
<ng-select formControlName="state" [ngClass]="{ 'error_border': submitted && f.state.errors }" (change)="onChangeState($event)">
<ng-option *ngFor="let state of stateInfo" [value]="state.id">{{state.name}}</ng-option>
</ng-select>
<div *ngIf="submitted && f.state.errors" class="text-danger">
<div *ngIf="f.state.errors.required">State is required</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="city">City <b style="color: red">*</b></label>
<ng-select formControlName="city" [ngClass]="{ 'error_border': submitted && f.city.errors }">
<ng-option *ngFor="let city of cityInfo" [value]="city.id">{{city.name}}</ng-option>
</ng-select>
<div *ngIf="submitted && f.city.errors" class="text-danger">
<div *ngIf="f.city.errors.required">City is required</div>
</div>
</div>
</div>
ts code
export class EditProfileComponent implements OnInit {
stateInfo: any[] = [];
countryInfo: any[] = [];
cityInfo: any[] = [];
dbCountryName = '';
dbCountryId = 0;
dbStateName = '';
dbStateId = 0;
dbCityName = '';
dbCityId = 0;
ngOnInit() {
this.form = this.formBuilder.group({
country: ['Select Country', Validators.required],
state: ['Select State', Validators.required],
city: ['Select City', Validators.required],
});
this.userService.getUserDetails(userDetails.id).subscribe((results) => {
if (results['status'] === true) {
this.dbStateName = results.data.state ? results.data.state : null;
this.dbStateId = results.data.state_id
? results.data.state_id
: null;
this.dbCityName = results.data.city ? results.data.city : null;
this.dbCityId = results.data.city_id ? results.data.city_id : null;
this.dbCountryName = results.data.country ? results.data.country : null;
this.dbCountryId = results.data.country_id
? results.data.country_id
: null;
this.cscService.getCountries().subscribe((result) => {
this.countryInfo = result.data;
this.form.patchValue({
country: this.dbCountryId
});
});
this.cscService.getStates(this.dbCountryId).subscribe((result) => {
this.stateInfo = result.data;
this.form.patchValue({
state: this.dbStateId
});
});
this.cscService
.getCities(this.dbStateId)
.subscribe((result) => {
this.cityInfo = result.data;
this.form.patchValue({
city: this.dbCityId
});
}
);
this.form.patchValue({
// country:
// results.data.country_id === null ? 'Select Country' : results.data.country_id,
// state:
// results.data.state_id === null ? 'Select State' : results.data.state_id,
// city: results.data.city_id === null ? 'Select City' : results.data.city_id,
});
}
});
}
getCountries() {
this.cscService.getCountries().subscribe((result) => {
this.countryInfo = result.data;
});
}
onChangeCountry(countryId: number) {
if (countryId) {
this.cscService.getStates(countryId).subscribe((result) => {
this.stateInfo = result.data;
this.cityInfo = null;
});
this.form.patchValue({
state: "Select State",
city: "Select City"
});
} else {
this.stateInfo = null;
this.cityInfo = null;
}
}
onChangeState(stateId: number) {
if (stateId) {
this.cscService
.getCities(stateId)
.subscribe((result) => (this.cityInfo = result.data));
this.form.patchValue({ city: "Select City" });
} else {
this.cityInfo = null;
}
}
}
country data response
state data response - gets on country select (I have selected country id =1)
city data response - get on state select (I have selected state id =42)