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

How to filter a p-orderList with a p-dropdown in Angular

我是 angular 的新手,我一直在努力让不同的部分协同工作。我有一个来自 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);
}

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

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 而不是设备。

<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 代替您分配设备的位置<p-orderList>