首页 > 解决方案 > 如何使用 Angular 8 中的列规范将任何对象传递到通用概述中?

问题描述

我正在尝试创建一个可以接受任何模型的概述,并在 Angular 中使用来自该模型的数据填充网格。

假设我有以下模型:

export class Incident {
   id: number;
   number: number;
   reportDate: string;
   startDate: string;
   endDate: string;
   vehicleNumber: number;
   licensePlate: string;
   location: string;
   supplier: string;
   pickupLocation: string;
   causeOfFailure: string;
   cause: string;
   descriptionExternal: string;
   status: string;
}

我有一个服务将此数据作为 Json 返回,并且它被映射到事件对象数组。

概述是根据我传递给组件的设置构建的,如下所示:

export class OverviewSettings {
   title: string;
   collapsible: boolean;
   pagination: boolean;
   pageSize: number;
   primaryColumnSpecifications: ColumnSpecifications[];
   rowItems: any[];
}

和 ColumnSpecifications:

export class ColumnSpecifications {
   name: string;
   dataType: number;
   columnSize: string;
}

我将在概述中循环浏览的项目是 any[] 类型,因为我需要它能够处理我传递给它的每个模型。现在我正在传递一系列事件。

我想要实现的目标是能够传递哪一列将显示我的模型中的哪个属性。

我的问题是:如何使此概述可用于所有模型?

标签: angulargeneric-programming

解决方案


你能举一个你想做的例子吗?

泛型类的示例:

class KeyValuePair<T,U>
{ 
    private key: T;
    private val: U;

    setKeyValue(key: T, val: U): void { 
        this.key = key;
        this.val = val;
    }
}
class Department<T> {

   //different types of employees
   private employees:Array<T> = new Array<T>();

   public add(employee: T): void {
      this.employees.push(employee);
   }

}

推荐阅读