如何正确使用filterPredicate

时间:2019-06-05 20:57:49

标签: angular typescript angular-material

  

消息:“未定义工作区”
  堆栈:“ ReferenceError:未定义工作区↵

我有一个名为verifyIfHaveThisExactlyWorkspaceName的函数,如果表中存在相同的行名,则返回true或false。

在输入时键入即可。我想将相同的方法用于另一个方法,并按参数传递字符串。我对此有疑问。在这两种情况下,T都使用表中存在的值。

在这种情况下,我会成功

我的html文件


<mat-form-field class="table-w-div-input">
            <input matInput (keyup)="verifyIfHaveThisExactlyWorkspaceName($event.target.value)" placeholder="Qual assunto você deseja ver?">
</mat-form-field>

我的ts文件


public workspaces: MatTableDataSource<WorkspaceModel>;
    public displayedColumns = [
        'workspace',
        'createdAt',
        'createdBy',
        'updatedAt',
        'updatedBy',
        'button',
    ];

public verifyIfHaveThisExactlyWorkspaceName(workspace: string): boolean {
        debugger
        if (!workspace || !this.workspaces) {
            return false
        }
        workspace = workspace.trim();
        this.workspaces.filter = workspace
        this.workspaces.filterPredicate = (data: WorkspaceModel, workspace) => {
                return data['name'] === workspace;
        }
    }

但是当T这样做时,它是行不通的。我的表单不是未定义的(具有值)

public workspaces: MatTableDataSource<WorkspaceModel>;
    public displayedColumns = [
        'workspace',
        'createdAt',
        'createdBy',
        'updatedAt',
        'updatedBy',
        'button',
    ];
public executeOperation(content, spreadsheet?: NgForm) {
        if (spreadsheet) {
            this.createWorkspaceWithImport(spreadsheet)
        }
        else if (this.workspace) {
            this.updateWorkspace()
        }
        else if (!spreadsheet && !this.workspace) {
            if (this.verifyIfHaveThisExactlyWorkspaceName(this.workspaceForm.value.name)) {                
                this.activeModal = this.modalService.open(content, { centered: true, size: 'lg' });                
            }
        }
    }

public verifyIfHaveThisExactlyWorkspaceName(workspace: string): boolean {
        debugger
        if (!workspace || !this.workspaces) {
            return false
        }
        workspace = workspace.trim();
        this.workspaces.filter = workspace
        this.workspaces.filterPredicate = (data: WorkspaceModel, workspace) => {
                return data['name'] === workspace;
        }
    }

我的workspaceModel

import { User } from '../../user/user.actions';


export interface WorkspaceModel {

    createdAt: Date;
    createdBy: User | null;
    db: boolean;
    description: string;
    language: string;
    learning_opt_out: boolean;
    name: string;
    updatedAt: Date;
    updatedBy: User | null;
    watson: boolean;
    _id: string;
    activated: boolean;
    master: string | null;
}

我想在两种情况下都使用我的函数。

0 个答案:

没有答案