编写通用组件

This commit is contained in:
禾几海
2020-06-30 23:56:16 +08:00
parent c271b1a8cf
commit 781513d8c9
6 changed files with 67 additions and 33 deletions

View File

@@ -45,6 +45,10 @@ const routes: Routes = [
loadChildren: () => import('./admin-visitor/admin-visitor.module').then(mod => mod.AdminVisitorModule), loadChildren: () => import('./admin-visitor/admin-visitor.module').then(mod => mod.AdminVisitorModule),
// canActivate: [AuthGuard] // canActivate: [AuthGuard]
}, },
{
path: 'test',
loadChildren: () => import('./test-common-table/test-common-table.module').then(Mod => Mod.TestCommonTableModule)
},
{ {
path: '**', path: '**',
loadChildren: () => import('./admin-dashboard/admin-dashboard.module').then(mod => mod.AdminDashboardModule), loadChildren: () => import('./admin-dashboard/admin-dashboard.module').then(mod => mod.AdminDashboardModule),

View File

@@ -1,24 +1,33 @@
<nz-table> <nz-table>
<thead> <thead>
<tr> <tr>
<td *ngFor="let headData of data"> <ng-container *ngFor="let headData of data">
{{headData.fieldName}} <th *ngIf="headData.show">
</td> {{headData.fieldName}}
</th>
</ng-container>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr *ngFor="let t of dataList.list">
<td *ngFor="let headData of data"> <ng-container *ngFor="let headData of data">
<ng-template [ngIf]="!headData.isActionColumns"> <td *ngIf="headData.show">
{{headData.fieldName}} <ng-template [ngIf]="!headData.isActionColumns">
</ng-template> {{t[headData.fieldValue]}}
<ng-template *ngIf="headData.isActionColumns"> </ng-template>
<a *ngFor="let action of headData.action" (mouseenter)="action.hover(headData)" (mouseout)="null" <ng-container *ngIf="headData.isActionColumns">
(click)="action.click(headData)" [ngStyle]="{'color':action.color,'font-size':action.fontSize}"> <a *ngFor="let action of headData.action; let i = index" (mouseenter)="action.hover(t)"
{{action.name}} (mouseout)="null" (click)="action.click(t)"
</a> [ngStyle]="{'color':action.color,'font-size':action.fontSize}">
</ng-template> {{action.name}}
</td> <ng-template [ngIf]="i!=headData.action.length-1">
<nz-divider nzType="vertical"></nz-divider>
</ng-template>
</a>
</ng-container>
</td>
</ng-container>
</tr> </tr>
</tbody> </tbody>

View File

@@ -1,5 +1,7 @@
import {Component, Input, OnInit} from '@angular/core'; import {Component, Input, OnInit} from '@angular/core';
import {Data} from './data'; import {Data} from './data';
import {PageList, RequestObj} from '../../../../class/HttpReqAndResp';
import {HttpService} from '../../../../api/http/http.service';
@Component({ @Component({
selector: 'app-common-table', selector: 'app-common-table',
@@ -8,12 +10,27 @@ import {Data} from './data';
}) })
export class CommonTableComponent<T> implements OnInit { export class CommonTableComponent<T> implements OnInit {
constructor() { constructor(private httpService: HttpService) {
} }
@Input() data: Data<T>[] @Input() data: Data<T>[]
@Input() request: RequestObj
dataList: PageList<T> = new PageList<T>();
ngOnInit(): void { ngOnInit(): void {
this.httpService.Service<PageList<T>>(this.request).subscribe(resp => {
this.dataList = resp.result;
});
this.data.forEach(dat => {
if (!dat.action) return;
dat.action.forEach(act => {
if (!act.hover) {
act.hover = () => null;
}
})
})
} }
} }

View File

@@ -1,16 +1,20 @@
import {NgModule} from '@angular/core'; import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common'; import {CommonModule} from '@angular/common';
import {CommonTableComponent} from './common-table.component'; import {CommonTableComponent} from './common-table.component';
import {NzTableModule} from "ng-zorro-antd"; import {NzDividerModule, NzTableModule} from 'ng-zorro-antd';
@NgModule({ @NgModule({
declarations: [ declarations: [
CommonTableComponent CommonTableComponent
], ],
exports: [
CommonTableComponent
],
imports: [ imports: [
CommonModule, CommonModule,
NzTableModule NzTableModule,
NzDividerModule
] ]
}) })
export class CommonTableModule { export class CommonTableModule {

View File

@@ -1,15 +0,0 @@
export class Data<T> {
fieldName: string;
fieldValue: string;
show: boolean;
bindData?: T;
isActionColumns: boolean;
action: {
name: string,
color: string,
order: number,
fontSize: string,
click: (data: T) => void,
hover: (data: T) => void;
}[]
}

View File

@@ -0,0 +1,15 @@
export class Data<T> {
fieldName: string;
fieldValue: string;
show: boolean = true;
primaryKey?: boolean = false;
isActionColumns?: boolean = false;
action?: {
name: string,
color?: string,
order?: number,
fontSize?: string,
click: (data: T) => void,
hover?: (data: T) => void | null;
}[] = []
}