Angular / Material 7如何外包材料表行

时间:2018-11-11 21:39:32

标签: javascript angular angular-material angular-cdk

我想将管理员的表行设置外包到一个行组件中,但是出现以下错误消息:

enter image description here

如果我使用标签选择器而不是属性选择器,则会得到以下信息:

错误错误:找不到ID为“ title”的列。     在getTableUnknownColumnError(table.es5.js:651)

我累了什么

my-field.component.html:

<ng-container [matColumnDef]="myField + '-search'">
    <th mat-header-cell fxFlex *matHeaderCellDef>
        <mat-form-field [appearance]="appearance">
            <mat-label>{{label}}</mat-label>
            <input matInput (keyup)="applyFilter($event.target.value)" [placeholder]="placeholder">
        </mat-form-field>    
    </th>
</ng-container>

<ng-container [matColumnDef]="myField">
    <th mat-header-cell fxFlex fxLayoutAlign="start center" *matHeaderCellDef> 
        <div fxFlex="nogrow" mat-sort-header>{{header}}</div> 
    </th>
    <td mat-cell fxFlex fxLayoutAlign="start center" *matCellDef="let row"> {{row[myField]}} </td>
</ng-container>

my-field.component.ts:

import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';


@Component({
// moduleId: module.id,
selector: '[myField]',
templateUrl: './grid-field.component.html',
styleUrls: ['grid-field.component.scss']
})
export class GridFieldComponent implements OnInit, OnDestroy {
    public _class: string = 'GridFieldComponent';
    private nsp: string;

    @Input() label: string;
    @Input() header: string;
    @Input() option: string;
    @Input() appearance: string;
    @Input() placeholder: string;
    @Input() myField: string;

    @Output() onChange: EventEmitter<any> = new EventEmitter();

    constructor() {}

    public ngOnInit() {}

    public ngOnDestroy() {}

    public applyFilter(value) {
        const self = this;

        self.onChange.emit({
            'field': `-${self.myField}`,
            'value': value,
            'option': self.option
        })
    }
}

page.html:

<table mat-table [dataSource]="data" matSort>
    <ng-container myField="title" appearance="outline" label="Search" header="Title"></ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns" class="first-header-row"></tr>

    <tr mat-header-row *matHeaderRowDef="displayedSearchColumns"class="second-header-row" 
                        style="height: auto; padding: 10px 0;"></tr>

    <tr mat-row *matRowDef="let row; columns: displayedColumns;" style="height: auto;"></tr>
</table>

0 个答案:

没有答案