用angular material 做统计表格
安装 angular material、组件开发工具 (cdk) 和 angular 动画库,并运行代码原理图
ng add @angular/material
表格原理图将创建一个组件,它可以渲染出一个预置了可排序、可分页数据源的 angular material。【相关教程推荐:《angular教程》】
ng generate @angular/material:table texe1
然后在这的基础上进行修改。
该组件的html文件
<div class="mat-elevation-z8"> <table mat-table class="full-width-table" matsort aria-label="elements"> <!-- id column --> <ng-container matcolumndef="id"> <th mat-header-cell *matheadercelldef mat-sort-header>序号</th> <td mat-cell *matcelldef="let row">{{row.id}}</td> </ng-container> <!-- name column --> <ng-container matcolumndef="name"> <th mat-header-cell *matheadercelldef mat-sort-header> 岩土名</th> <td mat-cell *matcelldef="let row">{{row.name}}</td> </ng-container> <!-- num1 column --> <ng-container matcolumndef="num1"> <th mat-header-cell *matheadercelldef mat-sort-header> 期望数量</th> <td mat-cell *matcelldef="let row">{{row.num1}}</td> </ng-container> <!-- num2 column --> <ng-container matcolumndef="num2"> <th mat-header-cell *matheadercelldef mat-sort-header> 当前数量</th> <td mat-cell *matcelldef="let row">{{row.num2}}</td> </ng-container> <tr mat-header-row *matheaderrowdef="displayedcolumns"></tr> <tr mat-row *matrowdef="let row; columns: displayedcolumns;"></tr> </table> <!-- 控制表格数据的显示长度 --> <mat-paginator #paginator [length]="datasource?.data?.length" [pageindex]="0" [pagesize]="10" [pagesizeoptions]="[5, 10, 17]" aria-label="select page"> </mat-paginator></div>
该组件的texe1-datasource.ts文件
import { datasource } from '@angular/cdk/collections';import { matpaginator } from '@angular/material/paginator';import { matsort } from '@angular/material/sort';import { map } from 'rxjs/operators';import { observable, of as observableof, merge } from 'rxjs';// todo: replace this with your own data model typeexport interface texe1item { name: string; id: number; num1: number; num2: number;}// todo: replace this with real data from your applicationconst example_data: texe1item[] = [ {id: 1, name: '粉质粘土', num1:1000, num2:100,}, {id: 2, name: '淤泥质粉质粘土', num1:1000, num2:100,}, {id: 3, name: '粘土', num1:1000, num2:100,}, {id: 4, name: '粘质粉土', num1:1000, num2:100,}, {id: 5, name: '淤泥质粘土', num1:1000, num2:100,}, {id: 6, name: '圆砾(角砾)', num1:1000, num2:100,}, {id: 7, name: '中砂', num1:1000, num2:1000,}, {id: 8, name: '有机质土', num1:1000, num2:100,}, {id: 9, name: '泥炭质土a', num1:1000, num2:100,}, {id: 10, name: '泥炭质土b', num1:1000, num2:100,}, {id: 11, name: '砂质粉土', num1:1000, num2:100,}, {id: 12, name: '粉砂', num1:1000, num2:100,}, {id: 13, name: '细砂', num1:1000, num2:100,}, {id: 14, name: '粗砂', num1:1000, num2:100,}, {id: 15, name: '砾砂', num1:1000, num2:100,}, {id: 16, name: '卵石(碎石)', num1:1000, num2:100,}, {id: 17, name: '漂石(块石)', num1:1000, num2:100,},];/** * data source for the texe1 view. this class should * encapsulate all logic for fetching and manipulating the displayed data * (including sorting, pagination, and filtering). */export class texe1datasource extends datasource<texe1item> { data: texe1item[] = example_data; paginator: matpaginator | undefined; sort: matsort | undefined; constructor() { super(); } /** * connect this data source to the table. the table will only update when * the returned stream emits new items. * @returns a stream of the items to be rendered. */ connect(): observable<texe1item[]> { if (this.paginator && this.sort) { // combine everything that affects the rendered data into one update // stream for the data-table to consume. return merge(observableof(this.data), this.paginator.page, this.sort.sortchange) .pipe(map(() => { return this.getpageddata(this.getsorteddata([...this.data ])); })); } else { throw error('please set the paginator and sort on the data source before connecting.'); } } /** * called when the table is being destroyed. use this function, to clean up * any open connections or free any held resources that were set up during connect. */ disconnect(): void {} /** * paginate the data (client-side). if you're using server-side pagination, * this would be replaced by requesting the appropriate data from the server. */ private getpageddata(data: texe1item[]): texe1item[] { if (this.paginator) { const startindex = this.paginator.pageindex * this.paginator.pagesize; return data.splice(startindex, this.paginator.pagesize); } else { return data; } } /** * sort the data (client-side). if you're using server-side sorting, * this would be replaced by requesting the appropriate data from the server. */ private getsorteddata(data: texe1item[]): texe1item[] { if (!this.sort || !this.sort.active || this.sort.direction === '') { return data; } return data.sort((a, b) => { const isasc = this.sort?.direction === 'asc'; switch (this.sort?.active) { case 'name': return compare(a.name, b.name, isasc); case 'id': return compare(+a.id, +b.id, isasc); default: return 0; } }); }}/** simple sort comparator for example id/name columns (for client-side sorting). */function compare(a: string | number, b: string | number, isasc: boolean): number { return (a < b ? -1 : 1) * (isasc ? 1 : -1);}
该组件的texe1.component.ts文件
import { afterviewinit, component, viewchild } from '@angular/core';import { matpaginator } from '@angular/material/paginator';import { matsort } from '@angular/material/sort';import { mattable } from '@angular/material/table';import { texe1datasource, texe1item } from './texe1-datasource';@component({ selector: 'app-texe1', templateurl: './texe1.component.html', styleurls: ['./texe1.component.css']})export class texe1component implements afterviewinit { @viewchild(matpaginator) paginator!: matpaginator; @viewchild(matsort) sort!: matsort; @viewchild(mattable) table!: mattable<texe1item>; datasource: texe1datasource; /** columns displayed in the table. columns ids can be added, removed, or reordered. */ displayedcolumns = ['id', 'name','num1','num2']; constructor() { this.datasource = new texe1datasource(); } ngafterviewinit(): void { this.datasource.sort = this.sort; this.datasource.paginator = this.paginator; this.table.datasource = this.datasource; }}
最后再app.component.html文件中进行显示。
<app-texe1></app-texe1>
效果图:
更多编程相关知识,请访问:编程视频!!
以上就是聊聊怎么利用angular material做统计表格的详细内容。
