【问题标题】:How to copy data of first table to second table using Angular?如何使用 Angular 将第一个表的数据复制到第二个表?
【发布时间】:2020-12-18 14:48:24
【问题描述】:

我只想将第一个表的数据(项目和价格)复制到第二个表,并在单击第一个表(项目)时增加数量(第二个表)。我正在开发 Angular 9

第一桌:

Code | Item  | Price | Unit  |
1    | Mouse | 500   | Piece |
2    | Wire  | 100   | Piece |

第二张表:

Item | Quantity| Price | 
1    | 2       | 1000  | 
2    | 1       | 100   |

我做了很多工作,但我无法获得任何所需的输出。 可以使用 javaScript/Js/jquery

解决
<table class="table table-hover" id="firstTable">
    <thead class="table-primary">
        <tr>
            <td>Code</td>
            <td>Item</td>
            <td>Unit</td>
            <td>Price</td>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let data of itemList;" id="moveMeIntoMain"
            style="cursor:pointer">
            <td>{{data.itemCode}}</td>
            <td>{{data.itemName}}</td>
            <td><span>{{data.unitId}}</span>{{data.name}}</td>
            <td>{{data.retailRate}}</td>
        </tr>
    </tbody>
</table>


<table class="table table-hover" id="secondTable">
    <thead class="">
        <tr class="table-primary">
            <td>#</td>
            <td>Item</td>
            <td>Price</td>
            <td>Quantity</td>
            <td>Action</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>    
        </tr>
    </tbody>
</table>

Js

$(document).ready(function () {
        var items = [];
        $("#firstTable tr").on("click", function () {
            var newTr = $(this).closest("tr").clone();
            
            $(newButtonHTML).children("button").click(function (e) {
            });
            $(newTr).children("td:last").html("");
            items.push(newTr);
            newTr.appendTo($("#secondTable"));
        });
    })

【问题讨论】:

  • 第一个表是如何构建的?使用角度 *ngFor?
  • 是的! m 使用 *ngFor..
  • 现在我正在努力。我会尽快通知你:-)

标签: javascript jquery angular asp.net-core-webapi


【解决方案1】:

您可以简单地以 Angular 方式尝试 ;)。只需将点击处理程序添加到每一行并发送行数据

工作stackblitz

模板文件

<table>
  <thead>
    <th>Code</th>
    <th>Item</th>
    <th>Price</th>
    <th>Unit</th>
  </thead>
  <tr *ngFor="let data of firstTableData" (click)="updateSecondTable(data)">
    <td>{{ data.Code }}</td>
    <td>{{ data.Item }}</td>
    <td>{{ data.Price }}</td>
    <td>{{ data.Unit }}</td>
  </tr>
</table>
<hr>
<table *ngIf="secondTableData.length">
  <thead>
    <th>Item</th>
    <th>Quantity</th>
    <th>Price</th>
  </thead>
  <tbody>
    <tr *ngFor="let newdata of secondTableData">
      <td>{{ newdata.Item }}</td>
      <td>{{ newdata.Quantity }}</td>
      <td>{{ newdata.Price }}</td>
    </tr>
  </tbody>
</table>

在打字稿文件

  • 声明一个空的secondTableData数组
  • 并且有一个方法,它首先检查第二个表数组中的项目是否存在。如果项目已存在,则只需更新 quantity 和 price
  • 如果项不存在,只需将需要列的项推送到第二个表数组中

Component.ts

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;

  firstTableData = [{
      Code: 1,
      Item: 'Mouse',
      Price: 500,
      Unit: 'Piece'
    },
    {
      Code: 2,
      Item: 'Wire',
      Price: 100,
      Unit: 'Piece'
    },
    {
      Code: 3,
      Item: 'Some Item',
      Price: 300,
      Unit: 'Some Unit'
    }
  ];
  secondTableData = [];

  updateSecondTable(data) {
    let foundItem = this.secondTableData.find((item) => item.Item === data.Item);
    if (foundItem) {
      foundItem.Quantity += 1;
      foundItem.Price += data.Price;
      return;
    }
    this.secondTableData.push({
      Item: data.Item,
      Quantity: 1,
      Price: data.Price,
    })
  }
}

【讨论】:

  • 它工作正常。太感谢了。让我再补充一个问题。点击操作栏中的减号按钮后,如何删除数量并降低价格? :-)
  • @Dany 最好将这个问题作为单独的问题 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多