2017-11-27 3 views
0

템플릿 구동 양식을 사용하여이 행을 어떻게 삭제할 수 있습니까? 배열이 *ngFor에 반복되므로 혼란 스럽습니다. 내가 어떻게 할 수 있니? 여기에 내가 한 일이 있습니다.반복 삭제 * ngFor 템플릿 사용 각도 2/4

HTML

<tr *ngFor="let innerItem of project.material_projects | orderBy: {property: column, direction: direction} | let i = index"> 
    <td>{{innerItem.material.name}}</td> 
    <td>{{innerItem.balance}}</td> 
    <td> 
    <button type="button" class="btn btn-sm btn-danger" (click)="onDelete(i)"> Delete</button> 
    </td> 
</tr> 

TS

onDelete(index: number) { 
     this.projects.material_projects.splice(index, 1) 
} 

답변

0

문제는 논리적 부분입니다 :

| orderBy: {property: column, direction: direction} | 

이것은 당신의 project.material_projects 배열을 재정렬하고,이 라인 전에 같은 작업을 수행 할 필요가 있도록은 순서가 배열의 인덱스를 사용하고 있습니다 :

this.projects.material_projects.splice(index, 1) 

을 그리고 그것은 이루어집니다.


또는

독특한 liek 경우 가

당신은 id를 전달할 수 onDelete(innerItem.id)

그런

onDelete(id: number) { 
    let index = // logic to find index of that id ; 
    this.projects.material_projects.splice(index, 1); 
} 
+0

HTML 부분을 편집 할 수 있습니까? 나는 네가 말하는 것을 혼란스러워. 감사합니다 – Joseph

+0

@Joseph, 이것에 대한 plnkr을 만들어주세요, 거기서 보여 드리겠습니다. 두 번째 방법은 매우 명확합니다. –

+0

그것은 정의되지 않은 – Joseph

0

이 같은 시도 :

<tr *ngFor="let i = index; let innerItem of project.material_projects | orderBy: { property: column, direction: direction }"> 
    <td>{{innerItem.material.name}}</td> 
    <td>{{innerItem.balance}}</td> 
    <td> 
     <button type="button" class="btn btn-sm btn-danger" (click)="onDelete(i)"> Delete</button> 
    </td> 
</tr> 

및 ts onDelete 메소드는 다음과 같습니다.

onDelete(index) { 
    this.project.material_projects.splice(index, 1); 
} 
+0

문제는이 tr이 * ngFor = 프로젝트 아래 프로젝트라는 것이다. 그래서 오류 스플 라이스가 정의되지 않은 이유는 다음과 같습니다. – Joseph

+0

@Joseph tr 태그를 다음과 같이 시도하십시오. - – Chandru

+0

명령을 터치하십시오. 왜냐하면 내가 fa-ort 파이프를 가지고 있기 때문에 desc 또는 ascn은 어떤 화살표가 눌리는 지에 달려 있습니다. – Joseph