src/app/pages/user/user.component.ts
| selector | app-user |
| styleUrls | ./user.component.scss |
| templateUrl | ./user.component.html |
Properties |
Methods |
constructor(service: UserService)
|
||||||
|
Defined in src/app/pages/user/user.component.ts:26
|
||||||
|
Parameters :
|
| isAllSelected |
isAllSelected()
|
|
Defined in src/app/pages/user/user.component.ts:36
|
|
Returns :
boolean
|
| masterToggle |
masterToggle()
|
|
Defined in src/app/pages/user/user.component.ts:42
|
|
Returns :
void
|
| ngOnInit |
ngOnInit()
|
|
Defined in src/app/pages/user/user.component.ts:30
|
|
Returns :
void
|
| onAddTriggered |
onAddTriggered()
|
|
Defined in src/app/pages/user/user.component.ts:48
|
|
Returns :
void
|
| onDeleteTriggered |
onDeleteTriggered()
|
|
Defined in src/app/pages/user/user.component.ts:53
|
|
Returns :
void
|
| onEditorTriggered | ||||
onEditorTriggered(user)
|
||||
|
Defined in src/app/pages/user/user.component.ts:49
|
||||
|
Parameters :
Returns :
void
|
| dataSource |
Default value : new MatTableDataSource([])
|
|
Defined in src/app/pages/user/user.component.ts:25
|
| displayedColumns |
Type : string[]
|
Default value : [
'select',
'key',
'username',
'nickname',
'role',
'email',
'phone',
'createTime',
'status',
'star'
]
|
|
Defined in src/app/pages/user/user.component.ts:12
|
| selection |
Default value : new SelectionModel(true, [])
|
|
Defined in src/app/pages/user/user.component.ts:26
|
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%;
}