通用组件-多级属性显示

This commit is contained in:
禾几海
2020-07-01 12:40:45 +08:00
parent 7a788dc559
commit 014a9eebe4
5 changed files with 28 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
<nz-card nzSize="small" [nzExtra]="refresh" [nzTitle]="title" [nzLoading]="loading"> <nz-card nzSize="small" [nzExtra]="refresh" [nzTitle]="cardTitle" [nzLoading]="loading">
<nz-table nzTableLayout="fixed" <nz-table nzTableLayout="fixed"
[nzData]="dataList.list" [nzData]="dataList.list"
[nzTotal]="dataList.total" [nzTotal]="dataList.total"
@@ -16,17 +16,17 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr *ngFor="let t of dataList.list"> <tr *ngFor="let t of dataList.list;let index = index">
<ng-container *ngFor="let headData of data"> <ng-container *ngFor="let headData of data">
<td *ngIf="headData.show" <td *ngIf="headData.show"
nz-typography nz-typography
nzEllipsis nzEllipsis
nzEllipsisRows="1" nzEllipsisRows="1"
[nzTooltipTitle]="headData.fieldName+' : '+t[headData.fieldValue]" [nzTooltipTitle]="headData.isActionColumns ? null : headData.fieldName+' : '+getValue(index,headData.fieldValue)"
nzTooltipPlacement="right" nzTooltipPlacement="top"
nz-tooltip> nz-tooltip>
<ng-template [ngIf]="!headData.isActionColumns"> <ng-template [ngIf]="!headData.isActionColumns">
{{t[headData.fieldValue]}} {{ getValue(index, headData.fieldValue) }}
</ng-template> </ng-template>
<ng-container *ngIf="headData.isActionColumns"> <ng-container *ngIf="headData.isActionColumns">
<a *ngFor="let action of headData.action; let i = index" (mouseenter)="action.hover(t)" <a *ngFor="let action of headData.action; let i = index" (mouseenter)="action.hover(t)"
@@ -47,5 +47,5 @@
<ng-template #refresh> <ng-template #refresh>
<i nz-icon nzType="reload" nzTheme="outline" (click)="getData()"></i> <i nz-icon nzType="reload" nzTheme="outline" (click)="getData()" style="cursor: pointer"></i>
</ng-template> </ng-template>

View File

@@ -14,9 +14,12 @@ export class CommonTableComponent<T> implements OnInit, OnChanges {
} }
@Input() data: Data<T>[] /**
* 设置readonly data 因为后面有使用eval 为了安全 TODO
*/
@Input() readonly data: Data<T>[]
@Input() request: RequestObj @Input() request: RequestObj
@Input() title: string @Input() cardTitle: string
loading: boolean = true; loading: boolean = true;
dataList: PageList<T> = new PageList<T>(); dataList: PageList<T> = new PageList<T>();
@@ -51,4 +54,10 @@ export class CommonTableComponent<T> implements OnInit, OnChanges {
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
} }
getValue(index: number, fieldValue: string) {
// todo: 过滤
// tslint:disable-next-line:no-eval
return eval(`this.dataList.list[${index}].` + fieldValue);
}
} }

View File

@@ -1 +1 @@
<app-common-table [data]="data" [request]="req" title="文章管理"></app-common-table> <app-common-table [data]="data" [request]="req" cardTitle="文章管理"></app-common-table>

View File

@@ -1,7 +1,7 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {Data} from '../components/common-table/data'; import {Data} from '../components/common-table/data';
import {Article} from '../../../class/Article'; import {Article} from '../../../class/Article';
import {RequestObj} from "../../../class/HttpReqAndResp"; import {RequestObj} from '../../../class/HttpReqAndResp';
@Component({ @Component({
selector: 'app-test-common-table', selector: 'app-test-common-table',
@@ -26,13 +26,19 @@ title: "教你动手写一个刷课脚本"
updateDateFormat: "2020-05-27 00:55:05"*/ updateDateFormat: "2020-05-27 00:55:05"*/
constructor() { constructor() {
this.data = [ this.data = [
{fieldName: '主键', fieldValue: 'id', show: false}, {fieldName: '主键', fieldValue: 'id', show: false, primaryKey: true},
{fieldName: '标题', fieldValue: 'title', show: true}, {fieldName: '标题', fieldValue: 'title', show: true},
{fieldName: '标签', fieldValue: 'category', show: true}, {fieldName: '发布日期', fieldValue: 'publishDateFormat', show: true},
{fieldName: '更新日期', fieldValue: 'updateDateFormat', show: true},
{fieldName: '文章类型', fieldValue: 'original', show: true},
{fieldName: '阅读量', fieldValue: 'readingNumber', show: true},
{fieldName: '分类', fieldValue: 'category', show: true},
{fieldName: '👎数', fieldValue: 'dislikeCount', show: true}, {fieldName: '👎数', fieldValue: 'dislikeCount', show: true},
{fieldName: '👍数', fieldValue: 'likeCount', show: true}, {fieldName: '👍数', fieldValue: 'likeCount', show: true},
{fieldName: '状态', fieldValue: 'open', show: true}, {fieldName: '状态', fieldValue: 'open', show: true},
{fieldName: '简介', fieldValue: 'summary', show: false}, {fieldName: '简介', fieldValue: 'summary', show: false},
{fieldName: '作者', fieldValue: 'author.displayName', show: true},
{fieldName: '标签数', fieldValue: 'tags.length', show: true},
{ {
fieldName: '操作', fieldValue: '', show: true, isActionColumns: true, fieldName: '操作', fieldValue: '', show: true, isActionColumns: true,
action: [ action: [

View File

@@ -2,7 +2,7 @@ import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common'; import {CommonModule} from '@angular/common';
import {TestCommonTableComponent} from './test-common-table.component'; import {TestCommonTableComponent} from './test-common-table.component';
import {Router, RouterModule} from '@angular/router'; import {Router, RouterModule} from '@angular/router';
import {CommonTableModule} from "../components/common-table/common-table.module"; import {CommonTableModule} from '../components/common-table/common-table.module';
@NgModule({ @NgModule({