如何删除没有过滤器的角度材料表中的特定行?

问题描述 投票:2回答:3

我的samplepage.component.html

<table mat-table [dataSource]="myDataArray" >
      <!-- <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns" >
                      <th mat-header-cell *matHeaderCellDef > {{column}} </th>
                      <td mat-cell *matCellDef="let element" > {{element[column]}} </td>
                    </ng-container> -->


      <ng-container matColumnDef="id">
        <th mat-header-cell *matHeaderCellDef> Id. </th>
        <td mat-cell *matCellDef="let element; let i = index;"> {{i+1}} </td>
      </ng-container>


      <ng-container matColumnDef="ticketnumb">
        <th mat-header-cell *matHeaderCellDef> Ticket Number </th>
        <td mat-cell *matCellDef="let element"> {{element.ticketnumb}} </td>
      </ng-container>


      <ng-container matColumnDef="actionsColumn">
          <th mat-header-cell *matHeaderCellDef> Action </th>
          <td mat-cell *matCellDef="let element;  let j = index;"> 
              <button mat-raised-button  color="warn" focusable="false" (click)="deleteTicket(j)">
                  <i class="fa fa-times mat-icon"></i> Remove
                </button>
          </td>
        </ng-container>





      <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

我的samplepage.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  selector: 'app-batchticketvalidation',
  templateUrl: './batchticketvalidation.component.html',
  styleUrls: ['./batchticketvalidation.component.css']
})
export class BatchticketvalidationComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }
  displayedColumns = ['id', 'ticketnumb', 'actionsColumn'];
  dataSource: PeriodicElement[] = ELEMENT_DATA;
  private myDataArray: any;
  private tempTicketCount: number = 0;



  deleteTicket(rowid: number){

   if (rowid > -1) {
    this.myDataArray.splice(rowid, 1);
 }

  }

}


export interface PeriodicElement {
  id: number;
  ticketnumb: string;
  actionsColumn: any;
}


const ELEMENT_DATA: PeriodicElement[] = [
];

我是棱角分明的新人。我需要帮助从表中删除特定行,一旦删除行,表应该刷新或者应该显示现有数据。我只有静态行。这只是一个简单的模型html,我想向客户展示。

每行都有删除按钮,点击删除按钮我调用deleteTicket(rowid)。

当触发deleteTicket方法时,该行不会从Ui中删除,但是当我控制this.myDataArray时,数据将从对象中删除。

我尝试了所有可能性。

请帮忙

angular delete-row
3个回答
5
投票

在splice()之后通过item的索引,您应该更新数据源。

      const index = this.dataSource.data.indexOf(item.id);
      this.dataSource.data.splice(index, 1);
      **this.dataSource._updateChangeSubscription();**

0
投票

删除所选行后,您必须再次重新分配matDataTable。

deleteTicket(rowid: number){
  // raw_data is the array of data that you getting from the db.
  this.raw_data.splice(rowid,0)
  this.dataSource = new MatTableDataSource(this.users);   
}

0
投票

我建议使用MatTableDataSource

dataSource: MatTableDataSource = new MatTableDataSource<T>(ELEMENT_DATA);

deleteFunction(item){
    // find item and remove ist
    dataSource.data.splice(this.dataSource.data.indexOf(item.id), 1);
}
© www.soinside.com 2019 - 2024. All rights reserved.