网站搜索

javascript - Angular 复制表格中的一行


这是我的代码。在我的代码中,我允许用户在点击某行旁边的按钮时创建一个具有相同值的行。我希望他能够创建多个副本。没有 for 循环时,我只能创建一行,但使用 for 循环时,我无法创建多个副本。我的代码缺少什么?我应该怎么做?

copyWorkItem(index: any, row: IWorkItem, event: any) {
        const dialogRef = this._dialog.open(CopyDialogComponent, {
            width: "550px",
            data: row,
        });


        dialogRef.afterClosed().subscribe((result: IWorkItemSelection) => {

             for(let i= 1; i >= result.CopyQuantity; i++){

                let item: IWorkItem = {
                    Product: row.Product,
                    ProductCategory: row.ProductCategory,
                    Version: row.Version,
                    ProductBalance: row.ProductBalance,
                    DeliveryWarehouse: row.DeliveryWarehouse,
                }
    
                if (result.OrderIdCheck == null || result.OrderIdCheck == false) {
                    item.Order = null;
                }
                if(result.StockCheck == null || result.StockCheck == false || result.StockCheck == undefined){
                    item.ProductCategory = null;
                    item.Product = null;
                }
                if (result.VersionCheck == null || result.VersionCheck == false) {
                    item.Version = null;
                }
                if (result.DeliveryWarehouseCheck == null || result.DeliveryWarehouseCheck == false) {
                    item.DeliveryWarehouse = null;
                }
                if (result.QuantityCheck == null || result.QuantityCheck == false) {
                    item.Quantity = null;
                }


               //creates a new row
                let tempData = this.dataSource.data.splice(0);
                tempData.unshift(item);
                this.dataSource = new MatTableDataSource(tempData);
                this.EditIndex = 0;
    
             }
        });   
    }

答案#1

如果你想根据对话框的 CopyQuantity 结果生成一定数量的 HTML 元素,我建议将这些副本写入一个对象数组,然后使用 Angular 的 ngFor 指令在表格中渲染这些元素。