简体   繁体   English

如何从 ng2-smart-table 中的 filterFunction() 调用方法?

[英]How to invoke method from filterFunction() in ng2-smart-table?

I am trying to invoke a method from a filterfunction() by using 'this' keyword.我正在尝试使用“this”关键字从 filterfunction() 调用方法。 However, I realize that 'this' refers to the event handler instead the component, and the value I get for 'this' is null so I get a runtime error.但是,我意识到“this”指的是事件处理程序而不是组件,而我为“this”获得的值是 null,所以我收到运行时错误。

export class SmartTableComponent {

  settings = {
    ...
    columns: {
      ...
      firstName: {
        title: 'First Name',
        type: 'string',
        filterFunction(cell?: any, search?: string): boolean {          
          return this.doFilter(cell, search);
        }
      },
      ...
    },
    ...
  };


  doFilter(cell?: any, search?: string): boolean{
    return true;
  }
}

In Java, we would get a reference to 'this' by using SmartTableComponent.this.doFilter(...) but this does not seems to work in TypeScript.在 Java 中,我们将通过使用 SmartTableComponent.this.doFilter(...) 获得对“this”的引用,但这在 TypeScript 中似乎不起作用。

How can I invoke a component's method from a filterFunction in ng2-smart-table?如何从 ng2-smart-table 中的 filterFunction 调用组件的方法?

The problem is that whoever invokes this function will set the this variable, and so your idea of what this means in the function has changed. 问题是,谁调用这个函数将设置这个变量,所以你的意味着什么功能的想法发生了改变。 To fix the this to the function (and prevent it from changing), you can use Bind . 要修复功能(并防止它改变),你可以使用绑定 The following code shows the (hopefully) working example. 以下代码显示了(希望的)工作示例。

export class SmartTableComponent {

  settings = {
    ...
    columns: {
      ...
      firstName: {
        title: 'First Name',
        type: 'string',
        filterFunction(cell?: any, search?: string): boolean {          
          return this.doFilter(cell, search);
        }.bind(this)
      },
      ...
    },
    ...
  };


  doFilter(cell?: any, search?: string): boolean{
    return true;
  }
}

My previous explanation is more intuitive than strictly correct, if you really want to know how it works, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this 我以前的解释比完全正确的解释更直观,如果您真的想知道它是如何工作的,请查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

It seems that, by using a lambda expression instead of an anonymous function, the value of 'this' is maintained from the wrapper class. 似乎通过使用lambda表达式而不是匿名函数,可以从包装类维护'this'的值。 So, this is the solution: 因此,这是解决方案:

export class SmartTableComponent {

  settings = {
    ...
    columns: {
      ...
      firstName: {
        title: 'First Name',
        type: 'string',
        filterFunction:(cell?: any, search?: string) => {
          return this.filterByText(cell.doc[0]['value'], search);
        },
      },
      ...
    },
    ...
  };


  doFilter(cell?: any, search?: string): boolean{
    return true;
  }
}

I got the idea here: Angular 4 , 'this' is undefined when using callback 'onComponentInitFunction' function of ng2-smart table 我的想法是: Angular 4,当使用ng2-smart表的回调'onComponentInitFunction'函数时,'this'是未定义的

Use filterFunction like below像下面这样使用 filterFunction

    ...
    filterFunction: (cell?: any, search?: string): boolean => {          
              return this.doFilter(cell, search);
    }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM