至于使用这个Angular Material的原因呢,随便选的。

表格处理

记录一下第一张表 这张表用的($event)很鸡肋响应差还经常查不到,下面有改进版

mat-table中row如果需要排序,不要使用index绑定函数

否则会出现排序和传参不同的问题

  1. <mat-form-field appearance="standard">
  2. <mat-label>Filter</mat-label>
  3. <input matInput (keyup)="applyFilter($event)" placeholder="Ex. Mia" #input>
  4. </mat-form-field>
  5. <div class="mat-elevation-z8">
  6. <table mat-table [dataSource]="dataSource" matSort>
  7. <ng-container matColumnDef="id">
  8. <th mat-header-cell *matHeaderCellDef mat-sort-header> ID</th>
  9. <td mat-cell *matCellDef="let row"> {{row.id}} </td>
  10. </ng-container>
  11. <ng-container matColumnDef="name">
  12. <th mat-header-cell *matHeaderCellDef mat-sort-header> Name</th>
  13. <td mat-cell *matCellDef="let row"> {{row.name}} </td>
  14. </ng-container>
  15. <ng-container matColumnDef="brief">
  16. <th mat-header-cell *matHeaderCellDef mat-sort-header> Brief</th>
  17. <td mat-cell *matCellDef="let row"> {{row.brief}} </td>
  18. </ng-container>
  19. <ng-container matColumnDef="createDate">
  20. <th mat-header-cell *matHeaderCellDef mat-sort-header> CreateDate</th>
  21. <td mat-cell *matCellDef="let row"> {{row.createDate}}</td>
  22. </ng-container>
  23. <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  24. <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  25. <!-- Row shown when there is no matching data. -->
  26. <tr class="mat-row" *matNoDataRow>
  27. <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
  28. </tr>
  29. </table>
  30. <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" aria-label="Select page of users"></mat-paginator>
  31. </div>
  1. import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
  2. import {CollectionService} from "./service/collection.service";
  3. import {LoginService} from "../../login.service";
  4. import {MatSort} from "@angular/material/sort";
  5. import {MatPaginator} from "@angular/material/paginator";
  6. import {MatTableDataSource} from "@angular/material/table";
  7. import {Collection} from "./class/collection";
  8. @Component({
  9. selector: 'app-buttons',
  10. templateUrl: './collections.component.html',
  11. styleUrls: ['./collections.component.scss']
  12. })
  13. export class CollectionsComponent implements AfterViewInit {
  14. displayedColumns: string[] = ['id', 'name', 'brief', 'createDate'];
  15. //这一行对应matColumnDef属性
  16. dataSource: MatTableDataSource<Collection>;
  17. @ViewChild(MatPaginator) paginator!: MatPaginator;
  18. @ViewChild(MatSort) sort!: MatSort;
  19. //此处很关键 直接写 : 会报错 必须加上 !
  20. constructor(
  21. public collectionService: CollectionService,
  22. public loginService: LoginService
  23. ) {
  24. this.getCollectionsByUserId(this.loginService.user.id);
  25. this.dataSource = new MatTableDataSource(this.collectionService.collectionList);
  26. }
  27. ngAfterViewInit() {
  28. this.dataSource.paginator = this.paginator;
  29. this.dataSource.sort = this.sort;
  30. }
  31. getCollectionsByUserId(id: number) {
  32. this.collectionService.getCollectionsByEditorId(id)
  33. .subscribe(CollectionData => this.collectionService.collectionList = CollectionData.data)
  34. }
  35. applyFilter(event: Event) {
  36. const filterValue = (event.target as HTMLInputElement).value;
  37. this.dataSource.filter = filterValue.trim().toLowerCase();
  38. if (this.dataSource.paginator) {
  39. this.dataSource.paginator.firstPage();
  40. }
  41. }
  42. }

改进!!!!!!

  1. applyFilter(filterValue: String) {
  2. this.dataSource.filter = filterValue.trim().toLowerCase();
  3. if (this.dataSource.paginator) {
  4. this.dataSource.paginator.firstPage();
  5. }
  6. }
  1. <mat-form-field appearance="standard">
  2. <mat-label>搜索</mat-label>
  3. <input matInput #input (input)="applyFilter(input.value)">
  4. </mat-form-field>

响应数据的方式

  1. constructor(
  2. public collectionService: CollectionService,
  3. public loginService: LoginService,
  4. public categoryService: CategoryService//用于获取category作为选项卡使用
  5. ) {
  6. this.collectionService.getCollectionsByEditorId(this.loginService.user.id)
  7. .subscribe(R => {
  8. this.collectionService.collectionList = R.data;
  9. this.dataSource.data = this.collectionService.collectionList;//再填入数据
  10. });
  11. this.dataSource = new MatTableDataSource(this.collectionService.collectionList);//先new
  12. }

Router和ActivatedRouter的区别

暂时知道activatedRouter可以查url中的参数

angularMaterial中,弹出框需要单独设置一个component,还是挺麻烦的

https://material.angular.cn/components/dialog/overview

(checked)是单向绑定,在滑块开关中需要注意,换成[(ngModel)]才能双向绑定

https://github.com/angular/components/issues/3161

Icon相关

https://jossef.github.io/material-design-icons-iconfont/