javascript - 如何实现使用 Angular 6 创建的表格左侧的复选框?

标签 javascript angular typescript angular-material frontend

下面是我的 TS 文件。

import { Component, OnInit } from '@angular/core';
import { SelectionModel, DataSource } from '@angular/cdk/collections';
import { OrdersService } from '../orders.service';
import { Observable } from 'rxjs/Observable';

export interface DataTableItem {
  name: string;
  email: string;
  phone: string;
  company: {
    name: string;
  };
}

@Component({
  // tslint:disable-next-line:component-selector
  selector: 'data-table',
  templateUrl: './data-table.component.html',
  styleUrls: ['./data-table.component.css']
})

export class DataTableComponent implements OnInit {

  dataSource = new UserDataSource(this.orderService);
  selection = new SelectionModel<any>(true, []);

  /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
  displayedColumns = ['name', 'email', 'phone', 'company'];

  /** Whether the number of selected elements matches the total number of rows. */
  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  /** Selects all rows if they are not all selected; otherwise clear selection. */
  masterToggle() {
    this.isAllSelected() ?
      this.selection.clear() :
      this.dataSource.data.forEach(row => this.selection.select(row));
  }

  constructor(private orderService: OrdersService) { }

  ngOnInit() {
    console.log(JSON.stringify(this.dataSource));
  }
}

export class UserDataSource extends DataSource<any> {
  constructor(private orderService: OrdersService) {
    super();
  }

  connect(): Observable<DataTableItem[]> {
    return this.orderService.GetTestData();
  }

  disconnect() { }
}

我之前能够按照 Angular Material Table 中的示例来实现复选框,但是当我使用外部 API 填充表时,函数 isAllSelected()masterToggle() 开始出现错误。我应该编辑什么才能使功能再次工作?

最佳答案

对于一个 DataSource 类没有 data 属性,因此您的解决方案将不起作用。我不会扩展 DataSource,而是扩展 MatTableDataSource

如果您将数据源更改为以下内容:

export class UserDataSource extends MatTableDataSource<any> {
  constructor(private orderService: OrdersService) {
    super();
    this.orderService.GetTestData().subscribe(d => {
      this.data = d;
    });
  }
}

不要忘记导入MatTableDataSource:

import { MatTableDataSource } from '@angular/material';

Here is a stackblitz that shows a working example with the MatTableDataSource. The pipe(delay(1500)) is just there to simulate an async data request.

关于javascript - 如何实现使用 Angular 6 创建的表格左侧的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741863/

相关文章:

javascript - SAPUI5 - 如何获取我的自定义控件绑定(bind)到的模型的名称?

javascript - AngularJS ng-include 在指令链接中不起作用

具有多个模块和动态路由的 Angular 路由

javascript - 将函数重写为异步

typescript - Deno REPL 无法识别 TypeScript 变量声明

javascript - 从页面 javascript 按钮运行 Node.JS

javascript - 既然 Express 不带有主体解析器,我如何使用外部中间件解析 JSON?

javascript - 与绑定(bind)进度功能一起使用时,Angular Bootstrap Progressbar 不更新

angular - VS 代码- Angular : Experimental support for decorators Warning

javascript - 无法为 typescript 字典赋值