PeterStaev / NativeScript-Drop-Down

A NativeScript DropDown widget.
Apache License 2.0
105 stars 65 forks source link

Accessing array of objects? #108

Closed joey0xx closed 7 years ago

joey0xx commented 7 years ago

Is there a way to access a property from an array of objects? For example I have this array:

let days = [
    {day: "Sunday", visible: true},
    {day: "Monday", visible: true},
    {day: "Tuesday", visible: true},
    {day: "Wednesday", visible: true},
    {day: "Thursday", visible: true},
    {day: "Friday", visible: true},
    {day: "Saturday", visible: true}
];

and on the dropdown I want to access the day property. Ex: <DropDown [items]="days.day"></DropDown>

I tried using ngFor:

<StackLayout *ngFor="let day of days">
     <DropDown [items]="day.day"></DropDown>
</StackLayout>

but I get an error saying cannot read property 'day' of undefined...

PeterStaev commented 7 years ago

Hey @joey0xx , you cant bind the drop down to a custom array. The supported binding sources for the items are the ObservableArray and the plugin's ValueList. So you need to map your array to one of those.

joey0xx commented 7 years ago

@PeterStaev thank you, it was in the documentation I should've made the association to what I was trying to do... I still have a problem because Im trying to use a custom pipe to filter the dropdown by the value property.

export class VisibilityPipe implements PipeTransform {
    transform(items: ValueList<string>) {
        return items.filter(item => item.value === "true");
    }
}

Its sort of working but its only showing [object Object] instead of the display property. I tried doing it like this :

export class VisibilityPipe implements PipeTransform {
    transform(items: ValueList<string>): ValueList<any> {
        return items.filter(item => item.value === "true");
    }
}

But I get an error saying Propert getDisplay is missing in type ValueItem<string>. My guess its because the pipe is returning Array instead of ValueList. Is it possible to make what Im trying to do work?

PeterStaev commented 7 years ago

@joey0xx , you cannot return an array of ValueItem. You need to use the ValueList class. So in your pipes from the filtered array you need to create a ValueList instance using the provided constructor or push() method.

joey0xx commented 7 years ago

@PeterStaev Thanks, while it didn't work 100%, with the pipe because that only filtered once when the drop down loads, what I did is filter it every time it closes/opens/loads, push to ValueList and then assign it to items.