【问题标题】:Tabulator 4 can I extend the formatters instead of creating a custom formatter?Tabulator 4 我可以扩展格式化程序而不是创建自定义格式化程序吗?
【发布时间】:2018-10-11 14:26:04
【问题描述】:

Tabulator 有几个内置的单元格格式化程序,如文档中所述 - http://tabulator.info/docs/4.0/format#format

我们知道我们可以像这样构建自定义格式化程序:

var myCustomFormatter=function(cell, formatterParams, onRendered){
return "Mr" + cell.getValue();
}

然后在columns参数中这样使用:

{title:"Name", field:"name", formatter:myCustomFormatter}

虽然制表符格式化程序被“用作”字符串:

{title:"Name", field:"name", formatter:"textarea"}

我们的目标是通过扩展现有的格式化程序,将“myCustomFormatter”添加到参数列的可用选项列表中。

为什么?我们正在构建一些动态的东西,并且 columns 参数将填充一个从 JSON 字符串接收他的值的变量。像这样的:

var jc='[{"title":"Name", "field":"name", "formatter":"myCustomFormatter"}]';
var c=JSON.parse(c);

var table = new Tabulator("#tbdiv", {columns: c});

但此代码不起作用,因为“myCustomFormatter”(作为字符串)在 formatters 参数中不可用。

结论,制表符格式化程序可以用新的单元格格式化程序扩展(不改变原始代码)吗?

如果没有,有什么想法可以填写 columns 参数,以便它使用我们的自定义格式化程序吗?

谢谢

【问题讨论】:

  • c=JSON.parse(js);

标签: javascript tabulator


【解决方案1】:

您可以使用 Tabulator 原型上的 extendModule 函数扩展模块:

//add uppercase formatter to format module
Tabulator.prototype.extendModule("format", "formatters", {
    uppercase:function(cell, formatterParams){
        return cell.getValue().toUpperCase(); //make the contents of the cell uppercase
    }
});

//use formatter in column definition
{title:"name", field:"name", formatter:"uppercase"}

您需要确保在包含源文件之后但在创建第一个表格之前扩展 Tabulator。

完整的文档可以在Tabulator Website Documentation找到

【讨论】:

  • 感谢 Oli,过去我印象中看到了一些关于扩展格式化程序的内容,但我花了 3 天时间搜索文档的错误部分!
猜你喜欢
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多