0

我有这个:

    removeSelectedRows() {

    this.selection.selected.forEach(item => {
      let index: number = this.dataSource.findIndex(d => d === item);
       console.log(this.dataSource.findIndex(d => d === item));
       this.dataSource.splice(index,1)
       this.dataSource = new MatTableDataSource<Element>(this.dataSource);
}

我想删除选定的行,我在互联网上找到了这段代码,但它给了我一个错误:

RefactorComponent.html:12 ERROR TypeError: this.dataSource.findIndex is not a function
    at refactor.component.ts:65
    at Array.forEach (<anonymous>)
    at RefactorComponent.removeSelectedRows (refactor.component.ts:64)
    at Object.eval [as handleEvent] (RefactorComponent.html:12)
    at handleEvent (core.js:43993)
    at callWithDebugContext (core.js:45632)
    at Object.debugHandleEvent [as handleEvent] (core.js:45247)
    at dispatchEvent (core.js:29804)
    at core.js:42925
    at HTMLButtonElement.<anonymous> (platform-browser.js:2668)

我需要您占据所选行的位置,以便将其从数据源中删除。我正在使用角度 8。

4

1 回答 1

2

如果您的数据源是这样定义的

this.dataSource = new MatTableDataSource<Element>(dataArray);

你需要this.dataSource.data像这样调用方法findIndex

this.dataSource.data.findIndex(d => d === item);
于 2020-01-09T15:54:38.993 回答