如何在Angular中使用p下拉列表过滤p-orderList

时间:2019-06-17 15:40:38

标签: angular filtering dropdown primeng

我是新手,我一直在努力让不同的作品一起工作。我有一个来自PrimeNG的p-orderList,它显示JSON对象的列表,还有一个p-dropDown,它从listedObjects中读取属性并显示所有可能的选项。我需要过滤orderList,以便在未选择任何内容时显示所有可能的选项,或者过滤它以仅显示所选种类。

我填充了下拉列表,并在更改时触发。我还可以使用内置函数的脚本进行过滤。我不知道怎么做是将其附加到orderList。任何帮助是极大的赞赏。

HTML

<p-dropdown [options]="getExistingTypes()" [(ngModel)]="selectedType" [style]="{'width':'83%'}" (onChange)="onCategoryChange(selectedType)"></p-dropdown>
<div style="display: flex; justify-content: center; flex-direction: row; text-align: center;">
</div>
<p-orderList [value]="devices" [metaKeySelection]="false" [listStyle]="{'height':'400px'}" header="Devices" controlsPosition="right" dragdrop="false" [(selection)]="selected" [responsive]="true">
    <ng-template let-device pTemplate="item">
        <div style="font-size: x-large">
            {{device['object_name'] | noquotes}}
        </div>
        <div>
            <label>mac: </label>{{device.deviceData.MAC}}
        </div>
        <div>
            <label>id: </label>{{device['object_identifier']}}
        </div>
        <div>
            <label>type: </label>{{device['object_type']}}
        </div>
    </ng-template>
</p-orderList> 

TS

onCategoryChange(selectedType){
    var results = this.devices.filter(element => {return element.object_type === selectedType});
    console.log(results);
}

1 个答案:

答案 0 :(得分:0)

您需要将p-orderList指向过滤后的结果,因此必须将结果设为组件上的公共变量:

public filteredDevices: any; //make this an Array<x> of whatever your devices are, sames as this.devices
...

onCategoryChange(selectedType){
    filteredDevices = this.devices.filter(element => {return element.object_type === selectedType});
    console.log(results);
}

HTML大致相同,只是使用了filteredDevices而不是devices。

<p-orderList [value]="filteredDevices" [metaKeySelection]="false" [listStyle]="{'height':'400px'}" header="Devices" controlsPosition="right" dragdrop="false" [(selection)]="selected" [responsive]="true">
    <ng-template let-device pTemplate="item">
        <div style="font-size: x-large">
            {{device['object_name'] | noquotes}}
        </div>
        <div>
            <label>mac: </label>{{device.deviceData.MAC}}
        </div>
        <div>
            <label>id: </label>{{device['object_identifier']}}
        </div>
        <div>
            <label>type: </label>{{device['object_type']}}
        </div>
    </ng-template>
</p-orderList> 

您可能需要对[value]="filteredDevices"进行更多更改,我不确定这是分配<p-orderList>的设备列表的地方,但是请使用filteredDevices代替{ {1}}