The reason these dropdowns are blank is because PrimeNg required a model for all dropdowns. In your component, the model looks like this, for example:
this.dropdownContent = [
{ label: 'label1', value: 'content1' },
{ label: 'label2', value: 'content2' },
...
];
Because this is the case, more than likely your [options]="field.LookupVal.split('|')"
does not satisfy the model requirements for the dropdown component.
First, I would check inside your component if you can manually use the data and put it inside the model so that every value has a label and value. If this doesn't work, then there are 2 other ways I would recommend solving this.
1 Map (easiest way)
By mapping the model inside your component.
When the data comes in from the api, you should map it to make the dropdown. Something like this, based on the data from the dropdown sample you gave. Note, this would be either in your subscription, observable, ngOnInit, or somewhere else. just wherever you are getting and working with the data...:
this.options = this.apiData.LookupVal.split('|')".map((o) => ({
label: o,
value: o,
}));
Now, in your html, change your options to [options]="options"
, or whatever you named this inside your component. What is going on here, is you are mapping, or changing the data to fit the model requirements. So if the field.LookupVal.split('|')
or whatever your component's data is had a value of foo
, it would now have a value of { label: 'foo', value: 'foo' }
.
2 Pipe (more complex, only if necessary or you're doing this a lot of times)
If this does not work, or you can't get it to, the next option would be to create a pipe. You would want the pipe to convert each item inside the array to have a label and value, just like you did with the map. Something like this (it may need to be changed to your specific case):
import { Pipe, PipeTransform } from '@angular/core';
import { SelectItem } from 'primeng/api';
@Pipe({ name: 'dropdownPipe' })
export class DropdownPipe implements PipeTransform {
@param array
@param labelKey
transform(array: any[], labelKey: string | string[]): SelectItem[] {
if(!labelKey || !array) {
return undefined;
}
let pipeArray;
if (labelKey instanceof Array) {
for (let o = 2; o < labelKey.length; o++) {
pipeArray = array.map((val, i) => ({
label: pipeArray[i].label + val[labelKey[o]],
value: val
}));
}
return pipeArray;
}
Then, in your html, you can add the following (make sure the pipe is in your app or component module)
<p-dropdown
*ngIf="field.IsLookup == 'Y'"
[options]="field.LookupVal.split('|') | dropdownPipe"
[id]="field?.ColumnName"
[formControlName]="field?.ColumnName"
[showClear]="true">
</p-dropdown>