简体   繁体   English

Angular 7 ngFor 中的对象数组仅显示具有特定 object 属性值的数据

[英]Angular 7 Array of Objects in ngFor show only data with specific object property value

I have a following problem with showing and saving data to array of objects.我在将数据显示和保存到对象数组时遇到以下问题。 I have a form with 5 tabs.我有一个带有 5 个标签的表格。 I can switch between those tabs and show different data in table.我可以在这些选项卡之间切换并在表格中显示不同的数据。 I have also a possibility to add some new data to specific tab table.我还可以将一些新数据添加到特定的选项卡表中。 I have a form which is called "Additional data form".我有一个称为“附加数据表格”的表格。 Form is working fine, once I add some data to this form and click on submit, I'm pushing object with those data to my array which is called arrayData .表单工作正常,一旦我将一些数据添加到此表单并单击提交,我就会将 object 与这些数据一起推送到我的数组中,该数组称为arrayData I'm using *ngFor for showing data: <tr class="body-row" *ngFor="let position of arrayData; index as i;">我使用 *ngFor 来显示数据: <tr class="body-row" *ngFor="let position of arrayData; index as i;">

The problem is, I'm pushing those objects to array which is global (and it works) but I would like to show those data only on specific tab table (I would also like to add new form on specific tab).问题是,我将这些对象推送到全局数组(并且它有效),但我想仅在特定选项卡表上显示这些数据(我还想在特定选项卡上添加新表单)。 What I would like to achieve is to push those data to array but in *ngFor directive, I would like to show only data with specific zone index.我想要实现的是将这些数据推送到数组中,但在 *ngFor 指令中,我只想显示具有特定区域索引的数据。 What I do is, I'm getting tab index for example first tab is with index 0, second tab is with index 1 etc. Next, I'm checking in my code with switch case if selectedTabIndex is for example 0, and I'm pushing this value to my object property "zone" so it will look like zone: 0. In this case I have an object which looks like this: {zone: 0, data1: "33", data2: "4", data3: "5", data4: true} If I push data on tab with index 2, I'm setting this information to my object so zone:2 will be added to my next object.我所做的是,我正在获取选项卡索引,例如第一个选项卡的索引为 0,第二个选项卡的索引为 1,等等。接下来,如果 selectedTabIndex 为例如 0,我将使用 switch case 检查我的代码,并且我m 将此值推送到我的 object 属性“zone”,因此它看起来像 zone:0。在这种情况下,我有一个 object,看起来像这样: {zone: 0, data1: "33", data2: "4", data3: "5", data4: true}如果我在索引为 2 的选项卡上推送数据,我将此信息设置为我的 object,因此zone:2将添加到我的下一个 object。 My array looks like this:我的数组如下所示:

[
0: {zone: 1, data1: 1, data2: 2},
1: {zone: 1, data1: 23, data2: 33},
2: {zone: 2, data1: 33, data2: 44}
]

etc. depending on witch tab I posted my form.等等,取决于我发布了我的表格的女巫标签。

Is there a quick solution for showing data on tab index 0, objects with zone:0 value, on tab with index 1 zone:1 value etc.?是否有快速的解决方案来显示选项卡索引 0、具有zone:0值的对象、具有索引 1 zone:1值等的选项卡上的数据? Maybe I should use separate arrays of objects for specific zones?也许我应该为特定区域使用单独的 arrays 对象? Thank you.谢谢你。

=You can filter your array if I understand your problem well. =如果我很好地理解您的问题,您可以过滤您的数组。

getData(){
    return arrayData.filter(d => d.zone === this.selectedZone);
}

Use this method in ngfor.在 ngfor 中使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM