【发布时间】:2021-05-27 23:28:23
【问题描述】:
请帮助我了解如何将此参数传递给formatter 的查找函数。我尝试为制表符动态创建表格行:
有效:
keys = ["name", "price", "number"];
doAjax().then( variable => {
if ( products ) {
let cols = [{title: "number", field: "number"}];
keys.forEach(function ( key ) {
let formatter = "";
// check if key is number
if ( key == "number" ) {
formatter = function( cell ) {
return `<div íd="${key}">${cell.getValue()}</div>`;
};
};
cols.push({title: key, field: "data." + key, formatter: formatter});
});
};
});
不起作用:
let testf = function( cell, key ) {
return `<div íd="${key}">${cell.getValue()}</div>`; // key is not accessible here
};
keys = ["name", "price", "number"];
doAjax().then( variable => {
if ( products !== null ) {
let cols = [{title: "number", field: "number"}];
keys.forEach(function ( key ) {
let formatter = "";
// check if key is number
if ( key == "number" ) {
formatter = testf(cell, key); // not working
};
cols.push({title: key, field: "data." + key, formatter: formatter});
});
};
});
上下文代码:
var table = new Tabulator("#products", {
ajaxURL: "api/products",
columns: cols,
});
我如何重写第二种方法,让它传递参数键?
【问题讨论】:
标签: javascript tabulator