我試圖實現模式時,從表中的任何一行點擊「刪除」按鈕,其中它彈出一個模態窗口要求確認。Angular2:刪除選中的表格行點擊模式窗口中的按鈕
當我只是實現刪除沒有模式時,它似乎工作正常,只刪除該特定行,但在使用模式時,它會刪除第一行而不是選定的行。
user.component.html
<h1>{{welcome}}</h1>
<table class="table table-bordered">
<tr>
<th>#</th>
<th>Game</th>
<th>Platform</th>
<th>Release</th>
<th>Actions</th>
</tr>
<tr *ngFor="let game of games | paginate: {itemsPerPage: 5, currentPage:page, id: '1'}; let i = index">
<td>{{i + 1}}</td>
<td>{{game.game}}</td>
<td>{{game.platform}}</td>
<td>{{game.release}}</td>
<td><button data-title="title" data-id="2" data-toggle="modal" data-target="#deleteModal" class="confirm-delete"> Delete</button>
</tr>
</table>
<pagination-controls (pageChange)="page = $event" id="1" maxSize="5" directionLinks="true" autoHide="true">
</pagination-controls>
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">Delete this entry</h4>
</div>
<div class="modal-body">
<div class="alert alert-danger">Are you sure you want to delete this Record?</div>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-success danger" (click)="deleteGame(i)"><span class="glyphicon glyphicon-ok-sign"></span> Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
user.component.ts
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'user',
templateUrl: 'user.component.html',
})
export class UserComponent {
welcome : string;
games : [{
game: string,
platform : string,
release : string
}];
constructor(){
this.welcome = "Display List using ngFor in Angular 2";
this.games = [{
game : "Deus Ex: Mankind Divided",
platform: " Xbox One, PS4, PC",
release : "August 23"
},
{
game : "Hue",
platform: " Xbox One, PS4, Vita, PC",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "Deus Ex: Mankind Divided",
platform: " Xbox One, PS4, PC",
release : "August 23"
},
{
game : "Hue",
platform: " Xbox One, PS4, Vita, PC",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "Deus Ex: Mankind Divided",
platform: " Xbox One, PS4, PC",
release : "August 23"
},
{
game : "Hue",
platform: " Xbox One, PS4, Vita, PC",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "Deus Ex: Mankind Divided",
platform: " Xbox One, PS4, PC",
release : "August 23"
},
{
game : "Hue",
platform: " Xbox One, PS4, Vita, PC",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "Deus Ex: Mankind Divided",
platform: " Xbox One, PS4, PC",
release : "August 23"
},
{
game : "Hue",
platform: " Xbox One, PS4, Vita, PC",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
},
{
game : "The Huntsman: Winter's Curse",
platform: "PS4",
release : "August 23"
}];
};
deleteGame(i){
this.games.splice(i,1);
}
};
我建議你做某種點擊功能,即捕獲組件中哪個遊戲已被點擊(使用哪個索引)。因此,當用戶確認刪除時,您可以使用組件中存在的變量進行刪除。 – Alex