File

src/app/pages/user/user.component.ts

Implements

OnInit

Metadata

selector app-user
styleUrls ./user.component.scss
templateUrl ./user.component.html

Index

Properties
Methods

Constructor

constructor(service: UserService)
Parameters :
Name Type Optional
service UserService No

Methods

isAllSelected
isAllSelected()
Returns : boolean
masterToggle
masterToggle()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onAddTriggered
onAddTriggered()
Returns : void
onDeleteTriggered
onDeleteTriggered()
Returns : void
onEditorTriggered
onEditorTriggered(user)
Parameters :
Name Optional
user No
Returns : void

Properties

dataSource
Default value : new MatTableDataSource([])
displayedColumns
Type : string[]
Default value : [ 'select', 'key', 'username', 'nickname', 'role', 'email', 'phone', 'createTime', 'status', 'star' ]
selection
Default value : new SelectionModel(true, [])
import { Component, OnInit } from '@angular/core';
import { SelectionModel } from '@angular/cdk/collections';
import { MatTableDataSource } from '@angular/material';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {
  displayedColumns: string[] = [
    'select',
    'key',
    'username',
    'nickname',
    'role',
    'email',
    'phone',
    'createTime',
    'status',
    'star'
  ];

  dataSource = new MatTableDataSource([]);
  selection = new SelectionModel(true, []);

  constructor(private service: UserService) {}

  ngOnInit() {
    this.service.getUserList().subscribe(res => {
      this.dataSource = new MatTableDataSource(res);
    });
  }

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  masterToggle() {
    this.isAllSelected()
      ? this.selection.clear()
      : this.dataSource.data.forEach(row => this.selection.select(row));
  }

  onAddTriggered() {}
  onEditorTriggered(user) {
    console.log(user)
  }

  onDeleteTriggered() {
    console.log(this.selection.selected);
    this.service.delete(this.selection.selected);
  }
}
<button mat-raised-button (click)="onAddTriggered()">
  <mat-icon>add</mat-icon> 添加
</button>
<button mat-raised-button (click)="onDeleteTriggered()">
  <mat-icon>delete</mat-icon> 删除
</button>

<div class="mat-elevation-z8">
  <table mat-table [dataSource]="dataSource">
    <ng-container matColumnDef="select">
      <th mat-header-cell *matHeaderCellDef>
        <mat-checkbox
          (change)="$event ? masterToggle() : null"
          [checked]="selection.hasValue() && isAllSelected()"
          [indeterminate]="selection.hasValue() && !isAllSelected()"
        >
        </mat-checkbox>
      </th>
      <td mat-cell *matCellDef="let row">
        <mat-checkbox
          (click)="$event.stopPropagation()"
          (change)="$event ? selection.toggle(row) : null"
          [checked]="selection.isSelected(row)"
        >
        </mat-checkbox>
      </td>
    </ng-container>

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

    <ng-container matColumnDef="username">
      <th mat-header-cell *matHeaderCellDef>用户名</th>
      <td mat-cell *matCellDef="let element">{{ element.username }}</td>
    </ng-container>

    <ng-container matColumnDef="nickname">
      <th mat-header-cell *matHeaderCellDef>昵称</th>
      <td mat-cell *matCellDef="let element">{{ element.nickname }}</td>
    </ng-container>

    <ng-container matColumnDef="role">
      <th mat-header-cell *matHeaderCellDef>角色</th>
      <td mat-cell *matCellDef="let element">{{ element.role }}</td>
    </ng-container>

    <ng-container matColumnDef="email">
      <th mat-header-cell *matHeaderCellDef>邮箱</th>
      <td mat-cell *matCellDef="let element">{{ element.email }}</td>
    </ng-container>

    <ng-container matColumnDef="phone">
      <th mat-header-cell *matHeaderCellDef>手机号</th>
      <td mat-cell *matCellDef="let element">{{ element.phone }}</td>
    </ng-container>

    <ng-container matColumnDef="createTime">
      <th mat-header-cell *matHeaderCellDef>创建时间</th>
      <td mat-cell *matCellDef="let element">{{ element.createTime }}</td>
    </ng-container>

    <ng-container matColumnDef="status">
      <th mat-header-cell *matHeaderCellDef>状态</th>
      <td mat-cell *matCellDef="let element">{{ element.status }}</td>
    </ng-container>

    <ng-container matColumnDef="star" stickyEnd>
      <th mat-header-cell *matHeaderCellDef>操作</th>
      <td
        mat-cell
        *matCellDef="let element"
        (click)="onEditorTriggered(element)"
      >
        编辑
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr
      mat-row
      *matRowDef="let row; columns: displayedColumns"
      (click)="selection.toggle(row)"
    ></tr>
  </table>
</div>

./user.component.scss

:host {
  display: block;
  margin: 24px;
}

button {
  margin: 0 16px 16px 0;
}

table {
  width: 100%;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""