gpt4 book ai didi

javascript - 函数数组 - Javascript

转载 作者:行者123 更新时间:2023-11-29 17:45:18 27 4
gpt4 key购买 nike

我正在开发一个基于 vue 的系统,对于我的表,我使用的是:https://github.com/matfish2/vue-tables-2

该组件有很多选项。可以定义自定义单元格模板以在显示数据之前对其进行操作,例如“created_at”列的这个模板:

 templates: {
created_at(h, row) {
return this.formatDate(row.created_at);
},
//(...)
}

我也可以添加其他模板:

var user_id = function(h,row){return row.id};
...

templates: {
created_at(h, row) {
return this.formatDate(row.created_at);
},
user_id,
(...) // etc

}

但我想将函数分组到一个变量中,这样我就可以重用代码,例如:

 var custom_template = [
{ user_id(h,row){return row.id} },
{ user_name(h,row){return row.name} },
{ created_at(h, row){return this.formatDate(row.created_at)} }
];

templates: {
custom_templates
}

编辑 1:所以我可以有类似的东西:

templates: {                
user_id(h,row){return row.id} ,
user_name(h,row){return row.name} ,
created_at(h, row){return this.formatDate(row.created_at)}
}

这行不通,但有可能吗?我需要额外的代码来实现吗?谢谢! =)

最佳答案

var custom_template = [
function user_id(h, row) {
return row.id;
},
function user_name(h, row) {
return row.name;
},
function created_at(h, row) {
return row.created_at;
}
];

custom_template.forEach(
item => console.log(item(1, {
id: 1,
name: "test",
created_at: new Date()
})));

关于javascript - 函数数组 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50259534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com