简体   繁体   English

使用 Angular 8 在 NgFor 循环中将项目标记为更新

[英]Mark item as updated in NgFor loop using Angular 8

I have two sets of tables looping the same data.我有两组循环相同数据的表。

Table 1 are the active items表 1是活动项目

Table 2 are the inactive items表2为非活动项

When the user selects an item, I'd like to mark the item as updated by displaying a "Pending" indicator or badge (ex: user selects checkbox to make the item active).当用户选择一个项目时,我想通过显示“待处理”指示器或徽章来将该项目标记为已更新(例如:用户选择复选框以使该项目处于活动状态)。

However, I'm having difficulty creating a unique identifer for each item in case the user reverts to their original selection (ex: back to inactive).但是,我很难为每个项目创建一个唯一的标识符,以防用户恢复到原始选择(例如:回到非活动状态)。 I'd like the user to visualize their updated items before submitting.我希望用户在提交之前可视化他们更新的项目。

page.component.html页面.component.html

<h1>Active</h1>
<table>
    <thead>...</thead>
    <tbody>
        <tr *ngFor="let selectedItemof itemDetailsInfo.getSelectedItems()">
            <td class="text-center">
                <span class="custom-checkbox">
                    <input type="checkbox"
                           id="{{selectedItem.id}}"
                           value="{{selectedItem.id}}"
                           (change)="itemDetailsInfo.getSelectedItems(); isUpdated(selectedItem.id)"
                           [(ngModel)]="selectedItem.selected" />
                    <label for="{{selectedItem.id}}">
                        <svg class="icon icon-20 green">
                            <use xlink:href="assets/images/icons.svg#circle-check" />
                        </svg>
                    </label>
                </span>
            </td>
            <td>
                {{selectedItem.displayName}}
                <span *ngIf="itemId.itemPending" class="badge badge-pill">Pending</span>
            </td>
        </tr>
    </tbody>
</table>

<h1>Inactive</h1>
<table>
    <thead>...</thead>
    <tbody>
        <tr *ngFor="let unSelectedItem of itemDetailsInfo.getUnSelectedItems()">
            <td class="text-center">
                <span class="custom-checkbox">
                    <input type="checkbox"
                           id="{{unSelectedItem.id}}"
                           value="{{unSelectedItem.id}}"
                           (change)="itemDetailsInfo.getUnSelectedItems(); isUpdated(unSelectedItem.id)"
                           [(ngModel)]="unSelectedItem.selected" />
                    <label for="{{unSelectedItem.id}}">
                        <svg class="icon icon-20 green">
                            <use xlink:href="assets/images/icons.svg#circle-check" />
                        </svg>
                    </label>
                </span>
            </td>
            <td>
                {{unSelectedItem.displayName}}
                <span *ngIf="itemId.itemPending" class="badge badge-pill">Pending</span>
            </td>
        </tr>
    </tbody>
</table>

page.component.ts page.component.ts

pending: boolean = false;
itemId: any;

...

isUpdated(id: any) {
  let itemId = id;
  itemId.itemPending = !itemId.itemPending;
}

I have a feeling I'm making this more difficult than it needs to be.我有一种感觉,我让这件事变得比需要的更困难。

What you have to do is to introduce the itemIsPending property to the global list on which you are using the ngfor.您需要做的是将 itemIsPending 属性引入到您正在使用 ngfor 的全局列表中。 And convert the if that if the item.pending display that badge.并转换 if that if item.pending 显示该徽章。

*ngIf="selectedItem.itemPending" class="badge

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

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