[英]angular 2 - ngFor dynamic function
I have a table component which takes an input JSON for the table format and another JSON for the data. 我有一个表组件,该组件接受表格式的输入JSON和数据的另一个JSON。 My question is, when my table is rendered with *ngFor, how do I call cellFunction?
我的问题是,当我的表使用* ngFor呈现时,如何调用cellFunction?
My table format JSON: 我的表格格式JSON:
tblFormat= [
{ headerTxt: 'Order ID', df: 'Order_ID', color: 'blue', cellFunction: 'testME1' },
{ headerTxt: 'Buyer Name', df: 'name', color: 'blue',cellFunction: 'testME2' }
]
My Component 我的组件
import { Component, AfterViewInit, ViewChild, Input } from '@angular/core';
@Component({
selector: 'table-comp',
template: `<table class="table table-hover">
<thead>
<tr>
<th *ngFor="let h of tableInfo" [style.color]="h.color">{{h.headerTxt}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let d of data">
<td *ngFor="let c of tableInfo" [style.color]=" how do I? c.cellFunction()">
{{d[c.df]}}
</td>
</tr>
</tbody>
</table>`
inputs: ['data','tableInfo']
})
export class TableComp {
tableInfo=[];
data=[];
}
Depends mainly on where TestME1
and TestME2
are defined. 主要取决于
TestME1
和TestME2
的定义位置。 If it is the TableComp
, you can try doing: 如果是
TableComp
,则可以尝试执行以下操作:
<td *ngFor="let c of tblFormat" [style.color]="this[c.cellFunction]()">
If the functions are defined somewhere else, you just need to replace this
with the Object where it is defined. 如果函数被定义在其他地方,你只需要更换
this
与它被定义的对象。
For example, you can have a service with all the color functions which you inject to the component: 例如,您可以拥有包含注入组件的所有颜色功能的服务:
class TableComp {
constructor(public colorFunctions: ColorFunctions) {}
}
If you have a service like that, you can do: 如果您有这样的服务,则可以执行以下操作:
<td *ngFor="let c of tblFormat" [style.color]="colorFunctions[c.cellFunction]()">
Actually, I have no idea what you are actually trying to do! 实际上,我不知道您实际上要做什么!
I have made this work, please check this link -> https://stackblitz.com/edit/angular-xwsq2a 我已经完成了这项工作,请检查此链接-> https://stackblitz.com/edit/angular-xwsq2a
Add colorChose
method in component, and update template like this, 在组件中添加
colorChose
方法,并像这样更新模板,
<td *ngFor="let c of tblFormat" [style.color]="colorChose(c.cellFunction)">
In component, in colorChose
method 在组件中,在
colorChose
方法中
colorChose (param) {
return this.executeFunctionByName(param, this);
}
Create the executeFunctionByName
method in the same component. 在同一组件中创建
executeFunctionByName
方法。 This is used to call function when you know the function name as string
. 当您知道函数名称为
string
时,此函数用于调用函数。
executeFunctionByName(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}
I hope you have methods mentioned in cellFunction
, in this case testME1
, which returns color name, 我希望您在
cellFunction
中提到了方法,在本例中为testME1
,它返回颜色名称,
testME1 () {
return "red";
}
Hope it helps... 希望能帮助到你...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.